Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transaction in EJB

 
kri shan
Ranch Hand
Posts: 1462
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i handle the failure Transaction in EJB?
If power goes off / server crash in the middle of the Transaction, can i handle this situation thru 2 phase-commit (or) rollback?
 
Shahri Shahri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kri Shan,

Just check the article in the following url: http://www.kevinboone.com/ejb-transactions.html
ejb-transactions
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How can i handle the failure Transaction in EJB?

There are two schools of thought here (supposing you implement CMT):
  • Let the container roll back the transaction automatically.
  • Catch the exception inside of your code and manually roll back the transaction manually.


  • Believe or not but 1 is tricky, because the container won�t automatically roll back the transaction in all situations. The transaction gets rolled back only if the exception is a runtime exception. If your methods throw application exception, then you might consider 2.
    The second one is very straightforward: all you need to do is to catch the exception and call the setRollbackOnly() on the EJBContext.

    If power goes off / server crash in the middle of the Transaction, can i handle this situation thru 2 phase-commit (or) rollback?

    Not sure if I understand you exactly. The 2PC protocol is used with distributed transactions. If you rollback your local transaction using any of the methods above and your transaction is running within a distributed context, then the 2PC will assure you that the whole-distributed transaction will get rolled back as well. You don�t need to do anything about it.
    Regards.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic