This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Transaction in ejbCreate() and ejbPostCreate()

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a CMT entity bean contains the ejbCreate() and ejbPostCreate() methods:




I try out that once the ejbCreate() is executed, record is inserted into database "physically". When there is a CreateException occur in ejbPostCreate(), since I used the setRollbackOnly(), the foreign key value in the database is NULL.

I understand this situation, however, if I would like to:
If CreateException occur either in ejbCreate() and ejbPostCreate(), NO RECROD should be inserted into database. How can I do that ? By setting <trans-attribute> for these 2 methods ?

I want to do this is because of my business logic, I do not want the database to store garbage if any mistake take place during the "create" process.

Thanks
 
Ranch Hand
Posts: 379
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Vince Hon:
I have a CMT entity bean contains the ejbCreate() and ejbPostCreate() methods:




I try out that once the ejbCreate() is executed, record is inserted into database "physically". When there is a CreateException occur in ejbPostCreate(), since I used the setRollbackOnly(), the foreign key value in the database is NULL.

I understand this situation, however, if I would like to:
If CreateException occur either in ejbCreate() and ejbPostCreate(), NO RECROD should be inserted into database. How can I do that ? By setting <trans-attribute> for these 2 methods ?

I want to do this is because of my business logic, I do not want the database to store garbage if any mistake take place during the "create" process.

Thanks



According to the EJB specs 18.3.1, ejbCreate<METHOD> is a business method. Hence, you can invoke ctx.setRollbankOnly() also from the ejbCreate. This way, when a CreateException will be thrown, the container will not commit the db record insertion and you will have your database as clean as before.
 
Vince Hon
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


According to the EJB specs 18.3.1, ejbCreate<METHOD> is a business method.



thanks for yr reply.

ejbCreate<METHOD> -- do u mean it is the one in the bean class ?
I have added the
try{
//....
}catch (CreateException e){
ctx.setRollbackOnly;
}
But the compiler said "CreateException" is never thrown, how can I handle this ? Thank you.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you can use a boolean variable in ejbCreate to know whether any exception occured or not. (set it to true if exception occurs) Take the decision whether to insert a row in ejbPostCreate or not depending on boolean value
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic