berg_hang 2018-10-07 13:20 采纳率: 0%
浏览 657

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);   }
}

  • 写回答

4条回答 默认 最新

  • xiaomad 2018-10-07 15:13
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的