【深入Spring】Spring的事务管理

本文深入讲解Spring事务管理,包括事务管理API、环境搭建、使用AspectJ的AOP配置及注解管理事务。涵盖PlatformTransactionManager接口、事务传播行为、隔离级别、回滚策略等关键概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.Spring事务管理API介绍

1.事务管理器
事务管理器是PlatformTransactionManager接口对象。其主要用于完成事务的提交、回滚,及获取事务的状态信息。 PlatformTransactionManager接口常用的两个实现类
1.1.DataSourceTransactionManager:使用JDBC或MyBatis 进行持久化数据时使用。
1.2.HibernateTransactionManager:使用Hibernate进行持久化数据时使用。

  1. Spring的回滚方式
    Spring事务的默认回滚方式是:发生运行时异常时回滚,发生受查异常时提交。

  2. 事务定义接口事务定义接口TransactionDefinition中定义了事务描述相关的三类常量:事务隔离级别、事务传播行为、事务默认超时时限,及对它们的操作。
    所谓事务传播行为是指,处于不同事务中的方法在相互调用时,执行期间事务的维护情况。如,A事务中的方法doSome()调用B事务中的方法doOther(),在调用执行期间事务的维护情况,就称为事务传播行为。
    REQUIRED:指定的方法必须在事务内执行。若当前存在事务,就加入到当前事务中;若当前没有事务,则创建一个新事务。这种传播行为是最常见的选择

二.环境搭建

配置文件约束如下:

<?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:tx="http://www.springframework.org/schema/tx"   
xmlns:aop="http://www.springframework.org/schema/aop" 
xsi:schemaLocation="       
http://www.springframework.org/schema/beans       
http://www.springframework.org/schema/beans/spring-beans.xsd       
http://www.springframework.org/schema/tx       
http://www.springframework.org/schema/tx/spring-tx.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">
三.使用AspectJ的AOP配置管理事务(重点)
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    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/tx
        http://www.springframework.org/schema/tx/spring-tx.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">
   
   <!-- 引入数据库连接配置文件 -->
   <context:property-placeholder location="config.properties"/>
   
   <!-- 配置数据源 c3p0 -->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
   </bean>
   
   <!-- 注册jdbc模板对象 -->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
   </bean>

   <!-- 注册AccountDao层实现类 -->
   <bean id="accountDaoImpl" class="com.bjsxt.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
   </bean> 
   
   <!-- 注册FundDao层实现类 -->
   <bean id="fundDaoImpl" class="com.bjsxt.dao.impl.FundDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
   </bean>
   
   <!-- 注册service实现类 -->
   <bean id="accountServiceImpl" class="com.bjsxt.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDaoImpl"></property>
        <property name="fundDao" ref="fundDaoImpl"></property>
   </bean>
   
   <!-- 注册事务对象 -->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
   </bean>

   <!-- 注册事务通知 (重点)-->
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="buyFund" isolation="DEFAULT" rollback-for="com.bjsxt.exception.FundException" propagation="REQUIRED"/>
        </tx:attributes>
   </tx:advice>

   <!-- 配置aop切面 -->
   <aop:config>
        <aop:pointcut expression="execution(* *..service.*.buyFund(..))" id="point2"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="point2"/>
   </aop:config>

</beans>
四.使用事务注解管理事务
@Transactional(isolation=Isolation.DEFAULT,rollbackFor=FundException.class,propagation=Propagation.REQUIRED)
@Override
//rollbackFor指的是遇到什么异常进行回滚操作
public void buyFund(Account account, Fund fund) throws FundException {
    accountDao.update(account.getAid(), account.getBalance());
    if(true){
                //手动抛出异常
        throw new FundException("购买失败");
    }
    fundDao.update(fund.getFid(), fund.getAmount());
}

spring配置文件

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    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/tx
        http://www.springframework.org/schema/tx/spring-tx.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">
   
   <!-- 引入数据库连接配置文件 -->
   <context:property-placeholder location="config.properties"/>
   
   <!-- 配置数据源 c3p0 -->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
   </bean>
   
   <!-- 注册jdbc模板对象 -->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
   </bean>
   <!-- 注册AccountDao层实现类 -->
   <bean id="accountDaoImpl" class="com.bjsxt.dao.impl.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"></property>
   </bean> 
   
   <!-- 注册FundDao层实现类 -->
   <bean id="fundDaoImpl" class="com.bjsxt.dao.impl.FundDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
   </bean>
   
   <!-- 注册service实现类 -->
   <bean id="accountServiceImpl" class="com.bjsxt.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDaoImpl"></property>
        <property name="fundDao" ref="fundDaoImpl"></property>
   </bean>
   
   <!-- 注册事务对象 -->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
   </bean>

  <!--重点(配置事务注解驱动)-->
   <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值