• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transaction in ejbCreate() and ejbPostCreate()

 
Vince Hon
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
 
alzamabar
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.
 
raghu vadranam
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic