Spring 学习其四:Spring 的事务管理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Spring 学习其四:Spring 的事务管理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3696字,纯文字阅读大概需要6分钟。
内容图文
![Spring 学习其四:Spring 的事务管理](/upload/InfoBanner/zyjiaocheng/1295/21ed856357254cad921807f6422d7f31.jpg)
一、对事务的理解
我对事务的通俗理解就是,将一系类的数据库操作绑定在了一起,使得它们中只要一个无法执行成功,其他的也跟着被撤回。而且事务是可以一层包着一层的。这篇文章是我为了更好的理解 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
内容总结
以上是互联网集市为您收集整理的Spring 学习其四:Spring 的事务管理全部内容,希望文章能够帮你解决Spring 学习其四:Spring 的事务管理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。