spring配置宣告式事務

2022-03-12 20:17:26 字數 3575 閱讀 9513

1、宣告式事務配置

* 配置

sessionfactory

* 配置事務管理器

* 事務的傳播特性

* 那些類那些方法使用事務

(spring事務配置在預設狀態下

,只有丟擲執行時異常時才會回滾

)2、編寫業務邏輯方法

* 繼承

hibernatedaosupport

類,使用

hibernatetemplate

來持久化,

hibernatetemplate

是hibernate session的輕量級封裝

* 預設情況下執行期異常才會回滾(包括繼承了

runtimeexception

子類),普通異常是不會滾的

* 編寫業務邏輯方法時,最好將異常一直向上丟擲,在表示層(

struts

)處理* 關於事務邊界的設定,通常設定到業務層,不要新增到

dao上  

3、了解事務的幾種傳播特性

1.propagation_required: 如果存在乙個事務,則支援當前事務。如果沒有事務則開啟

(使用最多)2.

propagation_supports: 如果存在乙個事務,支援當前事務。如果沒有事務,則非事務的執行

3.propagation_mandatory: 如果已經存在乙個事務,支援當前事務。如果沒有乙個活動的事務,則丟擲異常。

4.propagation_requires_new: 總是開啟乙個新的事務。如果乙個事務已經存在,則將這個存在的事務掛起。

5.propagation_not_supported: 總是非事務地執行,並掛起任何存在的事務。

6.propagation_never: 總是非事務地執行,如果存在乙個活動事務,則丟擲異常

7.propagation_nested:如果乙個活動的事務存在,則執行在乙個巢狀的事務中

. 如果沒有活動事務

, 則按transactiondefinition.propagation_required 

屬性執行

4、

spring

事務的隔離級別

1.isolation_default: 這是乙個

platfromtransactionmanager

預設的隔離級別,使用資料庫預設的事務隔離級別

.另外四個與jdbc

的隔離級別相對應

2.isolation_read_uncommitted: 這是事務最低的隔離級別,它充許令外乙個事務可以看到這個事務未提交的資料。

這種隔離級別會產生髒讀,不可重複讀和幻像讀。

3.isolation_read_committed: 保證乙個事務修改的資料提交後才能被另外乙個事務讀取。另外乙個事務不能讀取該事務未提交的資料

(使用最多)4.

isolation_repeatable_read: 這種事務隔離級別可以防止髒讀,不可重複讀。但是可能出現幻像讀。

它除了保證乙個事務不能讀取另乙個事務未提交的資料外,還保證了避免下面的情況產生(

不可重複讀)。

5.isolation_serializable 這是花費最高代價但是最可靠的事務隔離級別。事務被處理為順序執行。

除了防止髒讀,不可重複讀外,還避免了幻像讀。

<?

xml version="1.0" encoding="utf-8"

?>

<

beans

xmlns

=""xmlns:xsi

=""xmlns:aop

=""xmlns:tx

=""xsi:schemalocation

=" /spring-beans-2.0.xsd

/spring-aop-2.0.xsd

/spring-tx-2.0.xsd"

>

<

bean

id="sessionfactory"

class

="org.springframework.orm.hibernate3.localsessionfactorybean"

>

<

property

name

="configlocation"

>

<

value

>classpath:hibernate.cfg.xml

value

>

property

>

bean

>

<

bean

id="transactionmanager"

class

="org.springframework.orm.hibernate3.hibernatetransactionmanager"

>

<

property

name

="sessionfactory"

ref="sessionfactory"

/>

bean

>

<

tx:advice

id="txadvice"

transaction-manager

="transactionmanager"

>

<

tx:attributes

>

<

tx:method

name

="add*"

propagation

="required"

/>

<

tx:method

name

="del*"

propagation

="required"

/>

<

tx:method

name

="modify*"

propagation

="required"

/>

<

tx:method

name

="*"

read-only

="true"

/>

tx:attributes

>

tx:advice

>

<

aop:config

>

<

aop:pointcut

id="allmethod"

expression

="execution(* com.dennist.service.*.*(..))"

/>

<

aop:advisor

advice-ref

="txadvice"

pointcut-ref

="allmethod"

/>

aop:config

>

beans

>

spring 宣告式事務配置

用 spring 事務管理器,由spring來負責資料庫的開啟,提交,回滾.預設遇到執行期例外 throw new runtimeexception 注釋 會回滾 unchecked,需要捕獲的例外 throw new exception 注釋 不會回滾 checked.需要修改規則加入注釋 tra...

Spring註解宣告式事務配置

一 引入命名空間 二 具有 transactional 註解的bean 自動配置為宣告式事務支援 bean id transactionmanager class org.springframework.orm.hibernate3.hibernatetransactionmanager prope...

Spring配置宣告式事務控制

transactionmanager class org.springframework.jdbc.datasource.datasourcetransactionmanager name datasource ref datasource property bean tx adviceid txm...