Spring JDBC-使用XML配置声明式事务
- 系列
- 概述
- 基于aop/tx命名空間的配置
- 示例
- tx:method元素屬性
系列
Spring對事務管理的支持概述以及 編程式的事務管理
Spring JDBC-使用XML配置聲明式事務
Spring JDBC-使用注解配置聲明式事務
概述
大多數開發者選擇聲明式事務管理的功能,這種方式對代碼的侵入性最小,可以讓事務管理完全從業務代碼中移除,非常符合非侵入式輕量容器的理念。
Spring的聲明式事務管理是通過AOP實現的,通過事務的聲明性信息,Spring負責將事務管理增強邏輯動態的織入到業務方法的相應連接點中。 這些邏輯包括獲取線程綁定資源、開始事務、提交/回滾事務、進行異常轉換和處理等工作。
基于aop/tx命名空間的配置
Spring2.0引入了AspectJ切面定義語言,這使得事務方法切面描述變得更加簡單。
Spring在基于Schema的配置中添加了一個tx命名空間,在配置文件中以明確結構化的方式定義事務屬性,大大提高了配置事務屬性的便利性。
示例
代碼已托管到Github—> https://github.com/yangshangwei/SpringMaster
我們來看下關于事務管理的配置文件:
<?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:p="http://www.springframework.org/schema/p"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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 引入其他xml --><import resource="classpath:com/xgj/dao/transaction/xmlTrans/conf_txaop_base.xml"/><!-- 使用強大的切點表達式語言輕松定義目標方法 --><aop:config proxy-target-class="true"><!-- 通過aop定義事務增強切面 --><aop:pointcut id="serviceMethod" expression="execution(* com.xgj.dao.transaction.xmlTrans.service.*Service.*(..))" /> <!-- 引用切面和事務增強 --><aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice"/></aop:config><!-- 事務增強 --><tx:advice id="txAdvice" transaction-manager="transactionManager" ><!-- 事務屬性定義 --><tx:attributes><tx:method name="get*" read-only="true" /><tx:method name="add*" rollback-for="Exception"/><tx:method name="update*"/></tx:attributes></tx:advice></beans>在配置文件中引入了
<import resource="classpath:com/xgj/dao/transaction/xmlTrans/conf_txaop_base.xml"/>在aop命名空間中,通過切點表達式語言,
expression="execution(* com.xgj.dao.transaction.xmlTrans.service.*Service.*(..))"將com.xgj.dao.transaction.xmlTrans.service包以及所有以Service為后綴的類納入了需要進行事務增強的范圍,并配合 tx:advice的aop:advisor完成了事務切面的定義。
aop:advisor引用txAdvice增強是在tx命名空間上定義的。 首先事務增強一定需要一個事務管理器的支持,tx:advice通過 transaction-manager屬性引用定義的事務管理器(默認查找名為transactionManager的事務管理器,所以如果名為transactionManager可以不指定)。
我們在ManagerService類中將addTeacher和addStudent中放在一個事務中
package com.xgj.dao.transaction.xmlTrans.service;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import com.xgj.dao.transaction.xmlTrans.dao.StudentDao; import com.xgj.dao.transaction.xmlTrans.dao.TeacherDao; import com.xgj.dao.transaction.xmlTrans.domain.Student; import com.xgj.dao.transaction.xmlTrans.domain.Teacher;/*** * * @ClassName: ManagerService* * @Description: @Service標注的Service層* * @author: Mr.Yang* * @date: 2017年9月22日 下午10:18:31*/@Service public class ManagerService {private TeacherDao teacherDao;private StudentDao studentDao;/*** * * @Title: addTeacherAndStudent* * @Description: 配置文件中<tx:method name="add*" rollback-for="Exception"/>* 我們故意經寫入student表時,讓其異常,看下teacher是否回滾--OK 都沒有寫入,說明在一個事務中* 如果有去掉<tx:method name="add*"* rollback-for="Exception"/>,經驗證Teacher可成功增加,說明不在一個事務中* * rollback-for="Exception"這個可不加,默認是回滾的* @param teacher* @param student* * @return: void*/public void addTeacherAndStudent(Teacher teacher, Student student) {teacherDao.addTeacher(teacher);studentDao.addStudent(student);}@Autowiredpublic void setTeacherDao(TeacherDao teacherDao) {this.teacherDao = teacherDao;}@Autowiredpublic void setStudentDao(StudentDao studentDao) {this.studentDao = studentDao;} }因為在配置文件中
<tx:method name="add*" rollback-for="Exception"/>所以addTeacherAndStudent中的方法在一個事務中,我們將addStudent中故意寫錯,讓其拋出異常,經驗證Teacher也沒有歐增加成功,說明配置生效,在一個事務中。
tx:method元素屬性
可以使用冒號來定義表格的對齊方式,如下:
| name | 是 | 與事務屬性關聯的方法名。通配符(*)可以用來指定一批關聯到相同的事務屬性的方法 | 如:’get*’、’handle*’、’on*Event’等等。 |
| propagation | 否 | REQUIRED | 事務傳播行為 |
| isolation | 否 | DEFAULT | 事務隔離級別 |
| timeout | 否 | -1 | 事務超時的時間(以秒為單位) ,如果為-1,則說明事務超時時間由底層的事務系統所決定 |
| read-only | 否 | false | 事務是否只讀 |
| rollback-for | 否 | 所有運行期異常回滾 | 觸發事務回滾的Excepiton,用異常名稱的片段進行匹配,可以設置多個,用逗號分開,如Exception1,Exception2 |
| norollback-for | 否 | 所有檢查型異常不回滾 | 不觸發事務回滾的Excepiton,用異常名稱的片段進行匹配,可以設置多個,用逗號分開,如Exception1,Exception2 |
繼承自runtimeexception或error的是非檢查型異常(運行期異常)
繼承自exception的是檢查型異常,檢查型異常則必須用try語句塊進行處理或者把異常交給上級方法處理.
如果需要為不同的業務Bean配置不同的事務管理風格,則可以在aop:config中定義多套事務切面。
基于aop/tx配置的聲明式事務管理是實際應用中最常使用的事務管理方式,它的表達能力最強且使用最為靈活。
總結
以上是生活随笔為你收集整理的Spring JDBC-使用XML配置声明式事务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript-JavaScrip
- 下一篇: asp.net ajax控件工具集 Au