zoukankan      html  css  js  c++  java
  • 云笔记项目-Spring事务学习_测试准备

    在做云笔记项目的过程中,顺便简单的学习了Spring的事务概念,业务以如果添加笔记,则增加用户星星数目作为例子,引入了事务的概念。类似注册送积分之类的,云笔记项目以增加笔记就送星星来说明事务。具体在添加笔记的业务方法中,添加增加星星的业务方法。添加笔记方法和添加星星方法都声明了事务,其中一个执行抛出RuntimeException,就会导致捆绑在一起的整个事务的回滚,具体不在展示这部分代码。

    从添加笔记则送星星的事务例子中,发现事务有传播属性和隔离属性,下面参考大牛博客,简单的学习下事务传播属性,以下是自己的一些理解,可能有不对的地方,还请有人看到后指正。

    Spring事务的传播属性

    事务的传播行为类型 说明
    @Transactional(propagation=Propagation.REQUIRED) 如果外层方法没有事务,就内层方法新建一个事务,如果已经存在一个方法的事务中,加入到这个事务中。这是最常见的选择。
    @Transactional(propagation=Propagation.MANDATORY) 使用当前外层方法的事务,如果当前外层方法没有事务,就抛出异常。
    @Transactional(propagation=Propagation.NESTED) 如果当前外层方法存在事务,则内层方法嵌套在事务内执行。如果当前外层方法没有事务,则与PROPAGATION_REQUIRED类似
    @Transactional(propagation=Propagation.NEVER) 以非事务方式执行,如果当前外层方法存在事务,则抛出异常。
    @Transactional(propagation=Propagation.NOT_SUPPORTED) 以非事务方式执行操作,如果当前外层方法存在事务,就把当前外层事务挂起。
    @Transactional(propagation=Propagation.REQUIRES_NEW) 新建事务,如果当前外层方法存在事务,把当前外层方法事务挂起。
    @Transactional(propagation=Propagation.SUPPORTS) 支持当前外层方法事务,如果当前外层方法没有事务,内层方法就以非事务方式执行。

    以上是事务传播的几个属性,看说明还是不太好理解,下面以实际的例子来体会几个属性的特性。参考博文,自己进行了修改,以插入一条员工信息来说明Spring事务传播的七种情形。大致采用了如下的步骤来实现,后面大致会进行简单的说明。

    (1)新建一个Maven项目,导入必要的依赖包

    (2)使用Spring MVC+MyBatis+MySQL来执行对数据库的操作和对象的管理

    (3)新建两张表,用于保存员工信息

    (4)对Spring-mvc和MyBatis进行配置,并添加事务管理

    (5)七种传播行为的测试理解

    环境搭建

    导入依赖包,具体如下:

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      2   <modelVersion>4.0.0</modelVersion>
      3   <groupId>com.boe</groupId>
      4   <artifactId>SpringTransaction</artifactId>
      5   <version>0.0.1-SNAPSHOT</version>
      6   <packaging>war</packaging>
      7   
      8   <dependencies>
      9      <!-- 导入测试的包 -->
     10     <dependency>
     11      <groupId>junit</groupId>
     12      <artifactId>junit</artifactId>
     13      <version>4.12</version>
     14     </dependency>
     15   
     16     <dependency>
     17      <groupId>junit</groupId>
     18      <artifactId>junit</artifactId>
     19      <version>4.12</version>
     20      <classifier>sources</classifier>
     21     </dependency>
     22   
     23     <dependency>
     24      <groupId>junit</groupId>
     25      <artifactId>junit</artifactId>
     26      <version>4.12</version>
     27      <classifier>javadoc</classifier>
     28     </dependency>  
     29     
     30     <!-- DBCP&OJDBC -->
     31     <dependency>
     32      <groupId>commons-dbcp</groupId>
     33      <artifactId>commons-dbcp</artifactId>
     34      <version>1.4</version>
     35     </dependency> 
     36     
     37     <dependency>
     38      <groupId>com.oracle</groupId>
     39      <artifactId>ojdbc6</artifactId>
     40      <version>11.2.0.1.0</version>
     41     </dependency>     
     42     
     43    <!-- 导入myBatis的包 -->
     44     <dependency>
     45      <groupId>org.mybatis</groupId>
     46      <artifactId>mybatis</artifactId>
     47      <version>3.4.0</version>
     48     </dependency>
     49    
     50     <dependency>
     51      <groupId>org.mybatis</groupId>
     52      <artifactId>mybatis</artifactId>
     53      <version>3.2.8</version>
     54      <classifier>sources</classifier>
     55     </dependency>
     56    
     57     <dependency>
     58      <groupId>org.mybatis</groupId>
     59      <artifactId>mybatis</artifactId>
     60      <version>3.2.8</version>
     61      <classifier>javadoc</classifier>
     62     </dependency>       
     63     
     64     <!-- MySql 驱动包-->
     65     <dependency>
     66      <groupId>mysql</groupId>
     67      <artifactId>mysql-connector-java</artifactId>
     68      <version>5.1.46</version>
     69     </dependency>    
     70     
     71    <!-- 导入myBatis-spring -->   
     72     <dependency>
     73      <groupId>org.mybatis</groupId>
     74      <artifactId>mybatis-spring</artifactId>
     75      <version>1.3.2</version>
     76     </dependency>
     77    
     78     <dependency>
     79      <groupId>org.mybatis</groupId>
     80      <artifactId>mybatis-spring</artifactId>
     81      <version>1.3.2</version>
     82      <classifier>sources</classifier>
     83     </dependency>
     84 
     85     <dependency>
     86      <groupId>org.mybatis</groupId>
     87      <artifactId>mybatis-spring</artifactId>
     88      <version>1.3.2</version>
     89      <classifier>javadoc</classifier>
     90     </dependency>
     91     
     92     <!-- 导入spring-MVC -->
     93     <dependency>
     94      <groupId>org.springframework</groupId>
     95      <artifactId>spring-webmvc</artifactId>
     96      <version>4.2.3.RELEASE</version>
     97     </dependency>
     98   
     99     <dependency>
    100      <groupId>org.springframework</groupId>
    101      <artifactId>spring-webmvc</artifactId>
    102      <version>4.2.3.RELEASE</version>
    103      <classifier>sources</classifier>
    104     </dependency>
    105   
    106     <dependency>
    107      <groupId>org.springframework</groupId>
    108      <artifactId>spring-webmvc</artifactId>
    109      <version>4.2.3.RELEASE</version>
    110      <classifier>javadoc</classifier>
    111     </dependency>    
    112     
    113    <!-- 针对Spring JDBC导包,MyBatis底层使用的Spring JDBC-->
    114     <dependency>
    115      <groupId>org.springframework</groupId>
    116      <artifactId>spring-jdbc</artifactId>
    117      <version>4.2.3.RELEASE</version>
    118     </dependency>
    119    
    120     <dependency>
    121      <groupId>org.springframework</groupId>
    122      <artifactId>spring-jdbc</artifactId>
    123      <version>4.2.3.RELEASE</version>
    124      <classifier>sources</classifier>
    125     </dependency>
    126    
    127     <dependency>
    128      <groupId>org.springframework</groupId>
    129      <artifactId>spring-jdbc</artifactId>
    130      <version>4.2.3.RELEASE</version>
    131      <classifier>javadoc</classifier>
    132     </dependency>      
    133     
    134   </dependencies>
    135   
    136 </project>

    在云笔记数据库中建立两张表(因为前面学习的是云笔记,所以把表建立在云笔记数据库里了)

    1 CREATE TABLE TRAN_EMP1(
    2     id int NOT NULL AUTO_INCREMENT,
    3     name VARCHAR(40) NOT NULL,
    4     age int,
    5     primary key(id)
    6 )
    7 ENGINE = InnoDB
    8 ;
    1 CREATE TABLE TRAN_EMP2(
    2     id int NOT NULL AUTO_INCREMENT,
    3     name VARCHAR(40) NOT NULL,
    4     age int,
    5     primary key(id)
    6 )
    7 ENGINE = InnoDB
    8 ;

    配置Spring-MVC.xml,需要配置组件扫描和注解驱动,由于本案例使用了三个包,因此都需要扫描纳入到Spring进行管理

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  
     4     xmlns:jee="http://www.springframework.org /schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
     5     xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xsi:schemaLocation="
     7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
     9         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
    10         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    11         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    12         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    13         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">    
    14         
    15         <!-- 配置组件扫描 -->
    16         <context:component-scan base-package="DAO"></context:component-scan>
    17         <context:component-scan base-package="Service"></context:component-scan>
    18         <context:component-scan base-package="LayerT"></context:component-scan>
    19         <!-- 添加注解驱动 -->
    20         <mvc:annotation-driven></mvc:annotation-driven>
    21     
    22         
    23 </beans>

    配置Spring-MyBatis的包,里面配置数据库连接和需要扫描的包含SQL的Mapper,以及交给Spring来创建SqlSessionFactoryBean和Mapper映射器具体实现,并配置好事务管理

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     3     xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  
     4     xmlns:jee="http://www.springframework.org /schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
     5     xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
     6     xsi:schemaLocation="
     7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
     8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
     9         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
    10         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
    11         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
    12         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    13         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">    
    14         
    15         <!-- 配置连接池 -->               
    16         <!-- 读取属性文件 -->
    17         <util:properties id="db" location="classpath:config/jdbc.properties">    
    18         </util:properties>
    19         <!-- 配置连接池,可以参考DBUtil的方法,这里采用spring创建连接池-->
    20         <!-- destroy-method方法作用是:当spring容器关闭后,会将连接释放回到连接池,避免资源浪费 -->
    21         <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    22          <property name="driverClassName" value="#{db.driver}"/>
    23          <property name="url" value="#{db.url}" />
    24          <property name="username" value="#{db.user}" />
    25          <property name="password" value="#{db.pwd}" />
    26          <property name="maxActive" value="#{db.maxactive}"></property>
    27         </bean>
    28         
    29         <!-- 配置SqlSessionFactoryBean -->
    30         <bean id="ssfb" class="org.mybatis.spring.SqlSessionFactoryBean">
    31          <!-- 指定连接资源 -->
    32          <property name="dataSource" ref="dataSource"/>
    33          <!-- 指定映射文件--> 
    34          <property name="mapperLocations" value="classpath:Mapper/*.xml"/> 
    35         </bean>
    36         
    37         <!-- 配置MapperScannerConfigurer -->
    38         <bean id="sst" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    39          <!--导入需要扫描的映射器包-->         
    40          <property name="basePackage" value="DAO"></property> 
    41         </bean>
    42         
    43         <!-- 要想让@Transactional注解生效,就需要配置如下两部分 -->
    44         <!-- 配置事务提交,声明式事务 -->
    45         <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    46           <property name="dataSource" ref="dataSource"></property><!-- 前面那个dataSource是类里有这个属性 -->
    47         </bean>
    48         <!-- 设置注解驱动的事务管理? -->
    49         <tx:annotation-driven transaction-manager="txManager"/>
    50 
    51 </beans>

    在我的Mapper.xml文件添加Sql,为插入员工信息

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <!DOCTYPE mapper
     3   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     4   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     5 
     6 <mapper namespace="DAO.EmpDAO">
     7   <!-- id要求唯一  parameterType:填写实体类的完整名字-->  
     8   
     9   <!-- 插入员工信息1 -->
    10   <insert id="addEMP1" parameterType="Entity.EMP">
    11       insert into tran_emp1(id,name,age) values(#{id},#{name},#{age})     
    12   </insert>
    13   
    14     <!-- 插入员工信息1 -->
    15   <insert id="addEMP2" parameterType="Entity.EMP">
    16       insert into tran_emp2(id,name,age) values(#{id},#{name},#{age})     
    17   </insert>
    18   
    19 </mapper>

    实体类+DAO+Service+LayerT层

    为EMP对象建立一个实体类,方便MyBatis使用。并建立访问数据库的DAO层,Service调用DAO层,并在访问数据库的方法上添加事务管理。LayerT调用Service层,其中Layer层中写具体的外层方法,包裹住有事务管理的Service层方法。最后在测试类中进行测试,查看不同事务注解的条件下执行结果。

    实体类

     1 package Entity;
     2 /**
     3  * 实体类
     4  * @author yangchaolin
     5  *
     6  */
     7 public class EMP {
     8       private int id;
     9       private String name;
    10       private int age;
    11                
    12     public EMP(String name, int age) {
    13         super();
    14         this.name = name;
    15         this.age = age;
    16     }
    17 
    18     public int getId() {
    19         return id;
    20     }
    21     public void setId(int id) {
    22         this.id = id;
    23     }
    24     public String getName() {
    25         return name;
    26     }
    27     public void setName(String name) {
    28         this.name = name;
    29     }
    30     public int getAge() {
    31         return age;
    32     }
    33     public void setAge(int age) {
    34         this.age = age;
    35     }
    36       
    37       
    38 }

     DAO层,建立EmpDAO接口,指向Mapper.xml映射文件。

     1 package DAO;
     2 
     3 import org.springframework.stereotype.Repository;
     4 
     5 import Entity.EMP;
     6 
     7 @Repository("empDAO")
     8 public interface EmpDAO {
     9       //插入员工方法1
    10       public int addEMP1(EMP emp);
    11       //插入员工方法2
    12       public int addEMP2(EMP emp);
    13 }

    Service层,建立了2个接口,一个针对表1,一个针对表2,并有具体的实现类,这里不展示接口内容,直接贴出实现类。

    第一个实现类只有插入一条员工信息的方法。第二个实现类除了插入员工信息的方法外,还有一个插入员工信息的方法,只是执行后会抛出RuntimeException

     1 package Service;
     2 
     3 import javax.annotation.Resource;
     4 
     5 import org.springframework.stereotype.Service;
     6 import org.springframework.transaction.annotation.Propagation;
     7 import org.springframework.transaction.annotation.Transactional;
     8 
     9 import DAO.EmpDAO;
    10 import Entity.EMP;
    11 
    12 @Service("service1")
    13 public class EMPService1Impl implements EMPService1{
    14     
    15     @Resource(name="empDAO")
    16     EmpDAO dao;
    17     
    18     @Transactional(propagation=Propagation.XXX)//传播类型待定
    19     public void addEmp1(EMP emp) {
    20         dao.addEMP1(emp);        
    21     }
    22 
    23 }
     1 package Service;
     2 
     3 import javax.annotation.Resource;
     4 
     5 import org.springframework.stereotype.Service;
     6 import org.springframework.transaction.annotation.Propagation;
     7 import org.springframework.transaction.annotation.Transactional;
     8 
     9 import DAO.EmpDAO;
    10 import Entity.EMP;
    11 
    12 @Service("service2")
    13 public class EMPService2Impl implements EMPService2{
    14     
    15     @Resource(name="empDAO")
    16     EmpDAO dao;
    17    
    18     @Transactional(propagation=Propagation.XXX)//传播类型待定
    19     public void addEmp2(EMP emp) {
    20         dao.addEMP2(emp);
    21     }
    22     
    23     @Transactional(propagation=Propagation.XXX)//传播类型待定
    24     public void addEmp2WithException(EMP emp) {
    25         dao.addEMP2(emp);
    26         throw new RuntimeException();
    27     }
    28 
    29 }

     LayerT层里,每个传播类型写了一个类进行测试,为了便于分类,将在后面一个个进行测试分析,这里只做好准备工作,最后测试项目结构如下图:

    参考博文:https://segmentfault.com/a/1190000013341344

  • 相关阅读:
    mysql 覆盖索引
    mysql 连接查询 和 子查询
    mysql varchar
    uchome 是如何将数据插入数据库的
    Tomcat5 在windows安装服务
    Linux中错误码及描述查看
    Longines浪琴手表型号解释
    perl 安装 Net::Telnet 模块
    php一些错误的显示问题
    firefox样式表cursor和兼容Ie firefox,解决文字溢出的问题(wordwrap:breakword;wordbreak:breakall)
  • 原文地址:https://www.cnblogs.com/youngchaolin/p/10613924.html
Copyright © 2011-2022 走看看