• Post Reply Bookmark Topic Watch Topic
  • New Topic

Transactions, Session Beand & Entity Beans

 
Aleksey Matiychenko
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a session bean that receieves an arraylist of objects each one containing data for an entity bean. The session bean goes through the list and creates an entity bean for each object.
That works fine but what we want is to rollback all created objects if any one of them fails. I tried a few things (other then doing remove on entity beans):
1. Marked both entity bean and session bean for CM Transaction. Made the transaction attribute "Required" on both sides. Made Session bean implement SessionSynchronization.
In this case I can see transaction start but never see beforeCompletion or afterCompletion methods of SessionSynchronization fired. The entity beans are not rolled back.
2. Marked Session bean as BM transcation and wrote my own code to start and end transaction. That did not do much either.
Any suggestions ?
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All you should have to do is mark both Entity and Session as Required.
That will do two things:
1) Ensure that you have a transaction opened for you by the container when you call the session method.
2) Ensure that the transaction context is passed to the call on each entity bean.
So everybody's in one big happy transaction.
The only thing left is to make sure you get a signal to the container when you want the transaction to roll back. There are two ways to do this:
1) Throw a "System Exception". This is a subclass of RuntimeException or RemoteException thrown from either one of the entity beans or from the session bean.
or 2) Call EJBContext.setRollbackOnly()
Note that other checked (non-runtime, non-remote) exceptions do not trigger a rollback - you will have to catch these and call setRollbackOnly if that is what you want to happen. This includes things like CreateException, etc.

Also, I'm not sure about the home methods. It used to be that some containers would not include things like ejbCreate in a transaction by default. I think this is cleared up now, but if you see problems with this you might want to ensure that your transaction settings in ejb-jar.xml specifically mention the home interface.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!