事务管理
PlatformTransactionManager:平台事务管理
根据不同的平台实现事务的管理
(1)DataSourceTransactionManager:底层使用的是jdbc
(2)HibernateTransactionManager:底层使用的是Hibernate
TransactionDefination:隔离级别,传播行为,是否只读等
TransactionStatus:事务管理过程中的事务的状态(是否提交等)
事务的传播行为
Spring中提供了七种事务的传播行为。
传播行为的用途:用来解决复杂的业务逻辑之间的互相调用的问题(不同的业务逻辑中有不同的事务)。
三种:假设在调用的过程中
(1)多个操作在同一个事务中
REQUIRED:A中有事务,使用A的事务,如果A中没有事务,那么就重新创建一个事务。
SUPPORTES:如果A中没有事务,就不重新创建事务,如果有事务的话,使用A的事务
MANDATORY:如果A中有事务,使用事务,如果没有事务的话,就抛出异常。
(2)多个操作不在同一个事务中
REQUIRES_NEW:如果A中有事务的话,就将事务挂起(不使用)创建新的的事务,只包含自己的操作,如果没有的话,也是创建一个新的事务只包含自身的操作。
NOT_SUPPORTED:如果A中有事务的话,将A中的事务挂起。不使用事务管理。
NEVER:如果有事务的话,就报错。
(3)嵌套
:如果A中有事务,就执行,执行完成后设置一个保存点,执行保存点之后,如果没有报异常的话,那么就通过,如果报了异常的话,就执行回滚的操作。(可以回滚到保存点之前,也可以回滚在保存点之后)
Spring事务管理搭建
应用场景:账户的转账
接口
(1)Dao层的接口
/**
* @author kiosk
*/
public interface AccountDao {
/**
* 转入
* @param from
* @param money
*/
void inmoney(String from, Double money);
/**
* 转出
* @param to
* @param money
*/
void outmoney(String to, Double money);
}
(2)Service层的接口
/**
* 转账业务的接口
* @author kiosk
*/
public interface AccountService {
/**
* 转账
* @param from
* @param to
* @param money
*/
void transfor(String from ,String to,Double money);
}
实现类
(1)Dao层的实现类
新知识:为了在实现类中免去了复杂的jdbc模板的配置,可以在Dao的实现类上继承一个JdbcDaoSupport类,这样就只用在xml文件中配置一个数据源即可(自动的创建jdbc的模板)。
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
public void inmoney(String to, Double money) {
this.getJdbcTemplate().update("update account set money = money + ? where name = ?",money,to);
}
public void outmoney(String from, Double money) {
this.getJdbcTemplate().update("update account set money = money - ? where name = ?",money,from);
}
}
(2)Service层的实现类
/**
* 转账的实现类
* @author kiosk
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao){
this.accountDao = accountDao;
}
/**
*
* @param from 转出账号
* @param to 转入账号
* @param money 金额
*/
public void transfor(String from, String to, Double money) {
accountDao.inmoney(to,money);
accountDao.outmoney(from,money);
}
}
xml文件配置
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="AccountServiceImpl" class="com.tx.demo1.AccountServiceImpl">
<property name="accountDao" ref="AccountDaoImpl"/>
</bean>
<bean id="AccountDaoImpl" class="com.tx.demo1.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClass" value="${jdbc.driver}"/>
</bean>
<!--继承一个JdbcDaoSupport之后,就可以直接在AccountDaoImpl中配置一个数据源,继承的这个类会创建出一个模板-->
<!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">-->
<!--<property name="dataSource" ref="dataSource"/>-->
<!--</bean>-->
</beans>
测试
import com.tx.demo1.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:SpringTx.xml")
public class SpringTx {
@Resource(name = "AccountServiceImpl")
private AccountService accountService;
@Test
public void test1() {
accountService.transfor("hello", "linux", 2000d);
}
}
编程式事务(手动实现事务管理)
(1)配置事务管理器
(2)配置事务模板
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="AccountServiceImpl" class="com.tx.demo1.AccountServiceImpl">
<property name="accountDao" ref="AccountDaoImpl"/>
<property name="template" ref="transactionTemplate"/>
</bean>
<bean id="AccountDaoImpl" class="com.tx.demo1.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClass" value="${jdbc.driver}"/>
</bean>
<!--继承一个JdbcDaoSupport之后,就可以直接在AccountDaoImpl中配置一个数据源,继承的这个类会创建出一个模板-->
<!--<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">-->
<!--<property name="dataSource" ref="dataSource"/>-->
<!--</bean>-->
<!--配置事务管理器-->
<bean id="dataSourceTransactionManager" 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="dataSourceTransactionManager"/>
</bean>
</beans>
(3)代码实现事务
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
/**
* 转账的实现类
*
* @author kiosk
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
/**
* @param from 转出账号
* @param to 转入账号
* @param money 金额
*/
private TransactionTemplate template;
public void setTemplate(TransactionTemplate transactionTemplate) {
this.template = transactionTemplate;
}
public void transfor(final String from, final String to, final Double money) {
template.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
accountDao.inmoney(to, money);
int s = 1 / 0;
accountDao.outmoney(from, money);
}
});
}
}
声明式事务管理(使用的是AOP思想)
(1)声明式事务之XML方式实现
利用上面的转账的例子(使用声明式事务)
持久层的接口
/**
* @author kiosk
*/
public interface AccountDao {
/**
* 转入
* @param from
* @param money
*/
void inmoney(String from, Double money);
/**
* 转出
* @param to
* @param money
*/
void outmoney(String to, Double money);
}
持久层的实现类
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
public void inmoney(String to, Double money) {
this.getJdbcTemplate().update("update account set money = money + ? where name = ?",money,to);
}
public void outmoney(String from, Double money) {
this.getJdbcTemplate().update("update account set money = money - ? where name = ?",money,from);
}
}
业务层的接口
/**
* 转账业务的接口
* @author kiosk
*/
public interface AccountService {
/**
* 转账
* @param from
* @param to
* @param money
*/
void transfor(String from, String to, Double money);
}
业务层的实现类
/**
* 转账的实现类
*事务增强的配置
* @author kiosk
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
public void transfor(final String from, final String to, final Double money) {
accountDao.inmoney(to, money);
int s = 1 / 0;
accountDao.outmoney(from, money);
}
}
xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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="AccountServiceImpl" class="com.tx.demo2.AccountServiceImpl">
<property name="accountDao" ref="AccountDaoImpl"/>
</bean>
<bean id="AccountDaoImpl" class="com.tx.demo2.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClass" value="${jdbc.driver}"/>
</bean>
<!--事务管理器的配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事务增强的配置-->
<tx:advice transaction-manager="transactionManager" id="interceptor">
<!--事务管理的规则-->
<tx:attributes>
<tx:method name="transfor*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--AOP的配置-->
<aop:config>
<aop:pointcut id="pointcut1" expression="execution(* com.tx.demo2.AccountServiceImpl.transfor(..))"/>
<aop:advisor advice-ref="interceptor" pointcut-ref="pointcut1"/>
</aop:config>
</beans>
(2)声明式事务的注解的方式
在xml的配置文件配置自动注解
在以上的业务层的实现类上加上@Transaction注解