Coordination between spring JtaTransactionManager and Aries JPA

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Coordination between spring JtaTransactionManager and Aries JPA

Christian

Hi,

I have a server application running in karaf and I have problems to combine transactions defined in spring/camel and code having @Transactional annotation.

In my blueprint.xml I have defined something like this:

   <reference id="jtaTransactionManager" interface="javax.transaction.TransactionManager" />
   <reference id="userTransaction" interface="javax.transaction.UserTransaction" />

   <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
       <argument ref="jtaTransactionManager" />
       <argument ref="userTransaction" />
   </bean>

   <bean id="PROPAGATION_REQUIRES_NEW" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
       <property name="transactionManager" ref="transactionManager" />
       <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW" />
   </bean>

So I start a new transaction by using PROPAGATION_REQUIRES_NEW in a camel route. The camel route leads to a WebService endpoint. Anywhere in the underlying code where I use @Transactional and get a EntityManager injected I can see that the transaction is handled by a coordination from aries-jpa and gets committed by the uppermost @Transactional annotation.
But I want that the underlying code uses the transaction created by the PROPAGATION_REQUIRES_NEW in the camel route so I am maybe able to rollback the transaction inside the camel route. How to achieve this? Do I have to use a TransactionManager from aries-jpa instead?

Thanks in advance,
Christian

Reply | Threaded
Open this post in threaded view
|

Re: Coordination between spring JtaTransactionManager and Aries JPA

Christian Schneider
I also had the problem of combining jpa and camel transactions a while ago and created an example and documentation for XA setup.
See:
https://github.com/Talend/tesb-rt-se/blob/master/examples/tesb/ebook/xa-docs.md

Camel route:

The example reads from a xml file, parses each record into a jaxb bean and sends the serialized bean to jms.
Then it reads from jms into a bean again and writes into a database using jpa. This second flow coordinates a camel jms transaction and a jpa db transaction using jta.

You can find the necessary jta setup in the camel context:

and the connection factory definition:

I think what is not mentioned in the example is that you have to setup an xa ready datasource (at least I did not find it) using pax-jdbc.
See

Cheers
Christian


Am Mi., 5. Dez. 2018 um 15:43 Uhr schrieb Niehues, Christian <[hidden email]>:

Hi,

I have a server application running in karaf and I have problems to combine transactions defined in spring/camel and code having @Transactional annotation.

In my blueprint.xml I have defined something like this:

   <reference id="jtaTransactionManager" interface="javax.transaction.TransactionManager" />
   <reference id="userTransaction" interface="javax.transaction.UserTransaction" />

   <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
       <argument ref="jtaTransactionManager" />
       <argument ref="userTransaction" />
   </bean>

   <bean id="PROPAGATION_REQUIRES_NEW" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
       <property name="transactionManager" ref="transactionManager" />
       <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW" />
   </bean>

So I start a new transaction by using PROPAGATION_REQUIRES_NEW in a camel route. The camel route leads to a WebService endpoint. Anywhere in the underlying code where I use @Transactional and get a EntityManager injected I can see that the transaction is handled by a coordination from aries-jpa and gets committed by the uppermost @Transactional annotation.
But I want that the underlying code uses the transaction created by the PROPAGATION_REQUIRES_NEW in the camel route so I am maybe able to rollback the transaction inside the camel route. How to achieve this? Do I have to use a TransactionManager from aries-jpa instead?

Thanks in advance,
Christian



--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist

Reply | Threaded
Open this post in threaded view
|

AW: Coordination between spring JtaTransactionManager and Aries JPA

Christian

Hi Christian,


Thanks for the fast and detailed answer.


I think I already used a XADatasource because of the properties set in my datasource configuration:

pool=dbcp2
xa=true

Question about that: I saw you can also use pool=aries but when I try to use it I get no EntityManager. So do I have to change additional things?


I think my problem was that I didn't use a pooled connectionfactory. Since I use XaPooledConnectionFactory like in your example it seems to work.

I only still see some difference when using seda instead of direct in camel routes, so there seems to be some issue when using different threads.

Also I need to check if it works when using cluster setup.


Thanks

Christian



Von: Christian Schneider <[hidden email]>
Gesendet: Mittwoch, 5. Dezember 2018 16:30:21
An: [hidden email]
Betreff: Re: Coordination between spring JtaTransactionManager and Aries JPA
 
I also had the problem of combining jpa and camel transactions a while ago and created an example and documentation for XA setup.
See:
https://github.com/Talend/tesb-rt-se/blob/master/examples/tesb/ebook/xa-docs.md

Camel route:

The example reads from a xml file, parses each record into a jaxb bean and sends the serialized bean to jms.
Then it reads from jms into a bean again and writes into a database using jpa. This second flow coordinates a camel jms transaction and a jpa db transaction using jta.

You can find the necessary jta setup in the camel context:

and the connection factory definition:

I think what is not mentioned in the example is that you have to setup an xa ready datasource (at least I did not find it) using pax-jdbc.
See
<a href="https://ops4j1.jira.com/wiki/spaces/PAXJDBC/pages/119078914/Pooling&#43;and&#43;XA&#43;support&#43;in&#43;1.0.0" id="LPlnk302976" previewremoved="true">https://ops4j1.jira.com/wiki/spaces/PAXJDBC/pages/119078914/Pooling+and+XA+support+in+1.0.0
<a href="https://ops4j1.jira.com/wiki/spaces/PAXJDBC/pages/61767716/Pooling&#43;and&#43;XA&#43;support&#43;for&#43;DataSourceFactory" id="LPlnk706759" previewremoved="true">https://ops4j1.jira.com/wiki/spaces/PAXJDBC/pages/61767716/Pooling+and+XA+support+for+DataSourceFactory

Cheers
Christian


Am Mi., 5. Dez. 2018 um 15:43 Uhr schrieb Niehues, Christian <[hidden email]>:

Hi,

I have a server application running in karaf and I have problems to combine transactions defined in spring/camel and code having @Transactional annotation.

In my blueprint.xml I have defined something like this:

   <reference id="jtaTransactionManager" interface="javax.transaction.TransactionManager" />
   <reference id="userTransaction" interface="javax.transaction.UserTransaction" />

   <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
       <argument ref="jtaTransactionManager" />
       <argument ref="userTransaction" />
   </bean>

   <bean id="PROPAGATION_REQUIRES_NEW" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
       <property name="transactionManager" ref="transactionManager" />
       <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW" />
   </bean>

So I start a new transaction by using PROPAGATION_REQUIRES_NEW in a camel route. The camel route leads to a WebService endpoint. Anywhere in the underlying code where I use @Transactional and get a EntityManager injected I can see that the transaction is handled by a coordination from aries-jpa and gets committed by the uppermost @Transactional annotation.
But I want that the underlying code uses the transaction created by the PROPAGATION_REQUIRES_NEW in the camel route so I am maybe able to rollback the transaction inside the camel route. How to achieve this? Do I have to use a TransactionManager from aries-jpa instead?

Thanks in advance,
Christian



--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist

Reply | Threaded
Open this post in threaded view
|

Re: Coordination between spring JtaTransactionManager and Aries JPA

Christian Schneider
> I only still see some difference when using seda instead of direct in camel routes, so there seems to be some issue when using different threads.
Yes. A transaction by default only works on the same thread.

Christian


Am Do., 6. Dez. 2018 um 14:46 Uhr schrieb Niehues, Christian <[hidden email]>:

Hi Christian,


Thanks for the fast and detailed answer.


I think I already used a XADatasource because of the properties set in my datasource configuration:

pool=dbcp2
xa=true

Question about that: I saw you can also use pool=aries but when I try to use it I get no EntityManager. So do I have to change additional things?


I think my problem was that I didn't use a pooled connectionfactory. Since I use XaPooledConnectionFactory like in your example it seems to work.

I only still see some difference when using seda instead of direct in camel routes, so there seems to be some issue when using different threads.

Also I need to check if it works when using cluster setup.


Thanks

Christian



Von: Christian Schneider <[hidden email]>
Gesendet: Mittwoch, 5. Dezember 2018 16:30:21
An: [hidden email]
Betreff: Re: Coordination between spring JtaTransactionManager and Aries JPA
 
I also had the problem of combining jpa and camel transactions a while ago and created an example and documentation for XA setup.
See:
https://github.com/Talend/tesb-rt-se/blob/master/examples/tesb/ebook/xa-docs.md

Camel route:

The example reads from a xml file, parses each record into a jaxb bean and sends the serialized bean to jms.
Then it reads from jms into a bean again and writes into a database using jpa. This second flow coordinates a camel jms transaction and a jpa db transaction using jta.

You can find the necessary jta setup in the camel context:

and the connection factory definition:

I think what is not mentioned in the example is that you have to setup an xa ready datasource (at least I did not find it) using pax-jdbc.
See

Cheers
Christian


Am Mi., 5. Dez. 2018 um 15:43 Uhr schrieb Niehues, Christian <[hidden email]>:

Hi,

I have a server application running in karaf and I have problems to combine transactions defined in spring/camel and code having @Transactional annotation.

In my blueprint.xml I have defined something like this:

   <reference id="jtaTransactionManager" interface="javax.transaction.TransactionManager" />
   <reference id="userTransaction" interface="javax.transaction.UserTransaction" />

   <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
       <argument ref="jtaTransactionManager" />
       <argument ref="userTransaction" />
   </bean>

   <bean id="PROPAGATION_REQUIRES_NEW" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
       <property name="transactionManager" ref="transactionManager" />
       <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW" />
   </bean>

So I start a new transaction by using PROPAGATION_REQUIRES_NEW in a camel route. The camel route leads to a WebService endpoint. Anywhere in the underlying code where I use @Transactional and get a EntityManager injected I can see that the transaction is handled by a coordination from aries-jpa and gets committed by the uppermost @Transactional annotation.
But I want that the underlying code uses the transaction created by the PROPAGATION_REQUIRES_NEW in the camel route so I am maybe able to rollback the transaction inside the camel route. How to achieve this? Do I have to use a TransactionManager from aries-jpa instead?

Thanks in advance,
Christian



--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist



--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist

Reply | Threaded
Open this post in threaded view
|

AW: Coordination between spring JtaTransactionManager and Aries JPA

Christian

And what would a transaction solution look like if the work is spread over different nodes in a cluster?


Christian


Von: Christian Schneider <[hidden email]>
Gesendet: Donnerstag, 6. Dezember 2018 17:07:48
An: [hidden email]
Betreff: Re: Coordination between spring JtaTransactionManager and Aries JPA
 
> I only still see some difference when using seda instead of direct in camel routes, so there seems to be some issue when using different threads.
Yes. A transaction by default only works on the same thread.

Christian


Am Do., 6. Dez. 2018 um 14:46 Uhr schrieb Niehues, Christian <[hidden email]>:

Hi Christian,


Thanks for the fast and detailed answer.


I think I already used a XADatasource because of the properties set in my datasource configuration:

pool=dbcp2
xa=true

Question about that: I saw you can also use pool=aries but when I try to use it I get no EntityManager. So do I have to change additional things?


I think my problem was that I didn't use a pooled connectionfactory. Since I use XaPooledConnectionFactory like in your example it seems to work.

I only still see some difference when using seda instead of direct in camel routes, so there seems to be some issue when using different threads.

Also I need to check if it works when using cluster setup.


Thanks

Christian



Von: Christian Schneider <[hidden email]>
Gesendet: Mittwoch, 5. Dezember 2018 16:30:21
An: [hidden email]
Betreff: Re: Coordination between spring JtaTransactionManager and Aries JPA
 
I also had the problem of combining jpa and camel transactions a while ago and created an example and documentation for XA setup.
See:
https://github.com/Talend/tesb-rt-se/blob/master/examples/tesb/ebook/xa-docs.md

Camel route:

The example reads from a xml file, parses each record into a jaxb bean and sends the serialized bean to jms.
Then it reads from jms into a bean again and writes into a database using jpa. This second flow coordinates a camel jms transaction and a jpa db transaction using jta.

You can find the necessary jta setup in the camel context:

and the connection factory definition:

I think what is not mentioned in the example is that you have to setup an xa ready datasource (at least I did not find it) using pax-jdbc.
See
<a href="https://ops4j1.jira.com/wiki/spaces/PAXJDBC/pages/119078914/Pooling&#43;and&#43;XA&#43;support&#43;in&#43;1.0.0" id="m_8192759023574885756LPlnk302976" target="_blank">https://ops4j1.jira.com/wiki/spaces/PAXJDBC/pages/119078914/Pooling+and+XA+support+in+1.0.0
<a href="https://ops4j1.jira.com/wiki/spaces/PAXJDBC/pages/61767716/Pooling&#43;and&#43;XA&#43;support&#43;for&#43;DataSourceFactory" id="m_8192759023574885756LPlnk706759" target="_blank">https://ops4j1.jira.com/wiki/spaces/PAXJDBC/pages/61767716/Pooling+and+XA+support+for+DataSourceFactory

Cheers
Christian


Am Mi., 5. Dez. 2018 um 15:43 Uhr schrieb Niehues, Christian <[hidden email]>:

Hi,

I have a server application running in karaf and I have problems to combine transactions defined in spring/camel and code having @Transactional annotation.

In my blueprint.xml I have defined something like this:

   <reference id="jtaTransactionManager" interface="javax.transaction.TransactionManager" />
   <reference id="userTransaction" interface="javax.transaction.UserTransaction" />

   <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
       <argument ref="jtaTransactionManager" />
       <argument ref="userTransaction" />
   </bean>

   <bean id="PROPAGATION_REQUIRES_NEW" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
       <property name="transactionManager" ref="transactionManager" />
       <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW" />
   </bean>

So I start a new transaction by using PROPAGATION_REQUIRES_NEW in a camel route. The camel route leads to a WebService endpoint. Anywhere in the underlying code where I use @Transactional and get a EntityManager injected I can see that the transaction is handled by a coordination from aries-jpa and gets committed by the uppermost @Transactional annotation.
But I want that the underlying code uses the transaction created by the PROPAGATION_REQUIRES_NEW in the camel route so I am maybe able to rollback the transaction inside the camel route. How to achieve this? Do I have to use a TransactionManager from aries-jpa instead?

Thanks in advance,
Christian



--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist



--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist

Reply | Threaded
Open this post in threaded view
|

Re: Coordination between spring JtaTransactionManager and Aries JPA

Christian Schneider
I never tested doing distributed transactions but in literature the common sense is that it sucks.

Christian

Am Di., 11. Dez. 2018 um 13:54 Uhr schrieb Niehues, Christian <[hidden email]>:

And what would a transaction solution look like if the work is spread over different nodes in a cluster?


Christian


Von: Christian Schneider <[hidden email]>
Gesendet: Donnerstag, 6. Dezember 2018 17:07:48
An: [hidden email]
Betreff: Re: Coordination between spring JtaTransactionManager and Aries JPA
 
> I only still see some difference when using seda instead of direct in camel routes, so there seems to be some issue when using different threads.
Yes. A transaction by default only works on the same thread.

Christian


Am Do., 6. Dez. 2018 um 14:46 Uhr schrieb Niehues, Christian <[hidden email]>:

Hi Christian,


Thanks for the fast and detailed answer.


I think I already used a XADatasource because of the properties set in my datasource configuration:

pool=dbcp2
xa=true

Question about that: I saw you can also use pool=aries but when I try to use it I get no EntityManager. So do I have to change additional things?


I think my problem was that I didn't use a pooled connectionfactory. Since I use XaPooledConnectionFactory like in your example it seems to work.

I only still see some difference when using seda instead of direct in camel routes, so there seems to be some issue when using different threads.

Also I need to check if it works when using cluster setup.


Thanks

Christian



Von: Christian Schneider <[hidden email]>
Gesendet: Mittwoch, 5. Dezember 2018 16:30:21
An: [hidden email]
Betreff: Re: Coordination between spring JtaTransactionManager and Aries JPA
 
I also had the problem of combining jpa and camel transactions a while ago and created an example and documentation for XA setup.
See:
https://github.com/Talend/tesb-rt-se/blob/master/examples/tesb/ebook/xa-docs.md

Camel route:

The example reads from a xml file, parses each record into a jaxb bean and sends the serialized bean to jms.
Then it reads from jms into a bean again and writes into a database using jpa. This second flow coordinates a camel jms transaction and a jpa db transaction using jta.

You can find the necessary jta setup in the camel context:

and the connection factory definition:

I think what is not mentioned in the example is that you have to setup an xa ready datasource (at least I did not find it) using pax-jdbc.
See

Cheers
Christian


Am Mi., 5. Dez. 2018 um 15:43 Uhr schrieb Niehues, Christian <[hidden email]>:

Hi,

I have a server application running in karaf and I have problems to combine transactions defined in spring/camel and code having @Transactional annotation.

In my blueprint.xml I have defined something like this:

   <reference id="jtaTransactionManager" interface="javax.transaction.TransactionManager" />
   <reference id="userTransaction" interface="javax.transaction.UserTransaction" />

   <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
       <argument ref="jtaTransactionManager" />
       <argument ref="userTransaction" />
   </bean>

   <bean id="PROPAGATION_REQUIRES_NEW" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
       <property name="transactionManager" ref="transactionManager" />
       <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW" />
   </bean>

So I start a new transaction by using PROPAGATION_REQUIRES_NEW in a camel route. The camel route leads to a WebService endpoint. Anywhere in the underlying code where I use @Transactional and get a EntityManager injected I can see that the transaction is handled by a coordination from aries-jpa and gets committed by the uppermost @Transactional annotation.
But I want that the underlying code uses the transaction created by the PROPAGATION_REQUIRES_NEW in the camel route so I am maybe able to rollback the transaction inside the camel route. How to achieve this? Do I have to use a TransactionManager from aries-jpa instead?

Thanks in advance,
Christian



--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist



--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist



--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist

Reply | Threaded
Open this post in threaded view
|

Re: Coordination between spring JtaTransactionManager and Aries JPA

David Jencks-3
I’m not quite sure what the underlying transaction manager is used here, but I doubt it supports distributed transactions.   The Geronimo transaction manager doesn’t, and although I had some interest in figuring out how to do it, over the course of 10 or so years, that interest never extended to even the start of an implementation.  If you think you need a distributed transaction, find another solution, it will work better and be faster.

Letting a transaction linger over several SEDA stages is likely to have the same kinds of problems as a distributed transaction, so even though it’s possible to suspend/resume transactions across threads, it’s a pretty bad idea.  If you need a transaction, confine it to one SEDA stage.

David Jencks

On Dec 11, 2018, at 11:36 AM, Christian Schneider <[hidden email]> wrote:

I never tested doing distributed transactions but in literature the common sense is that it sucks.

Christian

Am Di., 11. Dez. 2018 um 13:54 Uhr schrieb Niehues, Christian <[hidden email]>:

And what would a transaction solution look like if the work is spread over different nodes in a cluster?


Christian



Von: Christian Schneider <[hidden email]>
Gesendet: Donnerstag, 6. Dezember 2018 17:07:48
An: [hidden email]
Betreff: Re: Coordination between spring JtaTransactionManager and Aries JPA
 
> I only still see some difference when using seda instead of direct in camel routes, so there seems to be some issue when using different threads.
Yes. A transaction by default only works on the same thread.

Christian


Am Do., 6. Dez. 2018 um 14:46 Uhr schrieb Niehues, Christian <[hidden email]>:

Hi Christian,


Thanks for the fast and detailed answer.


I think I already used a XADatasource because of the properties set in my datasource configuration:


pool=dbcp2
xa=true


Question about that: I saw you can also use pool=aries but when I try to use it I get no EntityManager. So do I have to change additional things?


I think my problem was that I didn't use a pooled connectionfactory. Since I use XaPooledConnectionFactory like in your example it seems to work.

I only still see some difference when using seda instead of direct in camel routes, so there seems to be some issue when using different threads.

Also I need to check if it works when using cluster setup.


Thanks

Christian




Von: Christian Schneider <[hidden email]>
Gesendet: Mittwoch, 5. Dezember 2018 16:30:21
An: [hidden email]
Betreff: Re: Coordination between spring JtaTransactionManager and Aries JPA
 
I also had the problem of combining jpa and camel transactions a while ago and created an example and documentation for XA setup.
See:
https://github.com/Talend/tesb-rt-se/blob/master/examples/tesb/ebook/xa-docs.md

Camel route:

The example reads from a xml file, parses each record into a jaxb bean and sends the serialized bean to jms.
Then it reads from jms into a bean again and writes into a database using jpa. This second flow coordinates a camel jms transaction and a jpa db transaction using jta.

You can find the necessary jta setup in the camel context:

and the connection factory definition:

I think what is not mentioned in the example is that you have to setup an xa ready datasource (at least I did not find it) using pax-jdbc.
See

Cheers
Christian


Am Mi., 5. Dez. 2018 um 15:43 Uhr schrieb Niehues, Christian <[hidden email]>:

Hi,

I have a server application running in karaf and I have problems to combine transactions defined in spring/camel and code having @Transactional annotation.

In my blueprint.xml I have defined something like this:

   <reference id="jtaTransactionManager" interface="javax.transaction.TransactionManager" />
   <reference id="userTransaction" interface="javax.transaction.UserTransaction" />

   <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
       <argument ref="jtaTransactionManager" />
       <argument ref="userTransaction" />
   </bean>

   <bean id="PROPAGATION_REQUIRES_NEW" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
       <property name="transactionManager" ref="transactionManager" />
       <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW" />
   </bean>

So I start a new transaction by using PROPAGATION_REQUIRES_NEW in a camel route. The camel route leads to a WebService endpoint. Anywhere in the underlying code where I use @Transactional and get a EntityManager injected I can see that the transaction is handled by a coordination from aries-jpa and gets committed by the uppermost @Transactional annotation.
But I want that the underlying code uses the transaction created by the PROPAGATION_REQUIRES_NEW in the camel route so I am maybe able to rollback the transaction inside the camel route. How to achieve this? Do I have to use a TransactionManager from aries-jpa instead?

Thanks in advance,
Christian




--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist



--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist



--
--
Christian Schneider

http://www.liquid-reality.de

Computer Scientist