-->
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: Nested transactions, how?
PostPosted: Mon Jan 26, 2004 6:07 pm 
Beginner
Beginner

Joined: Fri Oct 10, 2003 10:30 am
Posts: 35
Location: Stockholm
Hi guys, what's the best framework for this problem?

I have a servlet which has to perform a "main" insert and a set of "subinserts".
in pseudo language:

Code:
insert a category;
for(1 to 10)
  insert a child category.


Now, since a child category insert may fail and I will still be happy with the global transaction, I need to insert the subcategories in a separate session/transaction, like this


Code:
mainsession.open
insert a category;
for(1 to 10) {
   subsession.open
   insert a child category.
   subsession.close
}
mainsession.close

so that I can still use the mainsession for later use.

This would work except that i run in leaking connections. (if I have two free connections in my pool, two thread may get simoultaneously to "subsession.open") and I get in dead lock.
Once I solved the problem using two different connection pools for the session and the subsession but I wonder if there is a better solution.

Any ideas?
Thanx,
Roberto


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 26, 2004 10:26 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I may miss understand you issue but is usualy best to avoid nested transactions. You can serialise your Tx operations, eg, pCode

session.open
insert a category;
session.close
session.open
for(1 to 10) {
insert a child category.
}
session.close


You could move the second session/Tx inside the loop if you wish to not have all child insert operations under the one transaction.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 27, 2004 2:59 am 
Beginner
Beginner

Joined: Fri Oct 10, 2003 10:30 am
Posts: 35
Location: Stockholm
You are right.
Unfortunately serialization is sometimes impossible, like in the "open session in view", where a session is opened and closed before and after "everithing".
/rob


Top
 Profile  
 
 Post subject: Re: Nested transactions, how?
PostPosted: Tue Jan 27, 2004 1:00 pm 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
A deadlock can be avoided with a single connection pool too, via proper configuration: For example, Resin's pool (http://www.caucho.com/resin-3.0/db/config.xtp) supports a "max-overflow-connections" parameter, specifying how many "overflow connections" may be created when a connection wait times out.

So if two main Sessions should block because each is waiting for a sub-connection from a pool that doesn't have any idle connecitons left, they would receive an "overflow connection" after the specified wait time. If no overflow is configured, an exception would get thrown after the wait time - so no deadlock in any case (as far as I see).

The general demarcation problem can be solved by transaction infrastructure through a "transaction suspension" option. EJB CMT offers the transaction attribute "REQUIRES_NEW", which will suspend the current transaction, execute within a new one, then resume the old transaction. If Hibernate Sessions are bound to those transactions, this seems to be exactly what you're trying to achieve.

Of course, with EJB, you'd need to write corresponding Hibernate Session management yourself, because EJB will just care about its JTA transactions.

The upcoming Spring Framework release 1.0 RC1 offers such transaction suspension for POJOs, as a new feature of its generic transaction infrastructure. Resources like Hibernate Sessions are automatically bound to transactions, therefore will get suspended and resumed with transactions. This works with all of Spring's transaction strategies, for example HibernateTransactionManager and JtaTransactionManager.

As a side note, you can use multiple DataSources and multiple SessionFactories in Spring, either each with their own transaction management or with JTA transactions spanning all of them.

Juergen
Spring Framework developer
http://www.springframework.org
http://www.hibernate.org/110.html


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.