我做了一个demo来测试关于spring事务回滚的功能,运用的是Jdbctemplate来写的,很简单,注释也加了,就是不太清楚事务为什么不回滚?
代码如下:
applicationContext配置文件代码
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开始注释扫描-->
<context:component-scan base-package="org.berg.demo5"/>
<!-- 引入数据库连接属性配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties" />
</bean>
<!--使用dbcp的数据库的操作文件-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- jdbc事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 2、注释模式事务:启动使用注解实现声明式事务管理的支持 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
DAO层代码
public class UserDaoImpl implements UserDao {
@Override
@Transactional(rollbackFor=Exception.class)
public void out(String outer, int money) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext4.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
String usql = "update account set money = money -"+money+" where username='"+outer+"'";
jdbcTemplate.execute(usql);
}
@Override
@Transactional(rollbackFor=Exception.class)
public void in(String inner, int money) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext4.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate)context.getBean("jdbcTemplate");
String usql = "update account set money = money + "+money+" where username ='"+inner+"'";
jdbcTemplate.execute(usql);
}
}
Service层
@Autowired
private UserDao userDao;
@Override
@Transactional(rollbackFor=Exception.class)
public void transfer(String outer, String inner, int money) {
userDao.out(outer, money);
int i=1/0;
userDao.in(inner, money);
}
Test代码
public class Test {
@Autowired
AccountService accountService;
@org.junit.Test
public void Test(){
ApplicationContext ss= new ClassPathXmlApplicationContext("classpath:applicationContext4.xml");
AccountService as= (AccountService) ss.getBean("AccountServiceImpl");
//Tom 向 Marry 转账1000
as.transfer("Tom", "tony", 1000); }
}