public int createEmployee(String name, int age, Long salary) { String insert_sql = "INSERT INTO EMPLOYEE ('name', 'age', 'salary') VALUES(?,?,?)"; JdbcTemplate jt = getJdbcTemplate() ; Object[] params = new Object[]{name, age, salary}; int ret = jt.update(insert_sql, params) ; throw new RuntimeException("simulate Error condition') ; return ret ; }
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="txManager"/>Step 2: Turn on support for transaction annotations
<tx:annotation-driven transaction-manager="txManager"/>Step 3: Add the @Transactional annotation to the createEmployee Method
@Transactional public int createEmployee(Employee employee) { ...Conceptually, calling a method on a transactional proxy looks like this...
CREATE TABLE Employee( EMPID INT NOT NULL AUTO_INCREMENT, NAME VARCHAR(26) NOT NULL, AGE INT NOT NULL, SALARY BIGINT NOT NULL, PRIMARY KEY (EMPID) );1. Create a project with a name SpringTMDemo and create a package com.dineshonjava.sdnext under the src folder in the created project.
package com.dineshonjava.sdnext.dao; import java.util.List; import com.dineshonjava.sdnext.domain.Employee; /** * @author Dinesh Rajput * */ public interface EmpDao { /** * This is the method to be used to create * a record in the Employee table. */ void create(String name, Integer age, Long salary); /** * This is the method to be used to list down * a record from the Employee table corresponding * to a passed Employee id. */ Employee getEmployee(Integer empid); /** * This is the method to be used to list down * all the records from the Employee table. */ List listEmployees(); /** * This is the method to be used to delete * a record from the Employee table corresponding * to a passed Employee id. */ void delete(Integer empid); /** * This is the method to be used to update * a record into the Employee table. */ void update(Integer empid, Integer age); }EmployeeDaoImpl.java
package com.dineshonjava.sdnext.dao.impl; import java.util.List; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.support.JdbcDaoSupport; import org.springframework.stereotype.Component; import com.dineshonjava.sdnext.dao.EmpDao; import com.dineshonjava.sdnext.domain.Employee; import com.dineshonjava.sdnext.jdbc.utils.EmployeeMapper; /** * @author Dinesh Rajput * */ @Component public class EmployeeDaoImpl extends JdbcDaoSupport implements EmpDao { // @Autowired // private JdbcTemplate jdbcTemplateObject; // // /** // * @param jdbcTemplateObject the jdbcTemplateObject to set // */ // public void setJdbcTemplateObject(JdbcTemplate jdbcTemplateObject) { // this.jdbcTemplateObject = jdbcTemplateObject; // } @Override public void create(String name, Integer age, Long salary) { try { String SQL = "INSERT INTO Employee (name, age, salary) VALUES (?, ?, ?)"; getJdbcTemplate().update(SQL, new Object[]{name, age, salary} ); System.out.println("Created Record Name = " + name + " Age = " + age+ " Salary = " + salary); // to simulate the exception. throw new RuntimeException("simulate Error condition") ; } catch (DataAccessException e) { System.out.println("Error in creating record, rolling back"); throw e; } } @Override public Employee getEmployee(Integer empid) { String SQL = "SELECT * FROM Employee WHERE empid = ?"; Employee employee = (Employee) getJdbcTemplate().queryForObject(SQL, new Object[]{empid}, new EmployeeMapper()); return employee; } @Override public List listEmployees() { String SQL = "SELECT * FROM Employee"; List employees = (List) getJdbcTemplate().query(SQL, new EmployeeMapper()); return null; } @Override public void delete(Integer empid) { String SQL = "DELETE FROM Employee WHERE empid = ?"; getJdbcTemplate().update(SQL, new Object[]{empid}); System.out.println("Deleted Record with EMPID = " + empid ); } @Override public void update(Integer empid, Integer age) { String SQL = "UPDATE Employee SET age = ? WHERE empid = ?"; getJdbcTemplate().update(SQL, new Object[]{age, empid}); System.out.println("Updated Record with EMPID = " + empid ); } }5. Create other required Java classes Employee, EmployeeMapper and EmpMainApp under the com.dineshonjava.sdnext package. You can create rest of the POJO classes if required.
package com.dineshonjava.sdnext.domain; /** * @author Dinesh Rajput * */ public class Employee { private int empid; private String name; private int age; private long salary; /** * @return the empid */ public int getEmpid() { return empid; } /** * @param empid the empid to set */ public void setEmpid(int empid) { this.empid = empid; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /** * @return the salary */ public long getSalary() { return salary; } /** * @param salary the salary to set */ public void setSalary(long salary) { this.salary = salary; } public String toString(){ return "EMPLOYEE{empid- "+this.empid+" name- "+this.name+ " age- "+this.age+" salary- "+this.salary+"}"; } }EmployeeMapper.java
package com.dineshonjava.sdnext.jdbc.utils; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; import com.dineshonjava.sdnext.domain.Employee; /** * @author Dinesh Rajput * */ public class EmployeeMapper implements RowMapper { public Employee mapRow(ResultSet rs, int rowNum) throws SQLException { Employee employee = new Employee(); employee.setEmpid(rs.getInt("empid")); employee.setName(rs.getString("name")); employee.setAge(rs.getInt("age")); employee.setSalary(rs.getLong("salary")); return employee; } }EmpMainApp.java
package com.dineshonjava.sdnext.main; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dineshonjava.sdnext.dao.EmpDao; import com.dineshonjava.sdnext.domain.Employee; /** * @author Dinesh Rajput * */ public class EmpMainApp { /** * @param args */ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); EmpDao empDao = (EmpDao) context.getBean("employeeDaoImpl"); System.out.println("------Records Creation--------" ); empDao.create("Raaz", 25, 50000l); System.out.println("------Listing Multiple Records--------" ); List employees = empDao.listEmployees(); for (Employee employee : employees) { System.out.print(employee); } } }6. Create Beans configuration file spring.xml under the src folder.
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.dineshonjava.sdnext.dao.impl"> </context:component-scan> <bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/DAVDB"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> <property name="initialSize" value="2"></property> <property name="maxActive" value="5"></property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="create"></tx:method> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut expression="execution(* com.dineshonjava.sdnext.dao.impl.EmployeeDaoImpl.create(..))" id="createOperation"></aop:pointcut> <aop:advisor advice-ref="txAdvice" pointcut-ref="createOperation"></aop:advisor> </aop:config> <!-- Initialization for TransactionManager --> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <bean class="com.dineshonjava.sdnext.dao.impl.EmployeeDaoImpl" id="employeeDaoImpl"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this will print the following exception will be raised. In this case transaction will be rolled back and no record will be created in the database table.
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="create" read-only="false" rollback-for="NoProductInStockException"> </tx:method> <tx:method name="*"> </tx:method> </tx:attributes> </tx:advice>Configuring different transactional semantics for different beans:
<beans> <aop:config> <aop:pointcut expression="execution(* x.y.service.*Service.*(..))" id="defaultServiceOperation"> </aop:pointcut> <aop:pointcut expression="execution(* x.y.service.ddl.DefaultDdlManager.*(..))" id="noTxServiceOperation"> </aop:pointcut> <aop:advisor advice-ref="defaultTxAdvice" pointcut-ref="defaultServiceOperation"> </aop:advisor> <aop:advisor advice-ref="noTxAdvice" pointcut-ref="noTxServiceOperation"> </aop:advisor> </aop:config> <!-- this bean will be transactional (see the 'defaultServiceOperation' pointcut) --> <bean class="x.y.service.DefaultFooService" id="fooService"></bean> <!-- this bean will also be transactional, but with totally different transactional settings --> <bean class="x.y.service.ddl.DefaultDdlManager" id="anotherFooService"> </bean> <tx:advice id="defaultTxAdvice"> <tx:attributes> <tx:method name="get*" read-only="true"></tx:method> <tx:method name="*"></tx:method> </tx:attributes> </tx:advice> <tx:advice id="noTxAdvice"> <tx:attributes> <tx:method name="*" propagation="NEVER"></tx:method> </tx:attributes> </tx:advice> <!-- other transaction infrastructure beans such as a PlatformTransactionManager omitted... --> </beans>
Labels: Spring3.0