spring事務管理

代碼運行環境搭建

spring一系列jar包需要準備齊全

AccountDao.java(接口)

package com.my.spring.transaction.test;

public interface AccountDao {
	
	/**
	 * @param in	:轉出賬戶
	 * @param money	:轉出金額
	 */
	public void inMoney(String in,double money);
	
	public void outMoney(String out,double money);
	
}
AccountDaoImpl.java實現類

package com.my.spring.transaction.test;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{
	
	@Override
	public void inMoney(String in, double money) {
		String sql = "update account set money = money + ? where username = ?";
		this.getJdbcTemplate().update(sql, money, in);
	}

	@Override
	public void outMoney(String out, double money) {
		String sql = "update account set money = money - ? where username = ?";
		this.getJdbcTemplate().update(sql, money, out);
	}

}
AccountService.java(接口)

package com.my.spring.transaction.test;

public interface AccountService {
	
	/**
	 * @param in	:轉出賬號
	 * @param out	:轉入賬號
	 * @param money	:轉賬金額
	 */
	public void transfer(String in,String out,double money);
	
	
}
AccountServiceImpl.java實現類

package com.my.spring.transaction.test;

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;
	
	//聲明式事務管理
	@Override
	public void transfer(String in, String out, double money) {
		accountDao.outMoney(out, money);
		//int i = 1/0;
		accountDao.inMoney(in, money);
	}
        //spring注入
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
}

spring配置

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 加載配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 數據庫連接池 -->
	<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>
	
	<bean id="accountService" class="com.my.spring.transaction.test.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"></property>
	</bean>
	
	<bean id="accountDao" class="com.my.spring.transaction.test.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	 <!-- 事務管理器 -->
	 <bean id="transactionManager" 
	 		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	 	<property name="dataSource" ref="dataSource"/>
	 </bean>
	
</beans>
TransactionTest.java
package com.my.spring.transaction.test1;

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;

/**
 * @author lhd
 *	案例測試
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring-context.xml")
public class TransactionTest {
	
	@Resource(name="accountService")
	private AccountService accountService;
	
	
	@Test
	public void demo1(){
		accountService.transfer("aaa", "bbb", 10);
	}
}


數據庫設計

代碼描述:在兩個用戶之間轉賬,如果因爲一些原因使得代碼中途不能繼續執行,就可能出現一個用戶金額減少,而另一個用戶金額未增加,使用org.springframework.jdbc.datasource.DataSourceTransactionManager來管理轉賬問題,實現轉賬代碼原子性(將轉賬代碼看作一個整體),也就一旦出現轉賬錯誤就會事務回滾。舉個例子就是在transfer實現方法中兩個inMoney和outMoney中加一個i = 1/0,這裏出現算數異常就不會向下執行代碼,從而使得一個用戶轉出錢,一個用戶沒收到錢。

編程式事務管理

編程式事務管理,由java代碼實現

spring配置文件修改

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 加載配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 數據庫連接池 -->
	<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>
	
	<bean id="accountService" class="com.my.spring.transaction.test.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"></property>
		<property name="transactionTemplate" ref="transactionTemplate"></property>
	</bean>
	
	<bean id="accountDao" class="com.my.spring.transaction.test.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	 <!-- 事務管理器 -->
	 <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"></property>
	 </bean>
	
</beans>
AccountDaoImpl.java需要繼承JdbcDaoSupport,這裏繼承JdbcDaoSupport,再在AccountDaoImpl中注入<property name="dataSource" ref="dataSource"></property>,由於繼承了JdbcDaoSupport,AccountDaoImpl就是自動setJdbcTemplate(),這裏就可以使用this.getJdbcTemplate(),然後值sql語句
transactionTemplate中需要注入transactionManager,因爲這裏事務還是需要交給transactionManager來管理,再將transactionTemplate注入accountService,這裏使用org.springframework.transaction.support.TransactionTemplate中的execute方法,具體實現將transfer實現方法修改成

//編程式事務管理
	@Override
	public void transfer(final String in,final String out,final double money) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus arg0) {
				accountDao.outMoney(out, money);
				int i = 1/0;
				accountDao.inMoney(in, money);
			}
		});
	}
這樣就實現了事務管理,這裏如果出錯那麼事務將會回滾,要麼全部執行,要麼全部不執行。
這種放修改需要修改java代碼,不推薦使用。

基於TransactionProxyFactoryBean的方法

spring配置文件的修改

	<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 加載配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 數據庫連接池 -->
	<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>
	
	<bean id="accountService" class="com.my.spring.transaction.test.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"></property>
	</bean>
	
	<bean id="accountDao" class="com.my.spring.transaction.test.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	 <!-- 事務管理器 -->
	 <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 name="transactionAttributes">
			<props>
				<!-- props的格式 
					* PROPAGATION	: 事務的傳播行爲
					* ISOLATION	: 事務隔離級別
					* readOnly	: 只讀(不可以修改,添加,刪除)
					* -Exception	: 發生那些異常回滾
					* +Eeception	: 發生那些異常不回滾
				 -->
				<prop key="transfer*">PROPAGATION_REQUIRED,+java.lang.ArithmeticException</prop>
			</props>
		</property>
	</bean>
	
</beans>
使用org.springframework.transaction.interceptor.TransactionProxyFactoryBean代理的方法來實現事務管理,需要將accountService設置target,也需要注入transactionManager,因爲真正管理事務的式transactionManager,還需要配置一個transactionAttributes,prop的key屬性,代表accountService中的transfer方法,*號代表通配符。
這裏還需要修改的是TransactionTest.java

@Resource(name="accountServiceProxy")
需要注入經過org.springframework.transaction.interceptor.TransactionProxyFactoryBean代理的對象。
* PROPAGATION    : 事務的傳播行爲
* ISOLATION        : 事務隔離級別
* readOnly        : 只讀(不可以修改,添加,刪除)
* -Exception    : 發生那些異常回滾
* +Eeception    : 發生那些異常不回滾
這種方法只能單個類配置,不僅配置繁瑣,修改起來也不方便,也是不推薦使用的。

基於AspectJ的xml配置

spring配置文件的修改

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 加載配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 數據庫連接池 -->
	<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>
	
	<bean id="accountService" class="com.my.spring.transaction.test1.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"></property>
	</bean>
	
	<bean id="accountDao" class="com.my.spring.transaction.test1.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 事務管理器 -->
	 <bean id="transactionManager" 
	 		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	 	<property name="dataSource" ref="dataSource"/>
	 </bean>
	 
	 <!-- 事務增強配置 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="transfer*" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	
	<aop:config>
		<!-- 切入點 -->
		<aop:pointcut expression="execution(* com.my.spring.transaction.test.*+.*(..))" id="pointcut"/>
		<!-- 配置切面 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
	</aop:config>
	
</beans>
這裏使用首先通過execution(* com.my.spring.transaction.test1.*+.*(..)),讓事務管理這些類,表示再com.my.spring.transaction.test包下的類的任意方法的任意參數,其中‘+’代表可以加上子類,最後兩點代表任意參數。tx:method中屬性:
propagation:事務的傳播行爲
no-rollback-for:發生那些異常不回滾
rollback-for:發生那些異常回滾
read-only:只讀(不可以修改,添加,刪除)
timeout:超時數據
這種使用還是很多了,這裏配置相對簡單,也很清晰。

基於註解的配置

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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-3.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-3.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

	<!-- 加載配置文件 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 數據庫連接池 -->
	<bean name="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
	</bean>
	
	<bean id="accountService" class="com.my.spring.transaction.test1.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"></property>
	</bean>
	
	<bean id="accountDao" class="com.my.spring.transaction.test1.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 事務管理器 -->
	 <bean id="transactionManager" 
	 		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	 	<property name="dataSource" ref="dataSource"/>
	 </bean>
	 
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
</beans>
這裏只需要<tx:annotation-driven transaction-manager="transactionManager"/>就可以開啓註解驅動。
使用:只需要再業務層使用@Transactional註解就可以了,這個註解可以是類級別的,也可以是方法級別的。

@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void transfer1(String in, String out, double money) {
		accountDao.outMoney(out, money);
		int i = 1/0;
		accountDao.inMoney(in, money);
	}
@Transactional也有很多屬性基本和上面那些差不多。
這種使用也比較多,缺點需要修改java代碼,優點就是配置簡單。










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章