• Post Reply Bookmark Topic Watch Topic
  • New Topic

2 phase commit using EJB  RSS feed

 
Chairmakani Rajesh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would to know how to achive 2 phase commit using EJB... Basically in my project I'm using more 1 database... If I do something in one database, it should reflect in the next database. And I'm not going to use Triggers also... Would like to know how to solve in the application level.
 
Vladimir Ergovic
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use UserTransaction from stateless session bean and set bean transaction to bean managed transaction so you will have good control. You can also use just ordinary Java class outside container. I asume that you are using Entity Beans (transaction in each is set to required)
Here is sample code (from my mind):
String lookup = "jta/usertransaction";
UserTransactiontx = (UserTransaction)initial.lookup(lookup);
if (tx != null) {
tx.begin();
bean1.create("blablaPK");
bean2.create("blablaPK2");
tx.commit();
success=true;
catch(...){success=false;...}
} finally {
if (success) {
try {
tx.commit();
} catch (Exception e1) {..}
} else {
try {
tx.rollback();
} catch (Exception e1) {..}
}
}
 
Chris Mathews
Ranch Hand
Posts: 2712
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use Container Managed Transactions?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!