-->
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: Question about transactions
PostPosted: Mon Nov 10, 2003 5:07 am 
Newbie

Joined: Thu Oct 09, 2003 2:52 am
Posts: 6
I have two methods which I want to call transactionally so that if one fails then the other one fails as well. Here's an example:

Code:
public boolean substract(String from, int amount) { ... }
public boolean add(String to, int amount) { ... }
public boolean transfer(String from, String to, int amount){
  ...
  subtract(from, amount);
  add(to, amount);
  ...
}


I start a transaction at the beginning of transfer method, and then commit it if both substract and add return true, and rollback it if either returns false. That's simple and works nicely.

But, if I want to be able to call methods add and subtract individually as well, I would need to create and commit/rollback a transaction in those methods as well. Now if I have understood correcly session.beginTransaction() works fine, it only creates a new transaction if necessary (ie. if I have already created one in transfer method is doesn't create a new one but uses the existing one). But the problem comes with transaction.commit(). I obviously want to call commit in add/subtract method ONLY if a new transaction was created (ie. if calling the method directly).

Any hints on how to do this? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 6:45 am 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I think neither add/subtract nor transfer shoud control transactions - this is a bit different level.

But it you want... You need two versions of add - just "add" and some kind of private internal _add (or addInternal or whatewer name prefer). The latter should only increment the amount but should not care about transactions. All other methods (add and transfer) control transactions and use _add to perform real action.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 10, 2003 7:40 am 
Senior
Senior

Joined: Wed Aug 27, 2003 6:04 am
Posts: 161
Location: Linz, Austria
Essentially, you need some kind of flexible transaction management that automatically detects whether to participate in existing transactions or execute in individual ones, correctly applying commits and rollbacks in any case. Native transactions on JDBC's Connection.commit or Hibernate's Transaction do not handle this, are are arguably not supposed to in the first place.

One solution is to use EJBs, i.e. local Stateless Session Beans. Marking methods as PROPAGATION_REQUIRED will automatically participate in existing transactions but still execute in individual ones else. Of course, it's pretty heavy to make each data access or business object an EJB, but it would work. You would also need to use transactional container DataSources and configure Hibernate's JTATransactionManagerLookup.

The Spring Framework offers a lightweight alternative for such transaction demarcation needs: generic transaction management strategies, with programmatic and declarative means of demarcation. In the Hibernate case, HibernateTransactionManager and JtaTransactionManager are appropriate strategies. Application code does not have to care about the actual strategy as it demarcates transactions in generic ways.

Have a look at the FAQ entry "How to handle transactions that span multiple data access objects?" at http://www.hibernate.org/74.html. For a more elaborate discussion, see the article at http://www.hibernate.org/110.html, in particular sections 5 to 7. Of course, I'm also happy to answer further questions if you're interested!

Juergen
(Spring Framework developer)


Top
 Profile  
 
 Post subject: Related Problem
PostPosted: Thu Aug 19, 2004 3:35 pm 
Newbie

Joined: Thu Aug 19, 2004 7:57 am
Posts: 14
jhoeller wrote:
[snip]

Have a look at the FAQ entry "How to handle transactions that span multiple data access objects?" at http://www.hibernate.org/74.html. For a more elaborate discussion, see the article at http://www.hibernate.org/110.html, in particular sections 5 to 7. Of course, I'm also happy to answer further questions if you're interested!

Juergen
(Spring Framework developer)



Juergen,

I'm wondering if my issue is related to your post here. I'm getting an exception using a case where I span two database operation within on transaction but not if the two operations are in separate transactions. Maybe you could help me understand what I'm doing wrong?

Origincal post:

http://forum.hibernate.org/viewtopic.php?t=933689

_________________
Andy Czerwonka


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.