spring 事物(三)—— 声明式事务管理详解

spring的事务处理分为两种:

1、编程式事务:在程序中控制事务开始,执行和提交;详情请点此跳转;

2、声明式事务:在Spring配置文件中对事务进行配置,无须在程序中写代码;(建议使用)

我对”声明式“的理解是这样的:Spring配置文件中定义好了这样一个规则,这个规则可以指定对哪些类的哪些方法在执行的时候添加事务控制,并配置好了事务的相关执行属性,就是在这些类的这些方法执行的时候隐式地添加事务开始、执行、提交或回滚的代码(当然我们看不到)

声明式事务又分了两种写法:

  • xml配置文件
  • 注解

XML配置形式

下面我们首先讲解下在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:p="http://www.springframework.org/schema/p"  
        xmlns:aop="http://www.springframework.org/schema/aop"   
        xmlns:context="http://www.springframework.org/schema/context"  
        xmlns:jee="http://www.springframework.org/schema/jee"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xsi:schemaLocation="    
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd  
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
            http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd  
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
    <!-- 类似于财务部门一样,类就是钱,所有需要类的实例都由srping去管理 -->
    <!-- <context:component-scan>:
        有一个use-default-filters属性,该属性默认为true,
        这就意味着会扫描指定包下的全部的标有注解的类,并注册成bean.
        可以发现这种扫描的粒度有点太大,如果你只想扫描指定包下面的Controller,
        该怎么办?此时子标签<context:incluce-filter>就起到了勇武之地。如下所示
        <context:component-scan base-package="news" use-default-filters="false">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
        如果use-dafault-filters在上面并没有指定,默认就为true,
        也就意味着你现在加<context:exclude-filter/>跟没加是一样的
        所有你要记住,你若想要用到<context:component-scan>的子标签,
        必须要把use-dafault-filters的值改为false
        当然还有一个是与之相反的而已这里就不啰嗦了
        上面这一对解释换成一句话就是:
            Use-dafault-filters=”false”的情况下:<context:exclude-filter>指定的不扫描,<context:include-filter>指定的扫描
    <context:component-scan>的base-package属性作用:设置要被扫描的包 -->
    
    <!-- (本案例不用到,只是用了一个全盘扫描,以上内容只是为了让大家了解它) -->
    
    <context:component-scan base-package="news.."/>
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->

    <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
        <!-- 每300秒检查所有连接池中的空闲连接 -->
        <property name="idleConnectionTestPeriod" value="300"/>
        <!-- 最大空闲时间,900秒内未使用则连接被丢弃。若为0则永不丢弃 -->
        <property name="maxIdleTime" value="900"/>
        <!-- 最大连接数 -->
        <property name="maxPoolSize" value="2"/>
    </bean>
    
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="myDataSource"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                <prop key="hibernate.connection.autocommit">false</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>
        <property name="mappingResources">
            <list>
                <value>news/entity/News.hbm.xml</value>
            </list>
        </property>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
         <!--  创建事务管理器, 管理sessionFactory(因为所有的session都是从sessionFactory获取的) -->
         <property name="sessionFactory" ref="sessionFactory" />
     </bean>
    <!--  配置通知, 那些方法需要切入什么类型的事务 -->     
    <tx:advice id="advice" transaction-manager="transactionManager">
         <tx:attributes>
             <tx:method name="add*" propagation="REQUIRED"/>
             <tx:method name="del*" propagation="REQUIRED"/>
             <tx:method name="update*" propagation="REQUIRED"/>
             <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
         </tx:attributes>
     </tx:advice>
     
     <!-- 配置切面表达式, 并且让 tx与切面表达式合二为一 -->
     <aop:config>
         <!-- 表达式, 定义哪个包的哪些类需要切入事务,但是此处并且没有制定类中哪些方法,需要切入什么样 事务 -->
         <aop:pointcut expression="execution(* news.service.*.*(..))" id="pointcut" />
         <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
     </aop:config>
</beans>

相信这段代码对于我们初学者来说不是那么容易看懂,下面我来给大家分析吧:

1、创建事务管理器

2、配置通知

  

<tx:attributes/>
属性 必须 默认值  描述
name   要切入的方法名,可以用通配符
propagation 不是 REQUIRED 事务传播行为
isolation 不是 DEFAULT 事务隔离级别
timeout 不是 -1 事务超时的时间(以秒为单位)
read-only 不是 false 事务是否只读?
rollback-for 不是   将被触发进行回滚的 Exception(s);以逗号分开。 如:'news.serviceImpl'
no-rollback-for 不是    被触发进行回滚的 Exception(s);以逗号分开。 如:'news.serviceImpl'

3、配置切面表达式, 并且让 tx与切面表达式合二为一 

    

解释一下execution(* news.service.*.*(..))"中几个通配符的含义:

第一个 * —— 通配 任意返回值类型

第二个 * —— 通配 包news.service下的任意class

第三个 * —— 通配 包news.service下的任意class的任意方法

第四个 .. —— 通配 方法可以有0个或多个参数 综上:包news.service下的任意class的具有任意返回值类型、任意数目参数和任意名称的方法

做到这里xml方式的事务管理基本就做完了,还差最后一步,那就是导入它相关的缺包(无论哪种形式的事务管理都要导入这些包)

    

注解形式

注解形式的声明式事务管理只要在applicationContext.xml这份文件里面加入一句

<tx:annotation-driven transaction-manager="transactionManager"/>

就可以从上面的代码中把下面写的那一大串xml去掉了,

<tx:advice id="advice" transaction-manager="transactionManager">
         <tx:attributes>
             <tx:method name="add*" propagation="REQUIRED"/>
             <tx:method name="del*" propagation="REQUIRED"/>
             <tx:method name="update*" propagation="REQUIRED"/>
             <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
         </tx:attributes>
 </tx:advice>
     
     配置切面表达式, 并且让 tx与切面表达式合二为一
     <aop:config>
         表达式, 定义哪个包的哪些类需要切入事务,但是此处并且没有制定类中哪些方法,需要切入什么样 事务
         <aop:pointcut expression="execution(* news.service.*.*(..))" id="pointcut" />
         <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
     </aop:config>

那怎么才能实现事务管理呢?

很简单,就不如你的那个类里面的全部方法都要加入事务管理,就可以直接在此类上面加入一个@Transactional注解就可以了

如果你只想一个方法加入事务管理,也可以直接单独在一个方法上面加入@Transactional注解

当然,这个注解还有一些属性比较常用的

参数名称 功能描述
readOnly 该属性用于设置当前事务是否为只读事务,设置为true表示只读,false则表示可读写,默认值为false。例如:@Transactional(readOnly=true)
rollbackFor

该属性用于设置需要进行回滚的异常类数组,当方法中抛出指定异常数组中的异常时,则进行事务回滚。例如:

指定单一异常类:@Transactional(rollbackFor=RuntimeException.class)

指定多个异常类:@Transactional(rollbackFor={RuntimeException.class, Exception.class})

rollbackForClassName

该属性用于设置需要进行回滚的异常类名称数组,当方法中抛出指定异常名称数组中的异常时,则进行事务回滚。例如:

指定单一异常类名称:@Transactional(rollbackForClassName="RuntimeException")

指定多个异常类名称:@Transactional(rollbackForClassName={"RuntimeException","Exception"})

noRollbackFor

该属性用于设置不需要进行回滚的异常类数组,当方法中抛出指定异常数组中的异常时,不进行事务回滚。例如:

指定单一异常类:@Transactional(noRollbackFor=RuntimeException.class)

指定多个异常类:@Transactional(noRollbackFor={RuntimeException.class, Exception.class})

noRollbackForClassName

该属性用于设置不需要进行回滚的异常类名称数组,当方法中抛出指定异常名称数组中的异常时,不进行事务回滚。例如:

指定单一异常类名称:@Transactional(noRollbackForClassName="RuntimeException")

指定多个异常类名称:

@Transactional(noRollbackForClassName={"RuntimeException","Exception"})

propagation

该属性用于设置事务的传播行为,具体取值可参考下面

例如:@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)

isolation 该属性用于设置底层数据库的事务隔离级别,事务隔离级别用于处理多事务并发的情况,通常使用数据库的默认隔离级别即可,基本不需要进行设置
timeout 该属性用于设置事务的超时秒数,默认值为-1表示永不超时

事物传播行为介绍: 

  • @Transactional(propagation=Propagation.REQUIRED) :如果有事务, 那么加入事务, 没有的话新建一个(默认情况下)
  • @Transactional(propagation=Propagation.NOT_SUPPORTED) :容器不为这个方法开启事务
  • @Transactional(propagation=Propagation.REQUIRES_NEW) :不管是否存在事务,都创建一个新的事务,原来的挂起,新的执行完毕,继续执行老的事务
  • @Transactional(propagation=Propagation.MANDATORY) :必须在一个已有的事务中执行,否则抛出异常
  • @Transactional(propagation=Propagation.NEVER) :必须在一个没有的事务中执行,否则抛出异常(与Propagation.MANDATORY相反)
  • @Transactional(propagation=Propagation.SUPPORTS) :如果其他bean调用这个方法,在其他bean中声明事务,那就用事务.如果其他bean没有声明事务

注意,在以下情况中spring的事务管理会失效:

  • private 方法无法添加事务管理.
  • final 方法无法添加事务管理.
  • static 方法无法添加事务管理.
  • 当绕过代理对象, 直接调用添加事务管理的方法时, 事务管理将无法生效.

猜你喜欢

转载自blog.csdn.net/ctwy291314/article/details/82461084