-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: How to "alter" a JDBC connection
PostPosted: Thu Apr 26, 2007 4:23 am 
Newbie

Joined: Thu Apr 26, 2007 4:15 am
Posts: 9
Hi,

I'm looking for a way to alter JDBC connections before they are used by hibernate/entity manager. Specifically I want to perform few "alter session" commands to set up the NLS environment.
(AFAIK I can't do this with the connection string.)

Thanks in advance,
Genady


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 26, 2007 7:43 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Depending on when those ALTER statements have to be issued, you might be able to get away with doing this in an Interceptor:

http://www.hibernate.org/hib_docs/v3/ap ... eptor.html

Otherwise I think you'll have to write a ConnectionProvider (you can probably extend DataSourceConnectionProvider). You could touch the Connection before it's handed off to Session.

-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 30, 2007 3:29 pm 
Newbie

Joined: Sun Apr 22, 2007 12:57 am
Posts: 7
There are two ways to go about it.
1) Subclass the datasource. It's little tricky to do if you want to do it in efficient manner by executing alter session on per physical connection instead of logical connection.
2) Make use of Oracle logon_trigger. This will work very well if your parameter is a constant or computed using db queries. Please consult your DBA before doing it. There are few things to remember,
i) Any error raise by logon_trigger doesn't stop DBAs from logging on (Oracle 8.1.6 and above have this feature). So be careful if you are using a database version from museum.
ii) They are not executed for internal or SYSDBA/SYSOPER.

Code:
create or replace trigger logon_trigger after logon on database
declare
begin
if user = 'SCOTT'
then
   ---alter session....
end if;
end;


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 06, 2007 8:47 am 
Newbie

Joined: Thu Apr 26, 2007 4:15 am
Posts: 9
Thanks everybody!

Chris - Can I implement your suggestion in some way that does not use hibernate APIs?

Genady


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 07, 2007 9:26 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Neither interface (ConnectionProvider or Interceptor) is standardized in JPA. However in either case, the bulk of your code is isolated because the implementation you provide is one class and it is specified in configuration; your code never knows about it. This is acceptable in most cases to folks who don't want Hibernate API's in their app.

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 9:02 am 
Newbie

Joined: Thu Apr 26, 2007 4:15 am
Posts: 9
I've tried the ConnectionProvider solution. I've subclassed DatasourceConnectionProvider, added
the hibernate.connection.datasource and hibernate.connection.provider_class properties to the xmls and
now I can issue an "alter session" statements whenever a new connection is returned.

This solution is nice since it reuses the datasource defined elsewhere. The downside is that I'd rather issue these statements on each new physical connection. Subclassing DriverManagerConnectionProvider seems like a good option, but I'm not sure how it will interact with the datasource resource we have.

How can I subclass a standard datasource and tell jboss/hibernate to use it (before resorting to the logon scripts solution).

Thank you all!
Genady


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 09, 2007 3:17 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
The opening of the physical connection is out of Hibernate's hands because of the DataSource abstraction. You might consider manually constructing a connection pool using Apache DBCP or the vendor of your choice. It's not so hard with BasicDataSource:

http://jakarta.apache.org/commons/dbcp/ ... ource.html

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.