zoukankan      html  css  js  c++  java
  • Spring的事务控制

    一、事务的概念

      事务是一组操作的执行单元,相对于数据库操作来讲,事务管理的是一组SQL指令,比如增加,修改,删除等,事务的一致性,要求,这个事务内的操作必须全部执行成功,如果在此过程种出现了差错,比如有一条SQL语句没有执行成功,那么这一组操作都将全部回滚

      事务特性(ACID)
      (1)Atomic(原子性):要么都成功,要么都失败
      (2)Consistent(一致性):数据应该不被破坏
      (3)Isolate(隔离性):用户间操作不相混淆
      (4)Durable(持久性):永久保存

    二、Spring提供的事务控制方式

      编程式事务管理

      编写程序式的事务管理可以清楚的定义事务的边界,可以实现细粒度的事务控制,比如你可以通过程序代码来控制你的事务何时开始,何时结束等,与后面介绍的声明式事务管理相比,它可以实现细粒度的事务控制,例如jdbc,hibernate,spring中不提倡使用。

      声明式事务管理

      如果你并不需要细粒度的事务控制,你可以使用声明式事务,在Spring中,你只需要在Spring配置文件中做一些配置,即可将操作纳入到事务管理中,解除了和代码的耦合, 这是对应用代码影响最小的选择,从这一点再次验证了Spring关于AOP的概念。当你不需要事务管理的时候,可以直接从Spring配置文件中移除该设置

    三、Spring对事务控制的实现

      Spring的声明式事务管理,核心实现就是基于AOP,但该管理只能进行粗粒度的事务控制,只能给整个方法应用事务而不能对某几行代码应用事务(因为AOP拦截的是方法)

      下面介绍Spring声明式事务管理的实现:

      步骤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: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">   
              
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <!-- 加载jdbc驱动 -->
                <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
                <!-- jdbc连接地址 -->
                <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/kk"></property>
                <!-- 连接数据库的用户名 -->
                <property name="user" value="root"></property>
                <!-- 连接数据库的密码 -->
                <property name="password" value="123456"></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>
            
            <!--  创建jdbcTemplate对象 -->
            <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
                <property name="dataSource" ref="dataSource">
                </property>
            </bean>
            
            <bean id="personDao" class="com.jyk.spring.transaction.PersonDao">
                <property name="jdbcTemplate" ref="jdbcTemplate"></property>
            </bean>
            
            <bean id="personService" class="com.jyk.spring.transaction.PersonService">
                <property name="personDao" ref="personDao">
                </property>
            </bean>
            
           <!-- 配置事务管理器类 -->
            <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                <property name="dataSource" ref="dataSource"></property>
            </bean>
            
            <!--  只读指定为false表示对数据的操作是读取,报异常后不用回滚 -->
            <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(* com.jyk.spring.transaction.PersonService.operateMethod(..))" id="pt"/>
                <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
            </aop:config>
    </beans>

      步骤2.编写业务接口和实现

    package com.jyk.spring.transaction;
    
    public class PersonService {
    
        private PersonDao personDao;
    
        public void setPersonDao(PersonDao personDao) {
            this.personDao = personDao;
        }
        
        public void operateMethod(Person person)
        {
            personDao.addPerson(person);
            
            //人为制造异常方便测试回滚
            int a = 1/0;
            
            //personDao.addPerson(person);
        }
    }
    package com.jyk.spring.transaction;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    
    public class PersonDao {
    
        private JdbcTemplate jdbcTemplate;
            
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
    
        public void addPerson(Person person)
        {
            String sql = "insert into aa(id,name) values (?,?)";
            jdbcTemplate.update(sql,person.getId(),person.getName());
        }
    }

    四、Spring的事务注解式使用

      使用注解实现Spring的声明式事务管理,更加简单快捷,准备工作如下

      (1)必须引入Aop相关的jar文件

      (2)bean.xml中指定注解方式实现声明式事务管理以及应用的事务管理器类

      (3)在需要添加事务控制的地方,写上: @Transactional

       @Transactional注解:

      (1)应用事务的注解

      (2)定义到方法上:当前方法应用spring的声明式事务

      (3)定义到类上:当前类的所有的方法都应用Spring声明式事务管理;

      (4)定义到父类上:当执行父类的方法时候应用事务。

      步骤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: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">   
              
            <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
                <!-- 加载jdbc驱动 -->
                <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
                <!-- jdbc连接地址 -->
                <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/kk"></property>
                <!-- 连接数据库的用户名 -->
                <property name="user" value="root"></property>
                <!-- 连接数据库的密码 -->
                <property name="password" value="123456"></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>
            
            <!--  创建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.jyk.spring.annotation.transaction.property">
            </context:component-scan>
            
            <!--  注解方式实现事务,指定注解方式实现事务 -->
            <tx:annotation-driven transaction-manager="txManager"/>
    </beans>

      步骤2.编写业务实现Service层和Dao层

    package com.jyk.spring.annotation.transaction.property;
    
    import javax.annotation.Resource;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Isolation;
    import org.springframework.transaction.annotation.Propagation;
    import org.springframework.transaction.annotation.Transactional;
    
    @Service
    public class PersonService {
    
        @Resource
        private PersonDao personDao;
        
        /*
         * @Transactional的使用位置
         * 1、定义到方法上:当前方法应用的spring的声明式事务
         * 2、定义到类上:当前类的所有方法都应用spring的声明式事务
         * 3、定义到父类上:当执行父类的方法的时候应用Spring的声明式事务
         */
        @Transactional(
                readOnly=false, //读写事务,只读事务不修改任何数据
                timeout = -1,//事务的超时时间没有限制
            //    noRollbackFor = ArithmeticException.class, //遇到数学异常不回滚
                isolation = Isolation.DEFAULT, //事务的隔离级别    
                propagation = Propagation.REQUIRED //传播行为
                )
        
        public void operateMethod(Person person)
        {
            personDao.addPerson(person);//添加用户
            int myexception = 1/0;
        }
    }
    package com.jyk.spring.annotation.transaction.property;
    
    import javax.annotation.Resource;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public class PersonDao {
    
        @Resource
        private JdbcTemplate jdbcTemplate;
    
        public void addPerson(Person person)
        {
            String sql = "insert into aa(id,name) values (?,?)";
            jdbcTemplate.update(sql,person.getId(),person.getName());
        }
    }
  • 相关阅读:
    20162302预备作业02
    20162302预备作业01
    设计模式第二次作业
    设计模式第一次作业
    项目选题报告(团队)
    高级软件工程结对第二次作业
    高级软件工程团队第一次作业
    高级软件工程结对第一次作业
    高级软件工程第二次作业
    高级软件工程第一次作业--准备
  • 原文地址:https://www.cnblogs.com/jiyukai/p/9387518.html
Copyright © 2011-2022 走看看