Spring Boot + Apache Camel SQL component + Transaction Management Example
We will be modifying the code we had developed in previous tutorial for Apache Camel + Spring SQL Component + MySQL Example
Video
This tutorial is explained in the below Youtube Video.Apache Camel - Table of Contents
File Transfer Using Java DSL Apache Camel Apache Camel Java DSL + Spring Integration Hello World Example Apache Camel Exception Handling Using Simple Example Apache Camel Redelivery policy using example Integrate Apache Camel and ActiveMQ EIP patterns using Apache Camel Apache Camel Tutorial- Integrate Spring Boot+ Apache Camel Apache Camel Tutorial- Integrate with MySQL DB using SQL query Apache Camel EIP - Splitter and Aggregator pattern Apache Camel Unit Testing Apache Camel + Spring + Quartz Hello World Example Camel application deployment on JBoss Fuse Apache Camel + Apache CXF SOAP Webservices Apache Camel + JAX-RS REST Webservice Apache Camel + CXFRS REST Webservice Apache Camel Routing Slip EIP Pattern Apache Camel Dynamic Router Pattern Apache Camel Load Balancer EIP Pattern Apache Camel Interceptors Apache Camel + Kafka Hello World Example Apache Camel - Marshalling/Unmarshalling XML/JSON Data Example Calling and Consuming Webservices using Apache Camel Apache Camel Tutorial - Send SMTP Email Using Gmail Apache Camel Tutorial - SEDA component Hello World Example Apache Camel Tutorial - Idempotent Consumer using MemoryIdempotentRepository and FileIdempotentRepository Spring Boot + Apache Camel + RabbitMQ - Hello World Example Spring Boot + Apache Camel JDBC component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + MySQL - Hello World Example Spring Boot + Apache Camel SQL component + Transaction Management Example
Exception thrown in Camel route without Transaction Management
Initially we will be taking a look at the application behaviour without transaction Management. So we will be throwing an exception after the values are inserted in MySQL database. As no transaction management has been implemented so auto commit is true and the insert is not rolled back. We will modify the Service class where we have configured the camel route. In the insert route after inserting the data we will be throwing an exception.package com.javainuse.service.impl; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.sql.DataSource; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.sql.SqlComponent; import org.apache.camel.spring.spi.SpringTransactionPolicy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.stereotype.Service; import org.springframework.transaction.PlatformTransactionManager; import com.javainuse.model.Employee; @Service public class EmployeeServiceImpl extends RouteBuilder { @Autowired DataSource dataSource; public DataSource getDataSource() { return dataSource; } public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; } @Bean public SqlComponent sql(DataSource dataSource) { SqlComponent sql = new SqlComponent(); sql.setDataSource(dataSource); return sql; } @Override public void configure() throws Exception { //Insert Route from("direct:insert").log("Processing message: ").setHeader("message", body()).process(new Processor() { public void process(Exchange xchg) throws Exception { //take the Employee object from the exchange and create the parameter map Employee employee = xchg.getIn().getBody(Employee.class); Map<String, Object> employeeMap = new HashMap<String, Object>(); employeeMap.put("EmpId", employee.getEmpId()); employeeMap.put("EmpName", employee.getEmpName()); xchg.getIn().setBody(employeeMap); } }).to("sql:INSERT INTO employee(EmpId, EmpName) VALUES (:#EmpId, :#EmpName)"). process(new Processor() { public void process(Exchange xchg) throws Exception { // throw an exception after insert statement. System.out.println("Exception Occurred"); throw new Exception(); } }); // Select Route from("direct:select").to("sql:select * from employee").process(new Processor() { public void process(Exchange xchg) throws Exception { //the camel sql select query has been executed. We get the list of employees. ArrayList<Map<String, String>> dataList = (ArrayList<Map<String, String>>) xchg.getIn().getBody(); List<Employee> employees = new ArrayList<Employee>(); System.out.println(dataList); for (Map<String, String> data : dataList) { Employee employee = new Employee(); employee.setEmpId(data.get("empId")); employee.setEmpName(data.get("empName")); employees.add(employee); } xchg.getIn().setBody(employees); } }); } }Start the application.
Send a POST request to insert new employee detail-
This will throw an exception. However we observe that the insertion in employee table is not reverted. So it is auto commit by default.