事务管理--------使用spring的transactionManager管理事务
dao层
public class EmpDaoImpl extends JdbcDaoSupport implements EmpDao {
@Override
public Emp selectOneEmp(Integer id) throws SQLException {
String sql = "select * from emp where empno=?";
List<Emp> emps = getJdbcTemplate().query(sql, new RowMapper<Emp>() {
@Override
public Emp mapRow(ResultSet resultSet, int i) throws SQLException {
Emp emp = new Emp();
emp.setEname(resultSet.getString("ename"));
emp.setEmpno(resultSet.getInt("empno"));
emp.setJob(resultSet.getString("job"));
emp.setHiredate(resultSet.getDate("hiredate"));
emp.setSal(resultSet.getDouble("sal"));
return emp;
}
},id);
return emps.get(0);
}
}
service层
public class EmpServiceImpl implements EmpService {
@Override
public Emp selectOneEmp(Integer id) throws SQLException {
return empDao.selectOneEmp(id);
}
@Override
public void sqlChange(Integer src,Integer target) {
try {
Emp srcEmp = empDao.selectOneEmp(src);
srcEmp.setSal(srcEmp.getSal()-500);
empDao.updateEmp(srcEmp);
int i = 1/0;
Emp targetEmp = empDao.selectOneEmp(target);
targetEmp.setSal(targetEmp.getSal()+500);
empDao.updateEmp(targetEmp);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置数据源 使用druid-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/lanqiao?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<bean id="empDao" class="org.lanqiao.dao.impl.EmpDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="empService" class="org.lanqiao.service.impl.EmpServiceImpl">
<property name="empDao" ref="empDao"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事务通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">-->
<tx:attributes>
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!--使用aop的方式将事务织入-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="empPoint" expression="execution(* org.lanqiao.service..*.*(..))"/>
<!--配置顾问 = 通知 + 切入点-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="empPoint"/>
</aop:config>
</beans>
测试类
@Test
public void sqlChangeTest() throws SQLException {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
EmpService service = context.getBean("empService",EmpService.class);
service.sqlChange(1001,1002);
}
注解方式
@Service("empService")
//使用在类上,表示当前类中的所有方法的将使用该策略
@Transactional(readOnly = false,propagation = Propagation.REQUIRED)
public class EmpServiceImpl implements EmpService {
@Override
//使用在方法上,表示当前方法将使用改事务策略
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public Emp selectOneEmp(Integer id) throws SQLException {
return empDao.selectOneEmp(id);
}
@Override
public void sqlChange(Integer src,Integer target) {
try {
Emp srcEmp = empDao.selectOneEmp(src);
srcEmp.setSal(srcEmp.getSal()-500);
empDao.updateEmp(srcEmp);
int i = 1/0;
Emp targetEmp = empDao.selectOneEmp(target);
targetEmp.setSal(targetEmp.getSal()+500);
empDao.updateEmp(targetEmp);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config/>
<context:component-scan base-package="org.lanqiao"/>
<!--默认的事务管理器的bean name是transactionManager-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 配置数据源 使用druid-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/lanqiao?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--dao继承了JdbcDaoSupport 父类中存在dataSource只能用setter方式注入-->
<bean id="empDao" class="org.lanqiao.dao.impl.EmpDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>