-->
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.  [ 8 posts ] 
Author Message
 Post subject: How do I get JDBC connections from Hibernate?
PostPosted: Mon Feb 16, 2004 4:58 am 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
What's the official way to do this?

I'm using the following code to configure Hibernate:
Code:
Configuration config = new Configuration().configure("/hibernate.cfg.xml");
SessionFactory sessionFactory = config.buildSessionFactory();


From there hibernate just deals with all the JDBC crap and everything goes along just swimmingly.
But now I have to get my hands dirty with a direct JDBC connection.
Given this configuration setup, what's the best way to get a direct JDBC connection?
I figure the best thing to do is get the ConnectionProvider Hibernate is using, because (a) it seems obvious and (b) one day I'm going to need to get the ConnectionProvider in order to register a Proxool lifecycle listener.

I can do dodgy things like:

    Cast SessionFactory to SessionFactoryImplementor and then call getConnectionProvider() (this is how our app does it currently, but it makes me feel dirty so I want to find a Better Way).
    Register our Custom ConnectionProvider subclass with a singleton and then call that to get connections.


But how am I supposed to do this?
I don't particularly want to create/close a session just to use it's connection, or is that "the" way to do it?

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 5:52 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Either, session.connection()
Or give Hibernate a userConnection and manage it yourself.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 8:18 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
So you think it's OK to cast SessionFactory to SessionFactoryImplementor?
Same for other classes, like casting Session to SessionImpl?

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 16, 2004 10:34 pm 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
I route all my dataaccess through hibernate- that is, if I need to do something in jdbc, I retrieve the connection via session.connection().

Using the hibernate config (xml) files to move from local jdbc connection in test/unit testing, and changing to a jndi source is very simple.

No problems so far, although keep in mind that you autocommit may not default to "on" when you get a connection this way.

James


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 12:38 am 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
stolley wrote:
So you think it's OK to cast SessionFactory to SessionFactoryImplementor?
Same for other classes, like casting Session to SessionImpl?


I don't see the need for the cast. Session declares:

Code:
public Connection connection() throws HibernateException;

This is on Session, not SessionFactory. SessionFactory, as its name implies, will give you the session. You may want to look at the ThreadLocal pattern if you haven't already, it gives a good, simple, implementation for managing sessions (and hence connections).

http://www.hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 12:43 am 
Pro
Pro

Joined: Tue Aug 26, 2003 8:07 pm
Posts: 229
Location: Brisbane, Australia
Our application currently does the implementor casting in order to get the ConnectionProvider so that it can create new JDBC connections.

So it seems like the consensus is that when you want to use a JDBC connection directly, the correct way to do it is to get a brand new Hibernate Session, use it's connection, then clean up the Hibernate Session (as opposed to trying to get Connection objects from the ConnectionProvider)?

_________________
Cheers,
Shorn.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 3:50 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
stolley wrote:
So you think it's OK to cast SessionFactory to SessionFactoryImplementor?
Same for other classes, like casting Session to SessionImpl?

Never cast on internal Hibernate API.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 03, 2004 12:21 am 
Regular
Regular

Joined: Wed Dec 31, 2003 4:26 am
Posts: 108
Location: Berkeley, CA
stolley wrote:
So it seems like the consensus is that when you want to use a JDBC connection directly, the correct way to do it is to get a brand new Hibernate Session, use it's connection, then clean up the Hibernate Session?


I'd recommend using an Inversion-of-control (aka, Dependency Injection) framework such as Spring for this. Let it set up the DataSource and supply it to whatever needs it: Hibernate or your own JDBC logic.

That's what I'd say is a "correct" approach.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.