zoukankan      html  css  js  c++  java
  • Spring入门——事务管理Transaction Manager

    参考文章:

    Spring事务管理(详解+实例)

    Spring事务配置的五种方式

    Spring事务管理及几种简单的实现

    Spring中事务控制的 API介绍

    1、事务初识

    事务是逻辑上的一组操作,要么全部成功,要么全部失败。

    事务具有ACID特性,参考百度百科,具体如下:

    原子性(Atomicity):整个事务中的所有操作,要么全部完成,要么全部不完成,不可能停滞在中间某个环节。

    一致性(Consistency):事务必须始终保持系统处于一致的状态,不管在任何给定的时间并发事务有多少。

    隔离性(Isolation):隔离状态执行事务,使它们好像是系统在给定时间内执行的唯一操作。

    持久性(Durability):在事务完成以后,该事务对数据库所作的更改便持久的保存在数据库之中,并不会被回滚。

    2、核心接口API

     
    image

    如上图,Spring事务管理高层抽象主要有3个:

    PlatformTransactionManager :事务管理器(用来管理事务,包含事务的提交,回滚)

    TransactionDefinition :事务定义信息(隔离,传播,超时,只读)

    TransactionStatus :事务具体运行状态

    2.1 PlatformTransactionManager核心事务管理器

    是Spring的事务管理器核心接口。

    Spring本身并不支持事务实现,只是负责包装底层事务,应用底层支持什么样的事务策略,Spring就支持什么样的事务策略。

    里面提供了常用的操作事务的方法:

    TransactionStatus getTransaction(TransactionDefinition definition):获取事务状态信息

    void commit(TransactionStatus status):提交事务

    void rollback(TransactionStatus status):回滚事务

    
    Public interface PlatformTransactionManager()...{
    
        // 由TransactionDefinition得到TransactionStatus对象
    
        TransactionStatus getTransaction(TransactionDefinition definition) throws TransactionException;
    
        // 提交
    
        Void commit(TransactionStatus status) throws TransactionException; 
    
        // 回滚
    
        Void rollback(TransactionStatus status) throws TransactionException; 
    
        }
    
    

    2.1.1 JDBC事务

    <bean id="transactionManager" class="org.springframework.jdbc.datasourceTransactionManager">    
    
        <property name="dataSource" ref="dataSource"/>
    
    </bean>
    

    DataSource TransactionManager:使用JDBC和iBatis进行持久化数据时使用

    2.1.2 Hibernate事务

    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    
        <property name="sessionFactory" ref="sessionFactory" />
    
    </bean>
    

    Hibernate TransactionManager:使用Hibernate 3.0进行持久化数据时使用

    2.1.3 Java持久化API事务(JPA)

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    
        <property name="sessionFactory" ref="sessionFactory" />
    
    </bean>
    

    JpaTransactionManager:使用JPA进行持久化时使用

    2.1.4 Java原生API事务

    <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager">
    
        <property name="transactionManagerName" value="java:/TransactionManager" />
    
    </bean>
    

    JtaTransactionManager :如果没有使用上述的事务管理,或者在一个事务跨越多个事务管理源时使用

    2.2 TransactionDefinition信息对象

    该接口定义了一些基本事务属性

    public interface TransactionDefinition {
        int getPropagationBehavior(); // 返回事务的传播行为
        int getIsolationLevel(); // 返回事务的隔离级别,事务管理器根据它来控制另外一个事务可以看到本事务内的哪些数据
        int getTimeout();  // 返回事务必须在多少秒内完成
        boolean isReadOnly(); // 事务是否只读,事务管理器能够根据这个返回值进行优化,确保事务是只读的
    } 
    

    2.3 TransactionStatus运行状态

    该接口定义了事务具体的运行状态

    public interface TransactionStatus{
        boolean isNewTransaction(); // 是否是新的事物
        boolean hasSavepoint(); // 是否有恢复点
        void setRollbackOnly();  // 设置为只回滚
        boolean isRollbackOnly(); // 是否为只回滚
        boolean isCompleted; // 是否已完成
    } 
    

    3、编程式事务

    3.1 编程式和声明式事务的区别

    Spring提供了对编程式事务和声明式事务的支持,编程式事务允许用户在代码中精确定义事务的边界,而声明式事务(基于AOP)有助于用户将操作与事务规则进行解耦。
    简单地说,编程式事务侵入到了业务代码里面,但是提供了更加详细的事务管理;而声明式事务由于基于AOP,所以既能起到事务管理的作用,又可以不影响业务代码的具体实现。

    3.2 如何实现编程式事务?

    spring提供了两种编程式事务管理,分别是使用TransactionTemplate和使用PlatformTransactionManager。

    3.2.1 使用TransactionTemplate

    采用TransactionTemplate和采用其他Spring模板一样,使用回调方法,把应用程序从处理取得和释放资源中解放出来。TransactionTemplate是线程安全的。代码示例如下:

    TransactionTemplate tt = new TransactionTemplate(); // 新建一个TransactionTemplate
        Object result = tt.execute(
            new TransactionCallback(){  
                public Object doTransaction(TransactionStatus status){  
                    updateOperation();  
                    return resultOfUpdateOperation();  
                }  
        }); // 执行execute方法进行事务管理
    

    使用TransactionCallback()可以返回一个值。如果使用TransactionCallbackWithoutResult则没有返回值。

    3.2.2 使用PlatformTransactionManager

    示例代码如下:

    DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); //定义一个某个框架平台的TransactionManager,如JDBC、Hibernate
        dataSourceTransactionManager.setDataSource(this.getJdbcTemplate().getDataSource()); // 设置数据源
        DefaultTransactionDefinition transDef = new DefaultTransactionDefinition(); // 定义事务属性
        transDef.setPropagationBehavior(DefaultTransactionDefinition.PROPAGATION_REQUIRED); // 设置传播行为属性
        TransactionStatus status = dataSourceTransactionManager.getTransaction(transDef); // 获得事务状态
        try {
            // 数据库操作
            dataSourceTransactionManager.commit(status);// 提交
        } catch (Exception e) {
            dataSourceTransactionManager.rollback(status);// 回滚
        }
    

    4、声明式事务

    4.1 配置方式

    根据代理机制的不同,总结了五种Spring事务的配置方式,配置文件如下:
    第一种方式:每个Bean都有一个代理

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
        <bean id="sessionFactory"  
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
            <property name="configLocation" value="classpath:hibernate.cfg.xml" />  
            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        </bean>  
    
        <!-- 定义事务管理器(声明式的事务) -->  
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        
        <!-- 配置DAO -->
        <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        
        <bean id="userDao"  
            class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">  
               <!-- 配置事务管理器 -->  
               <property name="transactionManager" ref="transactionManager" />     
            <property name="target" ref="userDaoTarget" />  
             <property name="proxyInterfaces" value="com.bluesky.spring.dao.GeneratorDao" />
            <!-- 配置事务属性 -->  
            <property name="transactionAttributes">  
                <props>  
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>  
            </property>  
        </bean>  
    </beans>
    

    第二种方式:所有Bean共享一个代理基类

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
        <bean id="sessionFactory"  
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
            <property name="configLocation" value="classpath:hibernate.cfg.xml" />  
            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        </bean>  
    
        <!-- 定义事务管理器(声明式的事务) -->  
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        
        <bean id="transactionBase"  
                class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"  
                lazy-init="true" abstract="true">  
            <!-- 配置事务管理器 -->  
            <property name="transactionManager" ref="transactionManager" />  
            <!-- 配置事务属性 -->  
            <property name="transactionAttributes">  
                <props>  
                    <prop key="*">PROPAGATION_REQUIRED</prop>  
                </props>  
            </property>  
        </bean>    
       
        <!-- 配置DAO -->
        <bean id="userDaoTarget" class="com.bluesky.spring.dao.UserDaoImpl">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        
        <bean id="userDao" parent="transactionBase" >  
            <property name="target" ref="userDaoTarget" />   
        </bean>
    </beans>
    

    第三种方式:使用拦截器

    <?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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
    
        <bean id="sessionFactory"  
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
            <property name="configLocation" value="classpath:hibernate.cfg.xml" />  
            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        </bean>  
    
        <!-- 定义事务管理器(声明式的事务) -->  
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean> 
       
        <bean id="transactionInterceptor"  
            class="org.springframework.transaction.interceptor.TransactionInterceptor">  
            <property name="transactionManager" ref="transactionManager" />  
            <!-- 配置事务属性 -->  
            <property name="transactionAttributes">  
                <props>  
                    <prop key="*">PROPAGATION_REQUIRED</prop>  
                </props>  
            </property>  
        </bean>
          
        <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
            <property name="beanNames">  
                <list>  
                    <value>*Dao</value>
                </list>  
            </property>  
            <property name="interceptorNames">  
                <list>  
                    <value>transactionInterceptor</value>  
                </list>  
            </property>  
        </bean>  
      
        <!-- 配置DAO -->
        <bean id="userDao" class="com.bluesky.spring.dao.UserDaoImpl">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    </beans>
    

    第四种方式:使用tx标签配置的拦截器

    <?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-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
        <context:annotation-config />
        <context:component-scan base-package="com.bluesky" />
    
        <bean id="sessionFactory"  
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
            <property name="configLocation" value="classpath:hibernate.cfg.xml" />  
            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        </bean>  
    
        <!-- 定义事务管理器(声明式的事务) -->  
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <tx:method name="*" propagation="REQUIRED" />
            </tx:attributes>
        </tx:advice>
        
        <aop:config>
            <aop:pointcut id="interceptorPointCuts"
                expression="execution(* com.bluesky.spring.dao.*.*(..))" />
            <aop:advisor advice-ref="txAdvice"
                pointcut-ref="interceptorPointCuts" />        
        </aop:config>      
    </beans>
    

    第五种方式:全注解

    <?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-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    
        <context:annotation-config />
        <context:component-scan base-package="com.bluesky" />
    
        <tx:annotation-driven transaction-manager="transactionManager"/>
    
        <bean id="sessionFactory"  
                class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
            <property name="configLocation" value="classpath:hibernate.cfg.xml" />  
            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        </bean>  
    
        <!-- 定义事务管理器(声明式的事务) -->  
        <bean id="transactionManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        
    </beans>
    

    此时在DAO上需加上@Transactional注解,如下:

    package com.bluesky.spring.dao;
    import java.util.List; 
    import org.hibernate.SessionFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 
    import org.springframework.stereotype.Component;
    import com.bluesky.spring.domain.User;
    
    @Transactional
    @Component("userDao")
    public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
        public List<User> listUsers() {
            return this.getSession().createQuery("from User").list();
        }
       ....
    }
    


    作者:小汉同学
    链接:https://www.jianshu.com/p/7c6d4dbbe8fc
    来源:简书
    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
  • 相关阅读:
    畅通工程续
    find the safest road
    Window Pains
    什么是DO / DTO / BO / VO /AO ?
    编程四大件
    1.Redis简介和安装
    0.Redis课程大纲
    8.docker容器虚拟化与传统虚拟机比较
    7.docker私有仓库
    6.Docker服务编排
  • 原文地址:https://www.cnblogs.com/anyiz/p/10661635.html
Copyright © 2011-2022 走看看