• Post Reply Bookmark Topic Watch Topic
  • New Topic

what transaction attribute has to be used?  RSS feed

 
Balaji Munuswamy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got one doubt in Transaction.

I have one public 'process()' in EJB which is exposed to client. This method in tern will call 'updateUSer()' and 'auditUser()' methods.

My requirement is if the 'updateUSer()' fails, Transaction should rollback only to updateUSer and the other method(auditUser) has to completed the Transaction successfully. I have to use container managed bean and only one method shoud be expose to client.

For this requirement, what transaction attributes has to be set for this requirement?

Thanks in advance.

Balaji.M
 
Alex Sharkoff
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Balaji,

In order to achieve your requirements I'd do the following:

1. Expose updateUSer() and auditUser() methods in the component interface.

This is because transaction attributes can be only specified on business methods declared in the component interface (this is true for SB; for entity beans can also specify on home methods defined by bean provider plus remove methods from EJBObject and EJBHome interfaces; for MDB - only can define trans attribute on onMessage( Message )) method)

As you want to expose one method (process()) in the comp interface you could define updateUser() and auditUser() methods in a different bean that has only local interface (only local clients can access it).

2. Make updateUSer() run in its own transaction - in ejb-jar.xml <container-transaction> section associate this method with RequiresNew transaction attribute. This will allow the results of this methods to be commited/rollbacked as soon as the method is completed - does not depend on the outcome of the transaction in which process() and auditUser() run.

3. Make auditUser() run in the caller's transaction (i.e. a transaction associated with the process()) method) - use Required transaction attribute. This will ensure that the results of auditUser() are only committed if process()) method completes successfully.
If you want auditUser() to be also independent from the caller (i.e. process()) method) then use RequiresNew (similar to updateUSer()).


Hope it helps
 
Balaji Munuswamy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex,

Your response is very helpful. Thank you very much.

I have few more doubts,

1)Component interface for Stateless bean is remote interface right? So it is not possible to use transaction without exposing the method to client is it?
2)As you mentioned I am using NotSupported attribute for process () and RequiresNew for other two methods. I am using NotSupported for process() because I am doing some query with will take more time so I don�t want to involve that query in transaction. I am getting IllegalStateException when I use ctx.setRollbackonly() in updateUSer. Is there any way to solve this problem?

Thanks,
Balaji.m
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1)Component interface for Stateless bean is remote interface right? So it is not possible to use transaction without exposing the method to client is it?


The only explanation i can think of is that EJB manages transaction and other middleware services for you by intercepting the call. So EJBObject intercepts only those calls that you want to expose to the client and applies all the middleware serices configured through the descriptor.



As you mentioned I am using NotSupported attribute for process () and RequiresNew for other two methods. I am using NotSupported for process() because I am doing some query with will take more time so I don�t want to involve that query in transaction. I am getting IllegalStateException when I use ctx.setRollbackonly() in updateUSer. Is there any way to solve this problem?



uhhm, this exception can get thrown if you invoke it within the process() method. Where are you invoking ctx.setRollbackonly() ? *within* the updateUser() method / in process()?
 
Alex Sharkoff
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Balaji,

Good to hear you found it helpful.

A few thoughts....


Component interface for Stateless bean is remote interface right?

It can also be local. Make your component interface extend EJBLocalObject and home interface extend EJBLocalHome; in ejb-jar.xml



getting IllegalStateException when I use ctx.setRollbackonly() in updateUSer


As Karthik mentioned first of all check that you call this method from within updateUser method.

Have you exposed an updateUser method in a component interface (can be in the same bean or another bean)? Remember that in order to make Container do transaction stuff one must invoke methods through the component interface. Therefore, even if you have got two methods in the same bean you will have to invoke another method (i.e. updateUser()) through a bean's component interface:



Big IF.
The above piece of code is only legal if your session bean is stateless one
If it is a stateful sb such access is illegal as it will be considered a concurrent access to a stateful session bean.

Hope it helps
 
Balaji Munuswamy
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you , It is working fine.

We are using Stateless session bean so I am creating EJBObject and call other methods in process(). It works fine.

(remoteinterface) ctx.getEJBObject();

Thanks,
Balaji.M
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!