-->
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: Get java.sql.Connection from ???
PostPosted: Sat Nov 08, 2003 6:44 pm 
Beginner
Beginner

Joined: Sat Aug 30, 2003 1:36 am
Posts: 47
Location: Calgary, AB
Can you get a java.sql.Connection for use with native jdbc? So I don't have to set up a separeate outside hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 08, 2003 6:45 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
mySession.connection().<do stuff>

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 08, 2003 6:53 pm 
Beginner
Beginner

Joined: Sat Aug 30, 2003 1:36 am
Posts: 47
Location: Calgary, AB
Ok, how would you clean up after this, will mySession.close() clean up that connection? Notice in the api you have to commit the connection as well? or would committing the session after using the connection commit the connection?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 08, 2003 7:01 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If the Session got the connection from a JDBC pool, it will return it on close(), otherwise you can re-use the Connection you passed in with SessionFactory.openSession(myConnection).

Don't manually commit() the the Connection. It is possible, but much more convenient to use the Hibernate Transaction API:

Session session = sf.openSession(); // Get it from JDBC pool
Transaction tx = session.beginTransaction();

// do some work here

tx.commit();
session.close();

Throw in some Exception handling and you are fine.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 08, 2003 7:12 pm 
Beginner
Beginner

Joined: Sat Aug 30, 2003 1:36 am
Posts: 47
Location: Calgary, AB
Ok, so to use some hand coded sql though, it woudl be something like this:

Session session = sf.openSession(); // Get it from JDBC pool
Connection conn = session.connection();
Transaction tx = session.beginTransaction();

// do some work here with the conn object

tx.commit();
session.close();

That would be the right way to use that Connection?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 08, 2003 7:19 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Well, I'd get the connection() after I've started the Transaction, but I guess it doesn't matter.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 08, 2003 7:46 pm 
Beginner
Beginner

Joined: Sat Aug 30, 2003 1:36 am
Posts: 47
Location: Calgary, AB
great, thanks!


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.