我这xml配置事务有没有问题?为什么没起到事务的作用
看上去没什么问题,使用注解更简单一些@Transactional
你的数据库引擎设置是什么
//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">
<context:component-scan base-package="com.springmvc">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--整合mybatis-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="user" value="root"/>
<property name="password" value="1234"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.springmvc.dao"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
<tx:method name="*" read-only="false" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- aop增强-->
<aop:config>
<!-- 修饰符省略 所有返回值 全类名 impl包下的所有类的所有方法,所有形参-->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.springmvc.service.impl.*.*(..))"/>
</aop:config>
</beans>
//业务层代码
@Override
public void transfer(int sourceAcc, int targetAcc, Float moeny) {
Account source = dao.findById(sourceAcc);
Account target = dao.findById(targetAcc);
source.setMoney(source.getMoney()-moeny);
target.setMoney(target.getMoney()+moeny);
dao.update(source);
int a = 5/0;
dao.update(target);
}
由于信息比较少,我给出一点排查建议:
首先要明白,Spring事务管理是基于AOP动态代理实现的,调用所需要事务管理的方法时,应走Spring动态代理生成的类和方法,针对于这种情况,你可以通过以下两种方案来确定:
1. Service应通过依赖注入,可以用构造函数(Spring推荐方案),也可以用@Autowired通过set方法注入。切记不要直接通过this去访问,this关键字所使用的实例对象并不是Spring动态代理生成的,也没有事务增强,自然而然事务也不会生效。
2. debug。这肯定是终极解决方案,但debug也可以分层次。首先,你可以在方法内部打上断点,然后当程序进入断点以后,你可以查看此时的方法调用情况,确认一下是否有CGLIB相关的关键字,有的话,才表示说你的方法是经由动态代理后调用的。如果不是,就赶紧检查一下,调用方案是不是存在问题了。其次,调试中,你就可以一步一步的去拆解代码了,对于看不懂的Spring源码,可以一个类一个方法的去学习,熟悉,不懂的地方,就多上网搜搜,问题处理得多了,经验就有了嘛。
//业务层完整代码
package com.springmvc.service.impl;
import com.springmvc.dao.IAccountDao;
import com.springmvc.domain.Account;
import com.springmvc.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AccountServiceImpl implements IAccountService {
@Autowired
private IAccountDao dao;
@Override
public List<Account> findAll() {
return dao.findAll();
}
@Override
public void save(Account account) {
dao.save(account);
}
@Override
public void transfer(int sourceAcc, int targetAcc, Float moeny) {
Account source = dao.findById(sourceAcc);
Account target = dao.findById(targetAcc);
source.setMoney(source.getMoney()-moeny);
target.setMoney(target.getMoney()+moeny);
dao.update(source);
int a = 5/0;
dao.update(target);
}
}
您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632
非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!
速戳参与调研>>>https://t.csdnimg.cn/Kf0y