美文网首页
Spring的事务管理

Spring的事务管理

作者: 叫我不矜持 | 来源:发表于2019-02-15 22:30 被阅读4次
一.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>

相关文章

  • Spring中的AOP事务

    【目录】1 Spring的事务管理机制2 Spring事务管理两种方式 1 Spring的事务管理机制 Sprin...

  • Spring事务管理只对出现运行期异常进行回滚

    使用spring难免要用到spring的事务管理,要用事务管理又会很自然的选择声明式的事务管理,在spring的文...

  • spring事务

    1、spring事务管理器PlatformTransactionManager 1.1、没有spring事务管理器...

  • Spring之事务管理

    Spring事务管理(详解+实例)Spring详解(八)------事务管理 一. 概念 事务(Transacti...

  • Spring-事务机制

    一、Spring事务 事务管理概述 Spring事务管理分为编程式事务管理和声明式事务管理两种 编程式事务:允许用...

  • 19、Spring-事务机制-使用

    一、简介 Spring事务管理分为编程式事务管理和声明式事务管理两种, 声明式事务管理:底层是建立在Spring ...

  • Spring声明式事务管理之一:五大属性分析

    1.Spring事务管理概述 Spring事务管理分为编程式事务管理和声明式事务管理两种。编程式事务允许用户在实现...

  • Spring基础(三)

    11. 事务管理 11.1 Spring Framework事务管理介绍 广泛的事务支持是Spring Frame...

  • 【济南中心】 Spring事务管理的方式

    【济南中心】 Spring事务管理的方式 1.spring支持编程式事务管理和声明式事务管理两种方式 编程...

  • Spring事务的种类

    Spring事务的种类: spring支持编程式事务管理和声明式事务管理两种方式: ①编程式事务管理使用Trans...

网友评论

      本文标题:Spring的事务管理

      本文链接:https://www.haomeiwen.com/subject/kfdveqtx.html