• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

2 phase commit using EJB

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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) {..}
}
}
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not use Container Managed Transactions?
 
Do not set lab on fire. Or this tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic