You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by "Iglehart, Vince" <Vi...@kniferiver.com> on 2018/07/19 23:07:09 UTC

split(), shareUnitOfWork & transacted()

I have a question on propagating a transaction with split() and shareUnitOfWork(). I have a route defined below in Camel 2.20.2.  I read transactions from Datasource1, I split the result set, send each row to a recipient list which uses Datasource2, and then mark the row as processed.  I perform a select for update when reading the transactions from Datasource1.

from("seda:process")
.transacted("TRANS_REQUIRESNEW")

                // Select all transactions to process
                .to("sql:classpath:sql/SelForUpdate.sql?dataSource=DataSource1")
                .split(body())
                                .shareUnitOfWork()
                                .setHeader("transactionId", simple("${body.transactionId}"))

                                // Datasource2 recipients
                                .recipientList().method(Routing.class).shareUnitOfWork().end()

                                // Update dateProcessed.
                                .to("sql:classpath:sql/UpdateDateProcessed.sql?dataSource=DataSource1");

I want the "select for update" and the "update" to take place in the same transaction.  Through the use of the transacted() and shareUnitOfWork() calls, I would expect that select and update would be using the same connection to Oracle.  However, it appears that there are two connections being used - 1 for the select for update and 1 for the update.  Upon running the update inside of the split(), the route waits for the release from the select for update.

Do I have the route and Spring context config correct?

The Spring context file has the following configuration for the datasources.


<jee:jndi-lookup expected-type="javax.sql.DataSource" id="Datasource1" jndi-name="jdbc/Datasource1"/>
<jee:jndi-lookup expected-type="javax.sql.DataSource" id="Datasource2" jndi-name="jdbc/Datasource2"/>

<bean id="datasource1TxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="Datasource1" />
</bean>

<bean id="datasource2TxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="Datasource2" />
</bean>

<bean id="TRANS_REQUIRESNEW" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
                  <property name="transactionManager">
                  <bean id="txMgrRouting" class="org.springframework.data.transaction.ChainedTransactionManager">
                                  <constructor-arg>
                                    <list>
                                                <ref bean="datasource1TxManager"/>
                                                <ref bean="datasource2TxManager"/>
                                    </list>
                                </constructor-arg>
                </bean>
                </property>
                <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/>
</bean>


Thanks



Re: split(), shareUnitOfWork & transacted()

Posted by David Karlsen <da...@gmail.com>.
They won't share transactions as these are backed by spring and kept as
threadlocals

Den fre. 20. jul. 2018, 01:07 skrev Iglehart, Vince <
Vince.Iglehart@kniferiver.com>:

> I have a question on propagating a transaction with split() and
> shareUnitOfWork(). I have a route defined below in Camel 2.20.2.  I read
> transactions from Datasource1, I split the result set, send each row to a
> recipient list which uses Datasource2, and then mark the row as processed.
> I perform a select for update when reading the transactions from
> Datasource1.
>
> from("seda:process")
> .transacted("TRANS_REQUIRESNEW")
>
>                 // Select all transactions to process
>
> .to("sql:classpath:sql/SelForUpdate.sql?dataSource=DataSource1")
>                 .split(body())
>                                 .shareUnitOfWork()
>                                 .setHeader("transactionId",
> simple("${body.transactionId}"))
>
>                                 // Datasource2 recipients
>
> .recipientList().method(Routing.class).shareUnitOfWork().end()
>
>                                 // Update dateProcessed.
>
> .to("sql:classpath:sql/UpdateDateProcessed.sql?dataSource=DataSource1");
>
> I want the "select for update" and the "update" to take place in the same
> transaction.  Through the use of the transacted() and shareUnitOfWork()
> calls, I would expect that select and update would be using the same
> connection to Oracle.  However, it appears that there are two connections
> being used - 1 for the select for update and 1 for the update.  Upon
> running the update inside of the split(), the route waits for the release
> from the select for update.
>
> Do I have the route and Spring context config correct?
>
> The Spring context file has the following configuration for the
> datasources.
>
>
> <jee:jndi-lookup expected-type="javax.sql.DataSource" id="Datasource1"
> jndi-name="jdbc/Datasource1"/>
> <jee:jndi-lookup expected-type="javax.sql.DataSource" id="Datasource2"
> jndi-name="jdbc/Datasource2"/>
>
> <bean id="datasource1TxManager"
> class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
>                 <property name="dataSource" ref="Datasource1" />
> </bean>
>
> <bean id="datasource2TxManager"
> class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
>                 <property name="dataSource" ref="Datasource2" />
> </bean>
>
> <bean id="TRANS_REQUIRESNEW"
> class="org.apache.camel.spring.spi.SpringTransactionPolicy">
>                   <property name="transactionManager">
>                   <bean id="txMgrRouting"
> class="org.springframework.data.transaction.ChainedTransactionManager">
>                                   <constructor-arg>
>                                     <list>
>                                                 <ref
> bean="datasource1TxManager"/>
>                                                 <ref
> bean="datasource2TxManager"/>
>                                     </list>
>                                 </constructor-arg>
>                 </bean>
>                 </property>
>                 <property name="propagationBehaviorName"
> value="PROPAGATION_REQUIRES_NEW"/>
> </bean>
>
>
> Thanks
>
>
>