-->
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 do a multi-step transaction?
PostPosted: Wed Apr 21, 2010 12:18 pm 
Newbie

Joined: Wed Apr 14, 2010 4:14 pm
Posts: 5
I have two discrete saves that needed to be wrapped as a single transaction. It is not clear how to do this. I have tried the following:

@Transactional(propagation=Propagation.REQUIRES_NEW, rollbackFor=Exception.class)
controller() method
--- prepares first data set by creating domain objs and populating values
--- invokes method A that uses em to persist
--- prepares second data set by creating domain objs and populating values
--- invokes method B that uses em to persist

both method A and method B, residing in other service objs, have @Transactional annotation also.

How can I ensure that the controller method rolls back if either method a or b fail? Do I both persist and flush on both A and B or just persist at that level and then Flush at the controller level?


Top
 Profile  
 
 Post subject: Re: How to do a multi-step transaction?
PostPosted: Thu Apr 22, 2010 3:02 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
You mean something like this?:
Code:
Session session = ...;
Transaction transaction = session.beginTransaction();
try
{
   A();
   B();
   transaction.commit();
}
catch (Exception e)
{
   transaction.rollback();
}
session.flush();
session.close();


Top
 Profile  
 
 Post subject: Re: How to do a multi-step transaction?
PostPosted: Thu Apr 22, 2010 1:12 pm 
Newbie

Joined: Wed Apr 14, 2010 4:14 pm
Posts: 5
Thanks for the reply. That is indeed what I mean. I am however, using Container-managed transactions. I am having a difficult time getting this type of transaction set to work. I am using Spring, AOP and Hibernate and I can not get my @Transaction and EntityManager to work as I want. I can get A and B to work but I can not get the main part to control the logical unit. So in the CMT world want the following:

@Transaction
class1.transManager()
{
// start transaction here
EntityManager em = getEntityManager()
try
{
class2.doA()
class3.doB()
em.flush()
}
catch
{
em.clear
}
}

class2.doA (Join the transaction started above)
{
EntityManager em = getEntityManager()
try
{

em.persist(stuff)
}
catch
{
throw
}
}

class3.doB (Join the transaction started above)
{
EntityManager em = getEntityManager()
try
{

em.persist(stuff)
}
catch
{
throw
}
}

Maybe I should not use CMT but maybe manage it at a session level. Any thoughts?


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.