spring中事务管理使用@Transactional无法实现回滚,
Dao层:
package cn.BlackHumour.b_anno;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
/**
* dao实现,使用Spring对jdbc支持功能
*
*/
@Repository
public class DeptDao {
@Resource
private JdbcTemplate jdbcTemplate;
public void save(Dept dept) {
String sql = "insert into t_dept (deptName) values(?);";
jdbcTemplate.update(sql,dept.getDeptName());
}
}
Service层:
package cn.BlackHumour.b_anno;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
*
*
*/
@Service
public class DeptService {
@Resource
private DeptDao deptDao;
/**
* 事务控制
*/
@Transactional()
public void save(Dept dept) {
deptDao.save(dept); // 第一次
int i = 1/0;
deptDao.save(dept); // 第二次
}
}
测试:
package cn.BlackHumour.b_anno;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
@Test
public void testApp() throws Exception {
// 容器对象
ApplicationContext aContext = new ClassPathXmlApplicationContext("cn/BlackHumour/b_anno/bean.xml");
// 模拟数据
Dept dept = new Dept();
dept.setDeptName("测试");
DeptService deptService = (DeptService) aContext.getBean("deptService");
deptService.save(dept);
}
}
bean.XML
<?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:p="http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://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">
<!-- 1. 数据源对象:C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean>
<!-- 2. 配置JdbcTamplate工具类实例 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理器类 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解扫描 -->
<context:component-scan base-package="cn.BlackHumour.b_anno"></context:component-scan>
<!-- 注解方式实现事务:指定注解方式实现事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>