• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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
 
A teeny tiny vulgar attempt to get you to buy our stuff
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic