JAVA Spring 事务管理事务不回滚问题

Spring事务管理事务不回滚


dao层:

@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;

@Override
public void inmoney(String name, Integer money) {
    String sql = "update t_user set money=money+? where name=?";
    jdbcTemplate.update(sql, money, name);
}

@Override
public void outmoney(String name, Integer money) {
    String sql = "update t_user set money=money-? where name=?";
    jdbcTemplate.update(sql, money, name);
}
}

Service层:

			@Service
				public class UserServiceImpl implements UserService {
				    @Autowired
				    private UserDao userDao;
				    @Override
				    public void zhuangzhang() {
				        userDao.outmoney("aa",10);
				        int i=1/0;
				        userDao.inmoney("bb",10);
				    }
				}

测试类:

@Component
public class TestAdd {

@Test
public void say(){
    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("beans.xml");
    UserService userService=applicationContext.getBean(UserService.class);
    userService.zhuangzhang();
}
}

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: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">
<!--Spring自动代理-->
<context:annotation-config></context:annotation-config>
<!--开启Spring扫描-->
<context:component-scan base-package="com.zltz"></context:component-scan>

<context:property-placeholder location="jdbc.properties"></context:property-placeholder>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="url" value="${jdbc.url}"></property>
    <property name="password" value="${jdbc.password}"></property>
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="username" value="${jdbc.username}"></property>
</bean>

<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>
<tx:advice id="interceptor" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="zhuangzhang" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="pois" expression="execution(* com.zltz.service..*(..))"></aop:pointcut>
    <aop:advisor advice-ref="interceptor" pointcut-ref="pois"></aop:advisor>
</aop:config>
</beans>

出现的问题:以上代码没有错误,能跑通(毕竟比较简单),但是出错后数据库数据依旧修改,起初怀疑是
切面没有切入成功,但是数据库第一条SQL能修改数据第二条位执行说明已经切入了,就怀疑是Spring事务没有回滚,后来发现Spring事务正常回滚了,仔细研究了几个小时发现错出在了数据库上(还是基础不牢固),我用的数据库管理工具数SQLyong,在建表时有时候默认的引擎是MyIsAM模式。如图:数据库图片在这里插入图片描述
这种模式下不支持事务,得修改为InnoDB模式才可以,真是大意失荆州啊(也是学的不够扎实造成的),希望大家不要这样的犯错误,耽误了宝贵的学习时间。

猜你喜欢

转载自blog.csdn.net/qq_42891281/article/details/103091922