事务
事务指的是逻辑上的一组操作,这组操作要么全部成功,要么全部失败。
特性:原子性,一致性,隔离性,持久性
原子性指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不发生。
一致性指事务前后数据的完整性必须保持一致。
隔离性指多个用户并发访问数据库时,一个用户的事务不能被其他用户的事务所干扰,多个并发事务之间数据要相互隔离。
持久性指一个事务一旦被提交,它对数据库中数据的改变就是永久性的,即使数据库发生故障也不应该对其有任何影响。
Spring事务管理高层抽象主要包括3个接口:
PlatformTransactionManager 事务管理器
TransactionDefinition 事务定义信息(隔离、传播、超时、只读)
TransactionStatus 事务具体运行状态
事务 | 说明 |
---|---|
org.springframework.jdbc.datasource.DataSourceTransactionManager | 使用Spring JDBC或iBatis进行持久化数据时使用 |
org.springframework.orm.hibernate3.HibernateTransactionManager | 使用Hibernate3.0版本进行持久化数据时使用 |
事务传播行为类型 | 说明 |
---|---|
PROPAGATION_REQUIRED | 支持当前事务,如果不存在,就新建一个 |
PROPAGATION_SUPPORTS | 支持当前事务,如果不存在,就不使用事务 |
PROPAGATION_MANDATORY | 支持当前事务,如果不存在,就抛出异常 |
PROPAGATION_REQUIRES_NEW | 如果有事务的存在,挂起当前事务,创建一个新的事务 |
PROPAGATION_NOT_SUPPORTED | 以非事务的方式运行,如果有事物存在,挂起当前事务 |
PROPAGATION_NEVER | 以非事务的方式运行,如果有事物存在,就抛出异常 |
PROPAGATION_NESTED | 如果当前事务存在,则嵌套事务执行 |
eg:以转账情况为案例
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String in, String out, Double money) {
accountDao.inMoney(in, money);
// int a=10/0;
accountDao.outMoney(out, money);
}
}
import org.springframework.jdbc.core.support.JdbcDaoSupport;
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);
}
}
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<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="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="accountDao" class="com.lmr.transaction.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<bean id="accountService" class="com.lmr.transaction.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean>
</beans>
@RunWith(BlockJUnit4ClassRunner.class)
public class TestAccount extends UnitTestBase{
public TestAccount() {
super("classpath:spring-transaction.xml");
}
@Test
public void TestTransfer(){
AccountService service=(AccountService)super.getBean("accountService");
service.transfer("aaa", "bbb", 200.0);
}
}
运行测试类后,如果转账时没有发生异常,系统会正常进行。若是发生异常时:int a=10/0;这个语句会报异常,则钱就会被转丢,只执行了前面的accountDao.inMoney(in, money);而没有执行后面的accountDao.outMoney(out, money);
下面通过四种方式,来实现事务管理。
编程式事务管理:注入事务管理模版,并手动修改源代码。
<!-- 事务管理器,并引入连接池 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 事务管理的模版 -->
<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager" />
</bean>
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
//注入事务管理的模版
private TransactionTemplate transactionTemplate;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
@Override
public void transfer(String in, String out, Double money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
// TODO Auto-generated method stub
accountDao.inMoney(in, money);
int a=10/0;
accountDao.outMoney(out, money);
}
});
}
}
声明式事务管理:基于TransactionProxyFactoryBean的方式,需要对每个业务类都配置一个代理类
<!-- 事务管理器,并引入连接池 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 业务层的代理 -->
<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 :事务的传播行为
* ISOTATION :事务的隔离级别
* readOnly :只读
* -EXCEPTION :发生哪些异常回滚事务
* +EXCEPTION :发生哪些异常不回滚事务 -->
<!-- key就是要执行的方法名 -->
<prop key="transfer">PROPAGATION_REQUIRED</prop>
<!-- <prop key="transfer">PROPAGATION_REQUIRED,readOnly</prop> -->
<!-- <prop key="transfer">PROPAGATION_REQUIRED,+java.lang.ArithmeticException</prop> -->
</props>
</property>
</bean>
声明式事务管理:基于AspectJ的XML方式,配置事务的增强,配置切面,并将其注入。
<!-- 事务管理器,并引入连接池 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务的的通知:事务的增强 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- name就是要执行的方法名 -->
<tx:method name="transfer" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* com.lmr.transaction.AccountService+.*(..))" id="pointCut" />
<!-- 配置切面:注入增强 -->
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="pointCut" />
</aop:config>
声明式事务管理:基于注解的方式,开启注解事务,在业务类上加注解@Transactional
<!-- 事务管理器,并引入连接池 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Transactional(propagation=Propagation.REQUIRED)
public class AccountServiceAnnotationImpl implements AccountService{
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String in, String out, Double money) {
accountDao.inMoney(in, money);
// int a=1/0;
accountDao.outMoney(out, money);
}
}