• Spring 事务


    事务:一系列的动作,被当成一个单独的工作单元,要么都完成,要么全部不起作用。
    事务的四个属性:ACID    原子性,一致性,隔离性,持久性
    
    之前的事务处理:JDBC与Hibernate中的事务处理——与try...catch...finally...一起用。
    Spring中的事务处理:编程式,声明式。
    Spring从事务管理的API中抽象出一套独立事务机制。事务管理代码能独立于特点的具体技术。
    
    一、Spring的声明性事务。
    准备工作:配置数据源对象。
    
    首先,配置事务管理器。
    DataSourceTransactionManager类
        dataSource属性注入
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>    
    
    然后,在配置文件中启用事务注解    
    把tx命名空间加进来。
    <tx:annotation-driven transaction-manager=""transactionManager">
    
    最后,添加事务注解。在使用的方法上添加这个注解。
    @Transactional

    1.使用注解声明事物

    <?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"
        default-autowire="byName"
        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/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
        
        <!-- 自动扫描 -->    
        <context:component-scan base-package="com.itnba.maya.daoImpl,com.itnba.maya.serviceImpl"></context:component-scan>
        <!-- 读取数据库连接文件 -->    
        <context:property-placeholder location="classpath:db.properties"/>
        <!-- 生成连接池 -->
        <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
            <property name="driverClass" value="${driverClass}"></property>
            <property name="jdbcUrl" value="${jdbcUrl}"></property>
            <property name="user" value="${user}"></property>
            <property name="password" value="${password}"></property>
            <property name="minPoolSize" value="${minPoolSize}"></property>
            <property name="maxPoolSize" value="${maxPoolSize}"></property>
            <property name="initialPoolSize" value="${initialPoolSize}"></property>
        </bean>
        
        <!-- 生成JdbcTemplate -->
        <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- 配置事务管理器 -->
        <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    
        <!-- 启动事务注解 -->
        <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>

    注解演示:

    package com.itnba.maya.serviceImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.transaction.annotation.Transactional;
    import com.itnba.maya.service.IInfoService;
    
    @Component//自动扫描注解
    public class TestAll {
        @Autowired//ByName自动装配
        private IInfoService infoService;
        
        @Transactional//添加事物,当其中任何一个操作出现错误时,事物都会回滚
        public void deleteMany(){
            infoService.delete("p003");
            infoService.delete("p004");
        }    
    }

    二、事务的传播行为

    一个事务方法调用另一个事务方法时,如何进行事务控制。是做为外部事务的整体控制?还是做为内部事务的部分控制?
    
    在@Transactional注解中,使用propagation属性指导事务的传播行为。
    
    required -- 有事务就行(事务默认传播行为)
    @Transactional(propagation=Propagation.REQUIRED)
    
    required new -- 使用自己的事务,外面调用的方法的事务被挂起。
    @Transactional(propagation=Propagation.REQUIRES_NEW)

    还以上面题目作为示例,(如果我在service层中加上事物)

    @Service
    public class InfoService implements IInfoService {
    
        @Autowired//ByName自动装配
        private IWorkDao workDao;
        @Autowired
        private IInfoDao infoDao;
        
        //@Transactional(propagation=Propagation.REQUIRES_NEW)//使用自己的事务,TestAll中的事物将不起作用。
    @Transactional(propagation=Propagation.REQUIRED)//默认事物传播方式 @Override public void delete(String code) { workDao.deleteByInfoCode(code); infoDao.delete(code); } }

    **************************************************************************************************************

    通过XML方式来声明事物

    配置数据源对象
    用<bean>实例化各个业务对象。
    
    1.配置事务管理器。
    <bean id="transactionManager" class="org.springframework.jdbc.datasourceManager">
        <property name="datasource" ref="datasource"></property>
    </bean>
    2.配置事务属性
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="方法名" propagation="REQUIRES_NEW"/>    <!--新开事务-->
            <tx:method name="*"/>                                    <!--使用原有事务-->
        </tx:attributes>
    </tx:advice>
    3.配置事务切入点,注入事务属性
    <aop:config>
        <aop:pointcut expression="execution(.......)" id="txPointCut"/>
        <aop:advisor advice-ref="txtAdvice" pointcut-ref="txtPointCut"/>
    </aop:config>

    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:context="http://www.springframework.org/schema/context"
        default-autowire="byName"
        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/context 
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
        <!-- 读取数据库连接文件 -->    
        <context:property-placeholder location="classpath:db.properties"/>
        <!-- 生成连接池 -->
        <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
            <property name="driverClass" value="${driverClass}"></property>
            <property name="jdbcUrl" value="${jdbcUrl}"></property>
            <property name="user" value="${user}"></property>
            <property name="password" value="${password}"></property>
            <property name="minPoolSize" value="${minPoolSize}"></property>
            <property name="maxPoolSize" value="${maxPoolSize}"></property>
            <property name="initialPoolSize" value="${initialPoolSize}"></property>
        </bean>
        
        <!-- 生成JdbcTemplate -->
        <bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <!-- 属性注入 -->
        <bean class="com.itnba.maya.daoImpl.InfoDao" id="infoDao">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        <bean class="com.itnba.maya.daoImpl.WorkDao" id="workDao">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
        <bean class="com.itnba.maya.serviceImpl.InfoService" id="infoService">
            <property name="workDao" ref="workDao"></property>
            <property name="infoDao" ref="infoDao"></property>
        </bean>
        
        
        <!-- 配置事务管理器 -->
        <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                <!-- *代表对所有的事物方法 -->
                <tx:method name="*" propagation="REQUIRED"/>
            </tx:attributes>
        </tx:advice>
        
        <!-- Spring的事务声明是基于AOP面向切面实现的(不要忘记倒入aspectjweaver的jar包),切面Spring已经定义好了,我们只需要定义切点就可以 -->
        <aop:config>
            <!-- 定义切点(pointcut),所有包中的所有类中的所有方法都加上事务 -->
            <aop:pointcut expression="execution(* com.itnba.maya..*.*(..))" id="pointCut"/>
            <!-- 将切点与事务联系起来 -->
            <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
        </aop:config>
        
    </beans>
  • 相关阅读:
    8.耍杂技的牛 推公式
    内联函数分析
    类的静态成员变量
    操作符重载的概念
    数组本质分析
    动态内存分配
    函数重载分析
    指针本质分析
    单引号和双引号
    内存操作经典问题分析
  • 原文地址:https://www.cnblogs.com/AnswerTheQuestion/p/6653562.html
Copyright © 2020-2023  润新知