...
Outbound endpoints will automatically enlist in the current transaction context. But what if you do not want your outbound endpoint to enlist in the same transaction as your inbound endpoint? The solution is to add a Transaction Policy to the processing route. You first have to define transaction policies that you will be using. The policies use a spring TransactionTemplate to declare under the covers for declaring the transaction demarcation to use. So you will need to add something like the following to your spring xml:
Code Block | ||||
---|---|---|---|---|
| ||||
<bean id="PROPAGATION_REQUIRED" class="org.springframeworkapache.transactioncamel.support.TransactionTemplate"> <property name="transactionManager" ref="jmsTransactionManager"/> </bean> <bean id="PROPAGATION_NOT_SUPPORTED" class="org.springframework.transaction.support.TransactionTemplatespring.spi.SpringTransactionPolicy"> <property name="transactionManager" ref="jmsTransactionManager"/> <property name="propagationBehaviorName" value="PROPAGATION_NOT_SUPPORTED"/> </bean> <bean id="PROPAGATION_REQUIRES_NEW" class="org.apache.springframeworkcamel.transactionspring.supportspi.TransactionTemplateSpringTransactionPolicy"> <property name="transactionManager" ref="jmsTransactionManager"/> <property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/> </bean> |
...
Code Block | ||||
---|---|---|---|---|
| ||||
public void configure() { ... Policy requried = new SpringTransactionPolicy(bean(TransactionTemplateSpringTransactionPolicy.class, "PROPAGATION_REQUIRED")); Policy notsupportedrequirenew = new SpringTransactionPolicy(bean(TransactionTemplate.class, "PROPAGATION_NOT_SUPPORTED")); Policy requirenew = new SpringTransactionPolicy(bean(TransactionTemplate.class, "PROPAGATION_REQUIRES_NEW")); ... } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
// Send to bar in a new transaction
from("activemq:queue:foo").policy(requirenew).to("activemq:queue:bar");
// Send to bar without a transaction.
from("activemq:queue:foo").policy(notsupported ).to("activemq:queue:bar");
|
Transaction Policies improvements in Camel 1.4
In Camel 1.4 we have eased the syntax to setup the transaction polices directly on the SpringTransactionPolicy
object:
...
<bean id="PROPAGATION_REQUIRED" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
<property name="transactionManager" ref="jmsTransactionManager"/>
</bean>
<bean id="PROPAGATION_REQUIRES_NEW" class="org.apache.camel.spring.spi.SpringTransactionPolicy">
<property name="transactionManager" ref="jmsTransactionManager"/>
<property name="propagationBehaviorName" value="PROPAGATION_REQUIRES_NEW"/>
</bean>
And the Java DSL is a bit simpler now:
Code Block | ||
---|---|---|
java | java |
Policy requried = bean(SpringTransactionPolicy.class, "PROPAGATION_REQUIRED"));
|
Database Sample
In this sample we want to ensure that two endpoints is under transaction control. These two endpoints inserts data into a database.
The sample is in its full as a unit test.
...