日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

Spring JDBC-使用XML配置声明式事务

發布時間:2025/3/21 asp.net 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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’等等。
propagationREQUIRED事務傳播行為
isolationDEFAULT事務隔離級別
timeout-1事務超時的時間(以秒為單位) ,如果為-1,則說明事務超時時間由底層的事務系統所決定
read-onlyfalse事務是否只讀
rollback-for所有運行期異常回滾觸發事務回滾的Excepiton,用異常名稱的片段進行匹配,可以設置多個,用逗號分開,如Exception1,Exception2
norollback-for所有檢查型異常不回滾不觸發事務回滾的Excepiton,用異常名稱的片段進行匹配,可以設置多個,用逗號分開,如Exception1,Exception2

繼承自runtimeexception或error的是非檢查型異常(運行期異常)

繼承自exception的是檢查型異常,檢查型異常則必須用try語句塊進行處理或者把異常交給上級方法處理.

如果需要為不同的業務Bean配置不同的事務管理風格,則可以在aop:config中定義多套事務切面。

基于aop/tx配置的聲明式事務管理是實際應用中最常使用的事務管理方式,它的表達能力最強且使用最為靈活。

總結

以上是生活随笔為你收集整理的Spring JDBC-使用XML配置声明式事务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。