-->
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.  [ 4 posts ] 
Author Message
 Post subject: Confusion when using direct jdbc and hibernate together
PostPosted: Tue May 09, 2006 2:58 pm 
Newbie

Joined: Tue Mar 28, 2006 11:46 am
Posts: 10
Hi,

I am using hibernate version: 3.1.3

I have some confusion when using direct jdbc and hibernate together. In my code first I am using jdbc connection directly and in the same connection I want to perform some hibernate stuff. I want to perform the whole thing in one connection so in case of failure everything should be rolled back. So I am passing same connection to hibernate to open a session. My confusion is do I suppose to do tx.commit() or in the last of my code conn.commit(). If I am suppose to do only conn.commit() then is hibernate will take care of tx.commit().

Basically I am just confuse in which order things should be done. Do I need to open and commit on transaction? If I commit on transaction does it will commit operations which I have performed using direct jdbc?

My codes looks like:

// Step 1: obtain jdbc connection and do some insert
Connection conn = DriverManager.getConnection(url, name, password);
String insert = "INSERT into REPORT (ID, RESULT) VALUES (?, ?)";
PreparedStatement stmt = conn.prepareStatement(insert);
stmt.setLong(1, 1);
stmt.setString(2, "SUCCESS");
stmt.executeUpdate();
stmt.close();

// Step 2: do some hibernate work
Session session = SessionFactory.openSession(conn);
Transaction tx = session.beginTransaction();
session.save(userObject);
session.save(userObject2);
tx.commit();
HibernateSessionFactory.closeSession();

// Step 3: again perform some direct jdbc work
PreparedStatement stmt2 = conn.prepareStatement(insert);
stmt2.setLong(1, 2);
stmt2.setString(2, "SUCCESS");
stmt2.executeUpdate();
stmt2.close();

conn.commit();

Thanks for your help.

Sanjeev


Last edited by ssingal on Wed May 10, 2006 9:58 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 3:41 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
If it is simple work as given in your example, why dont you do everything through Hibernate approach. There are many advantages going with Hibernate than direct JDBC.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 3:49 pm 
Newbie

Joined: Tue Mar 28, 2006 11:46 am
Posts: 10
We do want to use direct jdbc and hibernate together because our existing implementation is already using jdbc and the hibernate we want to use it for new objects and tables.

If there is a mix match db requests of direct jdbc and hibernate we want to perform everything in one connection so that in case of failure everything rollback correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 09, 2006 4:12 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
http://www.hibernate.org/hib_docs/v3/ap ... l#ON_CLOSE

which will be the case with user supplied connections. Dont know what exactly the semantics of 'release mode'. Also these settings depend on your configuration information for session factory.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.