Spring 学习其四:Spring 的事务管理

一、对事务的理解

我对事务的通俗理解就是,将一系类的数据库操作绑定在了一起,使得它们中只要一个无法执行成功,其他的也跟着被撤回。而且事务是可以一层包着一层的。这篇文章是我为了更好的理解 Spring 的事务管理机制做的笔记。

Spring 的事务管理,采用的方案类似于它的 AOP,即通过切面把我们的数据库操作包裹起来,当被包裹的操作发生异常时,回滚事务,没有异常则提交事务。

二、配置事务管理器

使用 Spring + MyBatis 的框架时的配置方案,首先是先配置好数据库连接池(参考前面的文章),现在我的 Spring-cfg 文档内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache" 
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/cache 
     http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
    <bean class = "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name = "locations">
        <list>
          <value>classpath:jdbc.properties</value>
          <value>classpath:log4j2.properties</value>
        </list>
      </property>
       <property name = "ignoreResourceNotFound" value = "true"/>
    </bean>
    <bean id = "dataSource" class = "org.apache.commons.dbcp2.BasicDataSource">
      <property name = "driverClassName" value = "${database.driver}"/>
      <property name="url" value="${database.url}"/>
      <property name="username" value="${database.username}"/>
      <property name="password" value="${database.password}"/>
      <property name="maxTotal" value="${database.maxtotal}"/>
      <property name="maxIdle" value="${database.maxidle}"/>
      <property name="maxWaitMillis" value="${database.maxwaitmillis}"/>
    </bean>
    
    <bean id = "sqlSessionFactoryBean" class = "org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref = "dataSource"/>
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="mapperLocations">
      <list>
        <value>classpath*:com/**/*Mapper.xml</value>
      </list>
    </property>
  </bean>
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.**.dao" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
    <property name="annotationClass" value="org.springframework.stereotype.Repository"/>
  </bean>
</beans>

在此基础之上,需要添加数据源事务管理器:

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

使用事务的方式有很多,我们只了解最方便,也是最常用的方式:@Transactional 注解,该注解的属性如下:

配置项 含义 备注
value 定义事务管理器 Spring IOC 容器里的一个 Bean id,这个Bean 需要实现接口 PlatformTransactionManager
transactionManager  同上 同上
isolation 隔离级别  
propagation 传播行为  
timeout   超时时间 单位为秒,超时后,会抛出异常,导致回滚
readOnly 是否开启只读事务  
rollbackFor 回滚事务的异常类定义 默认是所有异常
rollbackForClassname 回滚事务的异常名定义  
noRollbackFor 哪些异常不回滚  
noRollbackForClassName   哪些异常不回滚(根据名称匹配)  

为了能够使用这个注解,还需要在 spring-cfg 中添加启用该注解的说明:

<tx:annotation-driven transaction-manager="transactionManager"/>

当我们相拥使用事务是,给对应的方法添加 @Transactional 注解即可:

扫描二维码关注公众号,回复: 3436958 查看本文章

猜你喜欢

转载自www.cnblogs.com/JiKio/p/9746617.html
今日推荐