框架应用:Spring framework (四) - 事务管理

时间:2023-03-09 16:03:04
框架应用:Spring framework (四) - 事务管理

事务控制

  事务是什么?事务控制?

  事务这个词最早是在数据库中进行应用,讲的用户定义的一个数据库操作序列,这些操作要么全做要么全不做,是一个不可分割的工作单位。

  事务的管理是指一个事务的开启,内容添加,提交和回滚.

代码层次的事务控制

  事务控制原本是在数据库进行的,但由于ORM映射后,操作数据库的语句未必是SQL语句,事务控制也被迁移到了工程语言上(Java/C++/Python).Spring framework支持了事务管理的机制,通过ORM映射后可以在业务代码中实现事务控制.

  框架应用:Spring framework (四) - 事务管理

事务控制形式

  编程式事务控制

    自己手动控制事务,jdbc和Hibernate提供这种事务管理方式.

    conn.setAutoCommit(false);  //jdbc设置手动控制事务

    session.beginTransaction();  //Hibernate开始一个事务

    这种方式可以控制到代码细节,灵活多变.

  声明式事务控制

    以方法为单位,可以在类的方法中织入事务控制的代码,从而解除了事务代码与业务代码的耦合.

    这种方式需要在配置文件中配置,虽然无法控制到代码细节(无法在某个方法中的某几行加入事务控制),但一般情况适用.

    框架应用:Spring framework (四) - 事务管理

Spring事务控制

  利用AOP技术实现声明式事务控制,包括XML方式和注解方式.

  xml方式    

  1.导入jar包

  2.加入ioc,aop,apectj,tx等约束,并配置数据源,声明式事务,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:p="http://www.springframework.org/schema/p"
    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">

    <!-- 1. 数据源对象: C3P0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>

    <!-- 2. JdbcTemplate工具类实例 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 3. dao实例 -->
    <bean id="deptDao" class="com.harry.transaction.dao.DeptDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <!-- 4. service实例 -->
    <bean id="deptService" class="com.harry.transaction.service.DeptService">
        <property name="deptDao" ref="deptDao"></property>
    </bean>

    <!-- #############5. Spring声明式事务管理配置############### -->
    <!-- 5.1 配置事务管理器类 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 5.2 配置事务增强(如果管理事务?) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!-- 5.3 Aop配置: 拦截哪些方法(切入点表表达式) + 应用上面的事务增强配置 -->
    <aop:config>
        <aop:pointcut expression="execution(* ccm.harry.transaction.service.save*(..))" id="pt"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>

</beans>

applicationContext.xml

  

  注解方式

  1.导入jar包

  2.xml数据源配置,事务管理类配置,开启ioc,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:p="http://www.springframework.org/schema/p"
    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">

    <!-- 1. 数据源对象: C3P0连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>

    <!-- 2. JdbcTemplate工具类实例 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 事务管理器类 -->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.harry.transaction"></context:component-scan>

    <!-- 注解方式实现事务: 指定注解方式实现事务 -->
    <tx:annotation-driven transaction-manager="txManager"/>

</beans>      

applicationContext.xml

  3.在Dao中的类或者类方法上使用@Transactional来表明事务增强的切入点  

  框架应用:Spring framework (四) - 事务管理

  事务属性

@Transactional(
            readOnly = false,  // 读写事务
            timeout = -1,       // 事务的超时时间不限制
            noRollbackFor = ArithmeticException.class,  // 遇到数学异常不回滚
            isolation = Isolation.DEFAULT,              // 事务的隔离级别,数据库的默认
            propagation = Propagation.REQUIRED            // 事务的传播行为
    )
    public void save(Dept dept){
        deptDao.save(dept);
        int i = 1/0;
        deptDao.save(dept);
    }

txProperty.java

  上述属性除了事务传播行为,其他都容易理解.重点需要理解事务的传播行为.

    Propagation.REQUIRED

      指定当前的方法必须在事务的环境下执行;

      如果当前运行的方法,已经存在事务, 就会加入当前的事务;

    Propagation.REQUIRED_NEW

      指定当前的方法必须在事务的环境下执行;

      如果当前运行的方法,已经存在事务:  事务会挂起; 会始终开启一个新的事务,执行完后;  刚才挂起的事务才继续运行。  

/***************************回滚,日志不会写入************************/
Class Log1{
        Propagation.REQUIRED  
        insertLog();
}

    Propagation.REQUIRED
    Void  saveDept(){
        insertLog();    // 加入当前事务
        .. 异常, 会回滚
        saveDept();
    }
/****************回滚,日志会开启新事务并进行写入****************/
Class Log2{
        Propagation.REQUIRED_NEW  
        insertLog();
}

    Propagation.REQUIRED
    Void  saveDept(){
        insertLog();    // 始终开启事务
        .. 异常, 日志不会回滚
        saveDept();
    }

txPropagation.java