Win a copy of Rust Web Development this week in the Other Languages 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Transaction in ejbCreate() and ejbPostCreate()

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic