java - Is it possible to propagate a transaction through different EJB 3.1 and several database servers? -
the problem this, have 3 ejb, ejb_a have connection server database 1, ejb_b have connection server database 2 , third ejb_c responsible calling first two.
when there error in ejb_a or ejb_b rollback done in ejb there error, on other ejb saved done, eventhough should propagate rollback 2 ejb.
ejb_c:
@stateless @localbean @transactionmanagement( transactionmanagementtype.bean ) public class ejb_c { private ejb_a ejba; private ejb_b ejbb; @postconstruct public void init(){ context context = null; try { context = new initialcontext(); this.ayudantesessionbean = (ayudantesessionbean) context.lookup("java:global/trans1/trans1dba/ejb_a"); this.ayudantesessionbean = (ayudantesessionbean) context.lookup("java:global/trans1/trans1dba/ejb_b"); } catch (namingexception ex) { logger.getlogger(orquestadorsessionbean.class.getname()).log(level.severe, null, ex); } } public void savetwoejbs(entitya, a, entityb b){ try{ ejba.save(a); ejbb.save(b); }catch (exception ex) { } } }
ejb_a:
@stateless @localbean public class ejb_a { @persistencecontext( unitname = "persista" ) private entitymanager em; @transactionattribute(transactionattributetype.required) public void guardaramigo(entitya a){ em.persist(a); } }
ejb_b:
@stateless @localbean public class ejb_b { @persistencecontext( unitname = "persistb" ) private entitymanager em; @transactionattribute(transactionattributetype.required) public void guardaramigo(entityb b){ em.persist(b); } }
could me?
well, make work need setup distributed transactions (xa) properly:
- check datasources xa-ready. typically means different jdbc driver class name , different type of connection pool during setup. process vendor-specified, described in documentation of application server.
you need remove annotation:
@transactionmanagement( transactionmanagementtype.bean )
to let container (actually transaction broker) manage transactions instead of bean. 3. need add @transactionattribute annotation on method savetwoejbs :
@transactionattribute(transactionattributetype.required) public void savetwoejbs(entitya, a, entityb b)
it's not required there no reason lookup ejb beans via jndi context, inject them via @ejb annotation:
@ejb private ejb_a ejba; @ejb private ejb_b ejbb;
some links go:
http://docs.oracle.com/cd/e13222_01/wls/docs100/ejb30/examples.html http://docs.oracle.com/cd/e16439_01/doc.1013/e13981/servtran001.htm http://entjavastuff.blogspot.ru/2011/02/ejb-transaction-management-going-deeper.html
Comments
Post a Comment