-->
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: joining an existing transaction with JDBCTransaction
PostPosted: Thu May 27, 2004 9:48 am 
Newbie

Joined: Sat May 22, 2004 4:57 pm
Posts: 9
I'd like to get JTA/EJB-like "join-existing-transaction" semantics in the simple case (sharing a single conn to the same DB, one-phase commit only) in Tomcat without having to dump more JARs in common/lib (e.g., JOTM) or migrate to Spring framework just yet. The default JDBCTransaction does not support this, and I was wondering if there is a solution floating around before reinventing the wheel.

We have code that looks like this:

Code:
A(session) {
  tx1 = session.beginTransaction();
  ... do stuff ...
  B(session);
  ... do more stuff ..
  tx1.commit();
}
 
B(session) {
  tx2 = session.beginTransaction();
  ... do stuff ...
  tx2.commit();
}


We want to avoid the default JDBCTransaction behavior, where tx2.commit() in B() will call connection.commit() on the underlying JDBC connection right away, preventing the outer transaction from being able to roll back properly. Has anyone had any luck with a lightweight solution for this, like introducing a ThreadLocal or similar into JDBCTransaction?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 27, 2004 9:50 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can put the Hibernate Transaction in a ThreadLocal and begin/commit/rollback in a Servlet filter. A good place to do that is the HibernateUtil class from the reference documentation.

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 27, 2004 10:03 am 
Newbie

Joined: Sat May 22, 2004 4:57 pm
Posts: 9
christian wrote:
You can put the Hibernate Transaction in a ThreadLocal and begin/commit/rollback in a Servlet filter. A good place to do that is the HibernateUtil class from the reference documentation.


We'd like to keep transaction scope minimized to the business method/service where it's active, rather than keeping a transaction open through the presentation layer/rendering phase. Is that possible?

Would it be reasonable to make my own Transaction like JDBCTransaction, that keeps a ThreadLocal to indicate whether a transaction was already in progress? beginTransaction and commit would become no-ops unless you're at the top level. rollback() would set a flag like UserTransaction.setRollbackOnly does. then top-level commit() either commits or rolls back depending on the rollbackOnly flag.

Has anyone done anything like that?


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 27, 2004 10:06 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This is really trivial: First, use the Hibernate Transaction API to keep your code portable. Then, use a ThreadLocal to hold the Transaction object for the current thread. Begin and commit whenever you like by calling static methods on that ThreadLocal utility class. Just have a look at HibernateUtil and try to understand how it handles the Session, its pretty much the same for Transaction.

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


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.