Discussion:
EJB Transaction inter server propagation
Darv
2014-08-13 14:11:54 UTC
Permalink
I am having some difficulty with transaction propagation between two EJBs on
seperate servers (which I understand can be done based on section 13.2.3 of
the EJB3.1 spec), and I would appreciate if some who might have done this,
could help me.

I am a bit green with EJBs so please excuse me. My initial thought was to
use RemoteInitialContextFactory to initiate the context and then lookup the
remote EJB. While the EJBs communicate ok, unfortunately the transaction
does not propagate, even if the 2 EJB's are on the same server (but
different projects). The @EJB annotation on the remote interface however
does propagate the transaction when on the same machine (diff projects).

The prototype code below is where MyFirstEjbBean saves a customer and then
calls MySecondEjbBean which save another customer on a seperate schema.
MyFirstEjbBean can then blow up to test the rollback. What annotations and
xml definitions are required to enable this to work inter servers? An
example would be greatly appreciated.

@Stateless
public class MyFirstEjbBean implements MyFirstEjb, MyFirstEjbRemote {

@PersistenceContext(name="EJBPrototypesPU")
private EntityManager em;

@EJB
MySecondEjbRemote mySecondEjb;

public void saveCustomers(){
Customer customer = new Customer("name1", "address1", "city1");
em.persist(customer);

mySecondEjb.saveCustomer();

// throw new IllegalStateException("Blowing up!");
}
}


@Stateless
public class MySecondEjbBean implements MySecondEjb, MySecondEjbRemote {

@PersistenceContext(name="EJBPrototypesPU")
private EntityManager em;

@TransactionAttribute(TransactionAttributeType.MANDATORY)
public void saveCustomer() {
// TODO Auto-generated method stub

Customer cust2 = new Customer("name2", "addr2","city2");

em.persist(cust2);
}
}

persistance.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="EJBPrototypesPU" transaction-type="JTA">
<jta-data-source>jdbc/dstestxa</jta-data-source>
<non-jta-data-source>jdbc/dstest</non-jta-data-source>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)"/>
<property name="openjpa.Sequence" value="table(Table=OPENJPASEQ,
Increment=1)"/>
<property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=TRACE,
Tool=INFO"/> </properties>
</persistence-unit>

</persistence>

Note: I am using Geronimo 3.0.1 which supports EJB3.1.



--
View this message in context: http://apache-geronimo.328035.n3.nabble.com/EJB-Transaction-inter-server-propagation-tp3988189.html
Sent from the Users mailing list archive at Nabble.com.
David Jencks
2014-08-13 15:32:26 UTC
Permalink
Hi,

Transaction propagation between geronimo server instances is not supported. If you use the cobra transport, then we send whether or not there is an active transaction at the client, so we can determine if the call at the server side is allowed (e.g. mandatory is never allowed, requires new is always allowed, requires is allowed only if there was no transaction on the client). I think that the default openejb remote protocol doesn't support even this level of interop.

Unfortunately implementing this is not all that easy. If you want to try to implement it I can try to give advice, but it was a problem I was not able to completely solve.

thanks
david jencks
Post by Darv
I am having some difficulty with transaction propagation between two EJBs on
seperate servers (which I understand can be done based on section 13.2.3 of
the EJB3.1 spec), and I would appreciate if some who might have done this,
could help me.
I am a bit green with EJBs so please excuse me. My initial thought was to
use RemoteInitialContextFactory to initiate the context and then lookup the
remote EJB. While the EJBs communicate ok, unfortunately the transaction
does not propagate, even if the 2 EJB's are on the same server (but
does propagate the transaction when on the same machine (diff projects).
The prototype code below is where MyFirstEjbBean saves a customer and then
calls MySecondEjbBean which save another customer on a seperate schema.
MyFirstEjbBean can then blow up to test the rollback. What annotations and
xml definitions are required to enable this to work inter servers? An
example would be greatly appreciated.
@Stateless
public class MyFirstEjbBean implements MyFirstEjb, MyFirstEjbRemote {
@PersistenceContext(name="EJBPrototypesPU")
private EntityManager em;
@EJB
MySecondEjbRemote mySecondEjb;
public void saveCustomers(){
Customer customer = new Customer("name1", "address1", "city1");
em.persist(customer);
mySecondEjb.saveCustomer();
// throw new IllegalStateException("Blowing up!");
}
}
@Stateless
public class MySecondEjbBean implements MySecondEjb, MySecondEjbRemote {
@PersistenceContext(name="EJBPrototypesPU")
private EntityManager em;
@TransactionAttribute(TransactionAttributeType.MANDATORY)
public void saveCustomer() {
// TODO Auto-generated method stub
Customer cust2 = new Customer("name2", "addr2","city2");
em.persist(cust2);
}
}
persistance.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="EJBPrototypesPU" transaction-type="JTA">
<jta-data-source>jdbc/dstestxa</jta-data-source>
<non-jta-data-source>jdbc/dstest</non-jta-data-source>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)"/>
<property name="openjpa.Sequence" value="table(Table=OPENJPASEQ,
Increment=1)"/>
<property name="openjpa.Log" value="DefaultLevel=WARN, Runtime=TRACE,
Tool=INFO"/> </properties>
</persistence-unit>
</persistence>
Note: I am using Geronimo 3.0.1 which supports EJB3.1.
--
View this message in context: http://apache-geronimo.328035.n3.nabble.com/EJB-Transaction-inter-server-propagation-tp3988189.html
Sent from the Users mailing list archive at Nabble.com.
Darv
2014-08-14 00:04:01 UTC
Permalink
Thx David. What annotations and xml is required in an EJB project to inject
another EJB which will run on another geronimo server without transaction
propagation? How is the location / ip address etc specified. Or do you need
to use the RemoteInitialContextFactory.



--
View this message in context: http://apache-geronimo.328035.n3.nabble.com/EJB-Transaction-inter-server-propagation-tp3988189p3988191.html
Sent from the Users mailing list archive at Nabble.com.
David Jencks
2014-08-14 01:00:04 UTC
Permalink
I'm afraid I don't remember if you can use injection for this. Asking at the tomee/openejb project might give you better info, although I'm not sure it will directly translate. If we made injection work, I hope you have to configure where the other server is outside the annotations since changing them when your other server moves isn't going to scale well.

david jencks
Post by Darv
Thx David. What annotations and xml is required in an EJB project to inject
another EJB which will run on another geronimo server without transaction
propagation? How is the location / ip address etc specified. Or do you need
to use the RemoteInitialContextFactory.
--
View this message in context: http://apache-geronimo.328035.n3.nabble.com/EJB-Transaction-inter-server-propagation-tp3988189p3988191.html
Sent from the Users mailing list archive at Nabble.com.
Loading...