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 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿