Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Transactions, Session Beand & Entity Beans

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
We're all out of roofs. But we still have tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic