-->
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.  [ 3 posts ] 
Author Message
 Post subject: How to maintain the session throughout a transaction?
PostPosted: Sat May 21, 2005 10:16 am 
Newbie

Joined: Mon May 16, 2005 4:40 am
Posts: 5
Hello,

Could someone help me in handling this problem ?

Say a transaction involves 3 database calls and i need to commit the transaction only when all three database calls succeed without any exceptions. This means that i cannot create, commit and close a seperate session for each of these 3 calls. How do i maintain the session across the transaction.

thanks,
godhs


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 21, 2005 2:45 pm 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
I can not find the article, but I remember reading that in Hibernate 3, when you set up you hibernate.cfg.xml file with your transaction factory set to JTATransaction and your transaction manager set to Weblogic or JBoss etc, the session factory would automatically attach any Session you request to the current ongoing transaction.
Code:
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>


If you can not have it to work, you can synchronize it yourself (which we did, but it's a lot of code) by implementing the javax.transaction.Synchronization interface and accessing your transaction manager with the following code:

Code:
private TransactionManager getTransactionManager()
{
    return (( SessionFactoryImplementor ) factory).getTransactionManager();
}

_________________
Vincent Giguère
J2EE Developer


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 21, 2005 6:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You can structure your code into layers.
Top layer is the Transaction use case, eg, doThreeOperations()
This is called from or is the call to a JTA CMT transaction method. If not using JTA then this is where you start and commit/rollback the transaction.

Then the next layer (DAO) has the various operations. eg

doThreeOperations() {
// TX started
try {
op1DAO.doOperation1();
op2DAO.doOperation2();
op3DAO.doOperation3();
}
catch(Exception e) {
doRollback();
}
// Tx Committed unless rollback hint provided.
}

All the DAO operations need is to call a utility class that provides the current session by using (and populating) a thread local. See WIKI for some code.
The thread local makes sure you get the same session for the current execution stream.


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