spring配置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:p="http://www.springframework.org/schema/p"
	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-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
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	<!-- 事務管理器 -->
	<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="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="create*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<!-- 切面 -->
	<aop:config>
	<!-- 第一個*表示不限制返回值 ..表示子包以及子包下所有的 *表示類 *表示方法括號中表示參數..爲所有參數 -->
		<aop:advisor advice-ref="txAdvice"
			pointcut="execution(* cn.tlj..*.*(..))" />
	</aop:config>
</beans>

主要說下切面和傳播行爲

切面execution(* cn.tlj..*.*(..))          第一個*表示對於所有的返回值,第一個..表示該包以及該包下的所有類,第二個*表示類,第三個*表示方法(..)表示參數

傳播行爲的屬性

<?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:tx="http://www.springframework.org/schema/tx"
	    xmlns:aop="http://www.springframework.org/schema/aop"
	    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/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
	    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
    ">
    
    
    <!-- 掃描註解 -->
	<context:component-scan base-package="lesson04.testm"></context:component-scan>
	<!-- 讀取properties資源文件 -->
	<context:property-placeholder location="classpath:/lesson04/jdbc/jdbcoracle.properties"/>
	
	
	<!-- ${username}是個關鍵字  默認獲取操作系統用戶 -->
	<!-- 連接數據庫 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="url" value="${url}"></property>
		<property name="username" value="${username1}"></property>
		<property name="password" value="${password}"></property>
		<property name="driverClassName" value="${driverClass}"></property>
	</bean>
	<!-- jdbc的模板   可以執行sql語句 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<!-- 把連接數據庫的bean注入 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 事務管理類 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 事務需要操作數據庫     要把連接數據庫的bean注入 -->
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 開啓事物   定義通知  通知的代碼  spring已經實現 -->
	<!-- 如果上面的id名爲 id="transactionManager" 下面的 transaction-manager="transactionManager"可以不寫-->
	<tx:advice id="myAdvise" transaction-manager="transactionManager">
		<tx:attributes>
			<!--      設置事務的傳播屬性
				propagation="REQUIRED"  方法和方法之間存在父子關係
					REQUIRED 默認   父方法沒有事務創建一個事務  有事務使用父方法當前事務
					REQUIRES_NEW   不管父方法是否存在事務  都會新建事務
					SUPPORTS  父方法存在事務  使用當前事務   沒有事務 使用jdbc的事務(自動提交)
			 		NOT_SUPPORTED  不管父方法中是否存在事務  都不會使用父方法中的事務(掛起事務)
			 		MANDATORY   必須在事務環境中運行  父方法沒有事務  拋出異常
			 			No existing transaction found for transaction marked with propagation 'mandatory'
			 		NEVER  父方法中不能存在事務   有事務就拋出異常
			 		
			 		
			 	設置隔離事務屬性	
	 			isolation="DEFAULT" 隔離級別
		   	       DEFAULT  使用數據庫本身的隔離級別 ORACLE(讀已提交) MYSQL(可重複讀)
				   READ_UNCOMMITTED  spring實現讀未提交 (髒讀)
				   READ_COMMITTED     spring實現讀已提交 (待解決--不可重複讀+幻讀)
				   REPEATABLE_READ   spring實現可重複讀 (待解決--幻讀)
				   SERIALIZABLE     spring實現串行化(已解決)
				   
				   
				設置回滾事務屬性 :spring事務 運行過程中 碰到運行時異常 自動回滾 非運行時異常不會回滾
				   rollback-for=""  指定會自動回滾的非運行時異常       設值成Exception就可以了   發生任何異常都回滾
				   no-rollback-for="" 指定某些運行時異常拋出時 不回滾
				   
				只讀事務屬性 (除特定的方法以外其他的業務邏輯方法 都不應該操作事務)  
				  read-only="true"設置只讀事務  
				  
				超時事務屬性 
				 timeout=10-30左右  mysql默認10s自動超時  oracle永不超時  
			 		
			 -->
			<tx:method name="Update*" propagation="REQUIRED" rollback-for="Exception" isolation="DEFAULT" timeout="5"/>
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="delete*"/>
			<tx:method name="*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 定義切點 -->
	<aop:config>
							<!-- 任意返回值    lesson04.testm.dao包下的所有類、所有方法攔截 -->
		<aop:pointcut expression="execution(* lesson04.testm.dao.*.*(..))" id="myPoint"/>
		<!-- 把切點和通知綁定     <tx:advice> -->
		<aop:advisor advice-ref="myAdvise" pointcut-ref="myPoint"/>
	</aop:config>
</beans>

 

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