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 注解即可:

 

转载于:https://www.cnblogs.com/JiKio/p/9746617.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值