spring事务管理,不回滚,demo测试出bug(很简单demo,耽误大佬们,30s)

我做了一个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);   }
}

数据库本身支持回滚吗?mysql的话要InnoDB才支持回滚

@Transactional注解事务不起作用的话有多种可能,

是否开启了对注解的解析,你已经写了 这个就pass了,
下一个数据库引擎要支持事务, 如果是mysql数据库的话,如果使用的是myisam,事务是不起作用的,可以改为innodb

应该还要配置切入点把。

几点:
1. DAO 无需配置事务注解
2. 上下文无需重复手工加载

可以改成这样测试看看

@Repository
 public class UserDaoImpl implements  UserDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public void out(String outer, int money) {
        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) {         
        String usql = "update account set money = money + "+money+" where username ='"+inner+"'";
        jdbcTemplate.execute(usql);
    }
}

还有问题的话,可以跟我讨论.

回答完毕,希望帮到你!