Arjun
Arjun

Reputation: 31

Apache Camel: Multiple Transactions within a Route

I have a need to create multiple transaction scopes within a Route. Something like below.

<route>
  <from uri="activemq:queue:foo"/>
  <transacted ref="required"/>
  <to uri="activemq:queue:bar"/>
  <to uri="bean:database1?method=insert(*,*)"/>
  <transacted ref="requiresNew"/>
  <to uri="bean:database2?method=insert(*,*)"/>
</route>

If I create above route, I get below error.

ERROR 2016-09-13 14:32:32 servlet.FrameworkServlet - Context initialization failed org.apache.camel.RuntimeCamelException: java.lang.IllegalArgumentException: The output must be added as top-level on the route. Try moving Transacted[ref:requiresNew] to the top of route. at org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException(ObjectHelper.java:1642)

Looked up the documentation and forums and looks like only one transaction scope is allowed within a Route. Am looking for a solution where transaction can be controlled from the Route and multiple transaction scopes can be created.

Is this currently allowed?

Upvotes: 2

Views: 2035

Answers (1)

Miloš Milivojević
Miloš Milivojević

Reputation: 5369

It's true that you can only have one transaction policy per route, but you can also separate the parts of your route that require a different policy into another route (also have a look at the official documentation for more details), e.g.

<route>
  <from uri="activemq:queue:foo"/>
  <transacted ref="required"/>
  <to uri="activemq:queue:bar"/>
  <to uri="bean:database1?method=insert(*,*)"/>
  <to uri="direct:requiresNew"/>
</route>

<route>
  <from uri="direct:requiresNew"/>
  <transacted ref="requiresNew"/>
  <to uri="bean:database2?method=insert(*,*)"/>
</route>

Upvotes: 3

Related Questions