Rolling back a transaction
So far we have seen transaction success or failure determined naturally—that is, by whether an exchange was successfully processed, or if an exception was thrown during its processing. This recipe will provide you with a number of additional options around controlling transaction rollback.
Getting ready
The Java code for this recipe is located in the org.camelcookbook.transactions.rollback
package. The Spring XML files are located under src/main/resources/META-INF/spring
and prefixed with rollback
.
How to do it...
You can use the rollback
DSL statement to throw an exception with an optional message.
In the XML DSL, we use the following code:
<from uri="direct:transacted"/>
<transacted/>
<setHeader headerName="message">
<simple>${body}</simple>
</setHeader>
<to uri="sql:insert into audit_log (message) values (:#message)"/>
<choice>
<when>
<simple>${body} contains 'explode'</simple>
<rollback...