Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Transactions  RSS feed

 
aakash bhatt
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say in my ejb there are 3 process a, b and c in sequence.
whereas I want to rollback a & c if error occurs but always i had to commit the process b.
So how should i do this
Regards,
aakash
 
Malar Sundar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can achieve this by setting the transaction attribute for the method responsible for 'process b' as
<trans-attribute>RequiresNew</trans-attribute>
in the deployment descriptor of your session bean...
Thanks,
Malar.
 
Nagendra Prasad
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting topic... if possible.. could you pls. elaborate on the following:
A. (Assumption) There are 3 processes (i.e methods) a b and c in a session bean.
B. Where are these invoked from? From within this session bean or from elsewhere?
C. If from elsewhere, if 'b' has a 'Requires New' and then 'c' is invoked, would 'c' not default to the container default transaction setting? Or would it run in the 'Requires New' context as well? This is the bit that confuses me!!
 
aakash bhatt
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If process B is Required New, then if process C Transaction is Supports or Required then it will follow the transaction of process B.
If process A is Required New
process B is Not Sopported or Never
process C is Supports or Required
I think this will solve
Regards,
aakash
 
Malar Sundar
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Let me elaborate...
The Session bean has 3 methods method a(), b(), and c(). To group all these three methods in a usecase, you should also have a seperate method, say x() in the same Session bean (or you can have it in other Session bean also, in that case methods a, b, and c will be called from that bean. For simplicity let it be in the same bean itself, for our discussion)
Transaction attributes:
method x() - Required
method a() - Required
method b() - RequiresNew
method c() - Required.
With this setup, if you invoke method x() on the session bean, a new Transaction(say T1) is started. now method a() will use the same tranaction (T1). For method b() a new Transaction (T2) will be created and T1 will be suspended. When method b() completes, the container commits or rollback the transaction T2, and T1 resumes. So now method c() uses T1 for it's processing.
Be sure to handle the exceptions correctly...
method x()
{
try {
a(); // uses Tx T1...
b(); // creates new Tx T2...
c(); // uses Tx T1...
}
catch(Exception e)
{
// notify the container to rollback the Tx
getSessionContext().setRollbackOnly();
}
}
follow the same way for every method...
Hope this helps...
Thanks,
Malar.
 
Siva Jagadeesan
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having "requires new" attribute for process B will take care of this problem. However, one thing, I am not sure about the question is whether you want the Process B to be part of the parent Transaction. For example, the client initiated the transaction and you want the other three processes (or better to say TASKS) to run in that transaction. The other requirement you have is even if one Task fails you want the other task and the transaction to commit. This is a perfect scenario for Nested Transaction. Nested Transaction is still not a part of J2EE, so I am not sure whether the App Server will support it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!