Spring 配置 事務的幾種方式

2021-06-29 11:23:40 字數 1904 閱讀 4160

spring配置檔案中關於事務配置總是由三個組成部分,datasource、transactionmanager和**機制這三部分,無論是那種配置方法,一般變化的只是**機制這塊!

首先我建立了兩個類,乙個介面乙個實現:

package com.dao;

public inte***ce userdao

實現:

package com.dao.impl;

import org.springframework.orm.hibernate3.support.hibernatedaosupport;

import com.dao.userdao;

public class userdaoimpl extends hibernatedaosupport implements userdao

}

第一種:每個bean都有乙個**:

<?xml version="1.0" encoding="utf-8"?>			

org.hibernate.dialect.mysql5dialect

true

true

propagation_required

第二種:所有bean共享乙個**:

<?xml version="1.0" encoding="utf-8"?>			

org.hibernate.dialect.mysql5dialect

true

true

propagation_required

第三種:***:

<?xml version="1.0" encoding="utf-8"?>			

org.hibernate.dialect.mysql5dialect

true

true

propagation_required

*daoimpl

transactioninterceptor

第四種:使用tx標籤配置的***:

<?xml version="1.0" encoding="utf-8"?>			

org.hibernate.dialect.mysql5dialect

true

true

第五種:註解:

<?xml version="1.0" encoding="utf-8"?>			

org.hibernate.dialect.mysql5dialect

true

true

如果使用了註解,那麼實現類應該這樣寫:

package com.dao.impl;

import org.springframework.orm.hibernate3.support.hibernatedaosupport;

import org.springframework.stereotype.component;

import org.springframework.transaction.annotation.transactional;

import com.dao.userdao;

@transactional

@component("userdaoagency")

public class userdaoimpl extends hibernatedaosupport implements userdao

}

這樣每個方法都能自己定義自己的事務處理!

spring幾種事務配置

lygle 寫道 根據 機制不同,有以下幾種配置方式 先定義乙個dao介面及實現類 public inte ce iuserdao實現類 public class userdaoimpl extends hibernatedaosupport implements iuserdao 第一種為每個be...

spring對事務的配置方式

spring對事務的配置方式,有兩種 使用xml配置檔案的形式 優點 可以在後期維護時,適當調整事務管理模式,並且只要遵循一定的命名規範,可以讓程式設計師不必關心事務本身。缺點 系統越龐大,xml檔案就越臃腫,一定程度上增加了維護的難度。使用註解的形式 優點 配置比較方便,程式設計師只要在servi...

spring 配置事務 配置檔案方式

xmlns xsi xmlns aop xmlns tx xmlns jdbc xmlns p xsi schemalocation spring beans 3.0.xsd spring jdbc 3.0.xsd spring aop 3.0.xsd spring tx 3.0.xsd destr...