javaEE Spring,JDBC事务,管理事务的方式之编码式(不常用),TransactionTemplate

Spring为不同的平台管理事务,提供了统一接口 PlatformTransactionManager接口(事务管理器,封装了对事务的操作)
SpringJDBC和iBatis使用DataSourceTransactionManager实现类
Hibernate使用HibernateTransactionManager实现类


src/applicationContext.xml(Spring配置文件,管理事务):

<?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: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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
	
	<!-- 指定spring读取db.properties配置 -->
	<context:property-placeholder location="classpath:db.properties"  />
	
	<!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
		<property name="dataSource" ref="dataSource" ></property>
	</bean>
	
	<!-- 事务模板对象 -->
	<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" >
		<property name="transactionManager" ref="transactionManager" ></property>
	</bean>
	
	
	<!-- 1.连接池 -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property>
		<property name="driverClass" value="${jdbc.driverClass}" ></property>
		<property name="user" value="${jdbc.user}" ></property>
		<property name="password" value="${jdbc.password}" ></property>
	</bean>
	
	<!-- 2.Dao-->
	<bean name="accountDao" class="cn.xxx.dao.AccountDaoImpl" >
		<property name="dataSource" ref="dataSource" ></property>
	</bean>
	
	<!-- 3.Service-->
	<bean name="accountService" class="cn.xxx.service.AccountServiceImpl" >
		<property name="ad" ref="accountDao" ></property>
		<property name="tt" ref="transactionTemplate" ></property>
	</bean>  

</beans>

AccountServiceImpl.java(Service层):

package cn.xxx.service;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import cn.xxx.dao.AccountDao;

public class AccountServiceImpl implements AccountService {

	private AccountDao ad;  // Dao层对象
	private TransactionTemplate tt;

	@Override
	public void transfer(final Integer from,final Integer to,final Double money) { //匿名内部类中使用,需要final修饰 
		
		//通过编码式管理事务的缺点是:在多个需要事务的方法中需要重复下面的代码。
		tt.execute(new TransactionCallbackWithoutResult() {
			@Override
			//执行需要事务的数据库操作
			protected void doInTransactionWithoutResult(TransactionStatus arg0) {
				//减钱
				ad.decreaseMoney(from, money);
				//int i = 1/0;  // 手动模拟异常
				//加钱
				ad.increaseMoney(to, money);
			}
		});
		
	}

	public void setAd(AccountDao ad) {
		this.ad = ad;
	}

	public void setTt(TransactionTemplate tt) {
		this.tt = tt;
	}
	
}

猜你喜欢

转载自blog.csdn.net/houyanhua1/article/details/82055733