Spring 事务管理
来源:互联网 发布:网络信息工作有哪些 编辑:程序博客网 时间:2024/06/05 05:10
1、PlatformTransactionManager
其中DataSourceManager用于spring JDBC或iBatis进行持久化数据时
HibernateTransactionManager用于Hibernate3.0进行持久化数据时
JpaTransactionManager
JdoTransactionManager
JtaTransactionManager
2、TransactionDefinition
如果不考虑事务的隔离级别,可能会导致脏读、不可重复读、幻读。
脏读:一个事务读取了另一个事务改写但还未提交的数据,如果这些数据被回滚,则读到的数据是无效的。
不可重复读:在同一个事务中,多次读取同一数据返回的结果有所不同。
幻读:一个事务读取了几行记录后,另一个事务插入了一些记录,幻读就发生了,再后来的查询中,第一个事务就会发现有些原来没有的记录。
事务隔离级别
DEFAULT 底层数据库默认的隔离级别,比如Mysql默认是REPEATABLE_READ
READ_UNCONNITED 允许读取还未提可能交的数据,会导致脏读、不可重复读、幻读。
READ_COMMITTED 允许,在并发事务已经提交后读取,可以防止脏读,但不可重复读,和幻读还会发生
REPEATABLE_READ 对相同字段多除非数据被事务改变。次读取的数据是一致的,可防止脏读和不可重复读,但幻读仍可发生。
SERIALIZABLE 完全服从数据库的ACID原则,不会发生脏读、不可重复读、幻读。在所以隔离级别中最慢。
事务的传播行为(七种)
web->service层->Dao层
Dao1(),Dao2()
事务是加在业务层的,service中的方法可能会调用到Dao层的多个方法。也可能会出现service层多个方法调用Dao层多个方法。然后web层同时调用service层不同方法。这时候就要确定这时候使用的是哪个事务,即事务是如何传播的。
第一类:web层调用的不同方法在同一事务中进行
1)PROPAGATION_REQUIRED:支持当前事务,如果不存在就创建一个事务,即不同的调用在同一个事务中
2)PROPAGATION_SUPPORTS:支持当前事务,如果不存在,就不使用事务
3)PROPAGATION_MANDATORY:支持当前事务,如果不存在,抛出异常。
第二类:web层调用的不同方法在不同事务中进行
4)PROPAGATION_REQUIRES_NEW:如果有事务存在,就挂起当前事务,创建一个新事务。即不同方法在不同事务中进行
5)PROPAGATION_NOT_SUPPORTED:以非事务方式运行,如果有事务就挂起当前事务
6)PROPAGATION_NEVER:以非事务方式运行,如果有事务存在,就抛出异常
第三类:web层调用的不同方法在前面的方法中使用的事务设置保存点,第二个方法在发生异常时即可以回到保存点的位置,也可以回到最初始的状态。
7)PROPAGATION_NESTED:如果当前事务存在,则嵌套事务执行
3、TransactionStatus
在该接口中提供了一些方法获取事务的状态
比如haveSavePoint();是否有保存点
isComplete();是否完成
等等
Spring 事务管理
-编程式的事务管理:这种方式在实际应用中很少使用,通过TransactionTemplate手动管理事务。
-声明式的事务管理:开发中一般使用这种方式,spring的声明式事务是通过AOP实现的。
新建一个web项目进行spring事务的测试
新建表Account
CREATE TABLE `account` (
`id` int(11) NOT NULL auto_increment,
`account` varchar(10) collate utf8_bin default NULL,
`money` double default NULL,
KEY `id` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
项目中引入必要的Jar包
包括c3p0连接池的jar包c3p0-0.9.2.1.jar
spring-jdbc-4.0.2.RELEASE.jar
spring-test-4.0.2.RELEASE.jar
spring-tx-4.0.2.RELEASE.jar
spring-beans-4.0.2.RELEASE.jar
spring-context-4.0.2.RELEASE.jar
spring-core-4.0.2.RELEASE.jar
spring-expression-4.0.2.RELEASE.jar
mysql-connector-java-5.1.30.jar
commons-logging-1.1.3.jar
log4j-1.2.17.jar
junit-4.11.jar
hamcrest-core-1.3.jar
spring-aop-4.0.2.RELEASE.jar
mchange-commons-java-0.2.3.4.jar
日志的配置文件log4j.properties
#定义LOG输出级别log4j.rootLogger=debug#定义日志输出目的地为控制台log4j.appender.Console=org.apache.log4j.ConsoleAppenderlog4j.appender.Console.Target=System.out#可以灵活地指定日志输出格式,下面一行是指定具体的格式log4j.appender.Console.layout = org.apache.log4j.PatternLayoutlog4j.appender.Console.layout.ConversionPattern=[%c] - %m%n#文件大小到达指定尺寸的时候产生一个新的文件log4j.appender.File = org.apache.log4j.RollingFileAppender#指定输出目录log4j.appender.File.File = logs/ssm.log#定义文件最大大小log4j.appender.File.MaxFileSize = 10MB# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志log4j.appender.File.Threshold = ALLlog4j.appender.File.layout = org.apache.log4j.PatternLayoutlog4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
核心配置文件applicationContext.xml
<?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:jee="http://www.springframework.org/schema/jee"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"><!-- 引入外部配置文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置c3p0连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- 配置业务类--> <bean id="accountService" class="com.terisadeng.spring.demo1.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!-- 配置Dao层 --> <bean id="accountDao" class="com.terisadeng.spring.demo1.dao.impl.AccountDaoImpl"> <!-- 注入连接池对象,来获取jdbc模板类操作数据库 --> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
jdbc属性文件jdbc.properties
jdbc.driverClass=com.mysql.jdbc.driverjdbc.url=jdbc:mysql:///spring_transactionjdbc.username=rootjdbc.password=root
Service层代码
package com.terisadeng.spring.demo1.service.impl;import com.terisadeng.spring.demo1.dao.AccountDao;import com.terisadeng.spring.demo1.service.AccountService;public class AccountServiceImpl implements AccountService{ private AccountDao accountDao; @Override public void transfer(String out, String in, double money) { accountDao.outMoney(out, money); accountDao.inMoney(in, money); } public AccountDao getAccountDao() { return accountDao; } public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; }}Dao层代码
package com.terisadeng.spring.demo1.dao.impl;import org.springframework.jdbc.core.support.JdbcDaoSupport;import com.terisadeng.spring.demo1.dao.AccountDao;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{ @Override public void outMoney(String out, double money) { String sql="update Account set money=money-? where name=?"; this.getJdbcTemplate().update(sql, money,out); } @Override public void inMoney(String in, double money) { String sql="update Account set money=money+? where name=?"; this.getJdbcTemplate().update(sql, money,in); }}
测试类代码
import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.terisadeng.spring.demo1.service.AccountService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class SpringDemo1{ @Resource(name="accountService") private AccountService accountService; @Test public void demo1(){ accountService.transfer("aaa", "bbb", 200); }}
1、代码中使用声明式的事务管理
applicationContext.xml添加配置
<bean id=transactionManager class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!--引入数据源--><property name="dataSource" ref="dataSource"></property></bean><!-- 配置事务管理的模板,简化事务操作 --><bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate"><property name="transactionManager" ref="transactionManager"></property></bean>因为事务是在业务层控制的,这里使用事务模板简化事务的操作,就要将事务的模板对象引入service层,所以修改service层配置<bean id="accountService" class="com.terisadeng.spring.demo1.service.impl.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property><property name="transactionTemplate" ref="transactionTemplate"></property></bean>service层代码修改如下
package com.terisadeng.spring.demo1.service.impl;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.TransactionCallbackWithoutResult;import org.springframework.transaction.support.TransactionTemplate;import com.terisadeng.spring.demo1.dao.AccountDao;import com.terisadeng.spring.demo1.service.AccountService;public class AccountServiceImpl implements AccountService{ private AccountDao accountDao; private TransactionTemplate transactionTemplate; @Override public void transfer(final String out,final String in,final double money) { transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus arg0) { accountDao.outMoney(out, money); int i=1/1; accountDao.inMoney(in, money); } }); } public AccountDao getAccountDao() { return accountDao; } public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } public void setTransactionTemplate(TransactionTemplate transactionTemplate) { this.transactionTemplate = transactionTemplate; }}2、代码中使用XML进行声明式的事务管理
applicationContext.xml配置修改如下:
<?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:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"> <!-- 引入外部配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 配置c3p0连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置业务类--> <bean id="accountService" class="com.terisadeng.spring.demo2.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!-- 配置Dao层 --> <bean id="accountDao" class="com.terisadeng.spring.demo2.dao.impl.AccountDaoImpl"> <!-- 注入连接池对象,来获取jdbc模板类操作数据库 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 对业务层进行事务的管理,使用代理来对service进行增强 --> <bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <!-- 配置目标对象 --> <property name="target" ref="accountService"></property> <!--注入事务管理器--> <property name="transactionManager" ref="transactionManager"></property> <!-- 注入事务的属性 --> <property name="transactionAttributes"> <props> <!-- prop的格式: * PROPAGATION:事务的传播行为。 * ISOLATION:事务的隔离级别。 * readOnly:只读。 * -Exception:发生哪些异常回滚事务。 * +Exception :发生哪些异常事务不回滚。 --> <!-- 这里写transfer方法是因为业务层只有这一个方法需要进行事务管理,实际开发可以根据需要定义,比如*代表所有方法 --> <prop key="transfer">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans>Service层修改如下:
package com.terisadeng.spring.demo2.service.impl; import com.terisadeng.spring.demo2.dao.AccountDao; import com.terisadeng.spring.demo2.service.AccountService; public class AccountServiceImpl implements AccountService { private AccountDao accountDao; @Override public void transfer( String out, String in, double money) { accountDao.outMoney(out, money); int d=1/0; accountDao.inMoney(in, money); } public AccountDao getAccountDao() { return accountDao; } public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } }测试类代码如下:
import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.terisadeng.spring.demo2.service.AccountService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext2.xml")public class SpringDemo2{ /** * 注入代理类:因为代理类进行了增强,包含事务操作 */ @Resource(name="accountServiceProxy") //@Resource(name="accountService") private AccountService accountService; @Test public void demo2(){ accountService.transfer("aaa", "bbb", 200); }}3、Spring的声明式事务管理的第二种方式(基于AspectJ的XML方式的配置)
applicationContext.xml修改如下:
<?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:jee="http://www.springframework.org/schema/jee"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"><!-- 引入外部配置文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置c3p0连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- 配置业务类--> <bean id="accountService" class="com.terisadeng.spring.demo3.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!-- 配置Dao层 --> <bean id="accountDao" class="com.terisadeng.spring.demo3.dao.impl.AccountDaoImpl"> <!-- 注入连接池对象,来获取jdbc模板类操作数据库 --> <property name="dataSource" ref="dataSource"></property> </bean><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 配置事务的通知:(事务的增强) --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- propagation:事务的传播行为isolation :事务的隔离级别read-only :只读。rollback-for:发生哪些异常回滚no-rollback-for:发生哪些异常不回滚。timeout :过期信息 --><tx:method name="transfer" propagation="REQUIRED"/></tx:attributes></tx:advice><!-- 配置切面 --><aop:config><!-- 配置切入点 --><aop:pointcut expression="execution(* com.terisadeng.spring.demo3.service.AccountService+.*(..))" id="pointcut1"/><!-- 配置切面 --><aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/></aop:config></beans>Service层与Dao不需要修改任何东西,这种方式的代理对象是自动生成的
测试代码如下:
import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.terisadeng.spring.demo3.service.AccountService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext3.xml")public class SpringDemo3{ @Resource private AccountService accountService; @Test public void demo3(){ accountService.transfer("aaa", "bbb", 200); }}4、基于注解方式的配置
applicationContext.xml配置如下:
<?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:jee="http://www.springframework.org/schema/jee"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"><!-- 引入外部配置文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置c3p0连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property></bean><!-- 配置业务类--> <bean id="accountService" class="com.terisadeng.spring.demo4.service.impl.AccountServiceImpl"> <property name="accountDao" ref="accountDao"></property> </bean> <!-- 配置Dao层 --> <bean id="accountDao" class="com.terisadeng.spring.demo4.dao.impl.AccountDaoImpl"> <!-- 注入连接池对象,来获取jdbc模板类操作数据库 --> <property name="dataSource" ref="dataSource"></property> </bean><!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"></property></bean><!-- 开启注解事务 --><tx:annotation-driven transaction-manager="transactionManager"/></beans>Service层代码修改如下:
package com.terisadeng.spring.demo4.service.impl;import org.springframework.transaction.annotation.Isolation;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import com.terisadeng.spring.demo4.dao.AccountDao;import com.terisadeng.spring.demo4.service.AccountService;/** * @Transaction注解中的属性: * propagation :事务的传播行为 * isolation :事务的隔离级别 * read-Only :只读 * rollback-for :发生哪些异常回滚 * no-rollback-for:发生哪些异常不回滚 * */@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)public class AccountServiceImpl implements AccountService{ private AccountDao accountDao; @Override public void transfer( String out, String in, double money) { accountDao.outMoney(out, money); int d=1/0; accountDao.inMoney(in, money); } public AccountDao getAccountDao() { return accountDao; } public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } }测试代码如下:
import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.terisadeng.spring.demo4.service.AccountService;//基于注解的事务配置方式@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext4.xml")public class SpringDemo4{ @Resource private AccountService accountService; @Test public void demo4(){ accountService.transfer("aaa", "bbb", 200); }}Spring将事务管理分成了两类:
*编程式事务管理:
*手动编写代码进行事务管理.(很少使用)
*声明式事务管理:
*基于TransactionProxyFactoryBean的方式.(很少使用)
*需要为每个进行事务管理的类配置一个TransactionProxyFactoryBean进行增强.
*基于AspectJ的XML方式.(经常使用)
*一旦配置好之后,类上不需要添加任何东西
*基于注解方式.(经常使用)
*配置简单,需要在业务层类上添加一个@Transactional的注解.
- 事务管理之Spring事务管理
- Spring事务管理
- spring事务管理
- Spring事务管理
- Spring事务管理
- Spring事务管理
- Spring事务管理
- Spring事务管理
- Spring 事务管理
- Spring事务管理
- spring 事务管理
- spring 事务管理
- spring 事务管理
- spring事务管理
- Spring 事务管理
- Spring事务管理
- spring事务管理
- spring 事务管理
- OpenvSwitch概念和原理
- webpack
- 树
- CentOS6.X升级GCC,手动安装GCC4.8.2
- # 回归、拟合算法心得
- Spring 事务管理
- Angular2项目中浏览器拦截弹出窗口的解决方法
- 资产盘点数据采集器 扫描枪(器)PDA应用APP
- Mysql分页&关联查询优化
- String字符串中的换行,制表符等处理
- 深度优先遍历
- css3 float排序问题
- java开发webservice几种方式小结
- layer插件select选中默认值