詳談Spring框架之事務(wù)管理
一、編程式事務(wù)
二、聲明式事務(wù)
1、基于XML的事務(wù)
1.1 Spring配置文件
<!-- 配置c3p0數(shù)據(jù)源,只是進(jìn)行了最簡(jiǎn)單的配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="hss325730"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
</bean>
<!-- 配置Spring的 JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置Bean -->
<bean id="bookDao" class="com.zhoujian.spring.transcation.xml.BookDaoImpl">
<property name="tempate" ref="jdbcTemplate"></property>
</bean>
<bean id="bookService" class="com.zhoujian.spring.transcation.xml.service.impl.BookServiceImpl">
<property name="dao" ref="bookDao"></property>
</bean>
<bean id="batchBuy" class="com.zhoujian.spring.transcation.xml.service.impl.BatchBuyImpl">
<property name="service" ref="bookService"></property>
</bean>
<!-- 配置事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事務(wù)管理器屬性 并與事務(wù)管理器關(guān)聯(lián)-->
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 在這里一般都是使用通配符進(jìn)行配置, 或者直接配置指定的方法 -->
<tx:method name="buy" propagation="REQUIRES_NEW"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置開(kāi)啟事務(wù)的切入點(diǎn),使用AOP進(jìn)行切入點(diǎn)的配置,并與事務(wù)管理器屬性關(guān)聯(lián)起來(lái) -->
<aop:config>
<aop:pointcut expression="execution(* com.zhoujian.spring.transcation.xml.service.*.*(..))" id="myPointcut"/>
<aop:advisor advice-ref="myAdvice" pointcut-ref="myPointcut"/>
</aop:config>
1.2 業(yè)務(wù)類(lèi)
和下面注解方式使用的類(lèi)一樣,不過(guò)是去掉了注解,我將所有的Service層放在一個(gè)包下,這樣便于AOP 切入點(diǎn)表達(dá)式的書(shū)寫(xiě)
2、基于注解的事務(wù)
2.1、Sprin配置文件
<!-- 配置自動(dòng)掃描 -->
<context:component-scan base-package="com.zhoujian.spring"></context:component-scan>
<!-- 配置c3p0數(shù)據(jù)源,只是進(jìn)行了最簡(jiǎn)單的配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root"></property>
<property name="password" value="hss325730"></property>
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/test"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
</bean>
<!-- 配置Spring的 JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事務(wù)管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 啟動(dòng)事務(wù)注解 --> <tx:annotation-driven transaction-manager="transactionManager"/>
2.2、業(yè)務(wù)類(lèi)(Spring 在 Service 層上開(kāi)啟事務(wù))
package com.zhoujian.spring.transcation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service("bookService")
public class BookServiceImpl implements BookService {
@Autowired
private BookDao dao;
//這里是在指定方法上面開(kāi)啟事務(wù)
@Transactional
@Override
public void buy(String userId, String bookId) {
Integer price = dao.getBookPrice(bookId);
dao.updateBookCount(bookId);
dao.updateUserAccount(userId, price);
}
}
2.3關(guān)于事務(wù)的屬性(事務(wù)隔離級(jí)別,事務(wù)傳播行為,事務(wù)異??刂?,事務(wù)強(qiáng)制回滾時(shí)間控制,事務(wù)是否只讀)
一般情況下,不需要進(jìn)行手動(dòng)改變事務(wù)屬性,使用默認(rèn)的就行
package com.zhoujian.spring.transcation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service("batchBuy")
public class BatchBuyImpl implements BatchBuy {
@Autowired
private BookService service;
/**
* 1、關(guān)于事務(wù)的傳播行為(當(dāng)前事務(wù)方法調(diào)用另外的事務(wù)方法時(shí),面對(duì)的事務(wù)怎么使用的問(wèn)題),
* 常用的有兩種(Propagation.REQUIRED, Propagation.REQUIRES_NEW)
* Spring 默認(rèn)使用 Propagation.REQUIRED,表示使用當(dāng)前事務(wù)方法持有的事務(wù)
*
* 例如: 事務(wù)方法A 調(diào)用事務(wù)方法B,
* 如果事務(wù)的傳播行為使用Propagation.REQUIRED時(shí),表示支持已經(jīng)存在的事務(wù),
* 如果在調(diào)用A方法之前不存在任何事務(wù),那么此時(shí)會(huì)創(chuàng)建一個(gè)新的事務(wù),在這里則都使用方法A所持有的事務(wù),
* 對(duì)于該配置,如果B過(guò)程中發(fā)生異常需要回滾,那么A中所進(jìn)行的所有數(shù)據(jù)庫(kù)操作也將同時(shí)被回滾,
* 因?yàn)檫@兩個(gè)方法使用了同一個(gè)事務(wù);
*
*
* 如果事務(wù)的傳播行為使用Propagation.REQUIRES_NEW時(shí), 表示將A方法所持有的事務(wù)掛起,
* 使用B方法自己的事務(wù),當(dāng)B方法事務(wù)完成之后,A事務(wù)才被喚醒
*/
@Transactional(propagation=Propagation.REQUIRED)
@Override
public void buy(String userId, List<String> bookIds) {
for(String bookId : bookIds){
service.buy(userId, bookId);
}
}
}
3、測(cè)試類(lèi)
package com.zhoujian.spring.transcation;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TranscationTest {
private static ApplicationContext ac = null;
private static BookDao dao = null;
private static BookService service;
private static BatchBuy buy;
static{
ac = new ClassPathXmlApplicationContext("bean-transcation.xml");
dao = ac.getBean("bookDao", BookDao.class);
service = ac.getBean("bookService", BookService.class);
buy = ac.getBean("batchBuy", BatchBuy.class);
}
@Test
public void test1(){
buy.buy("001", Arrays.asList("1","2"));
}
@Test
public void test() {
service.buy("001", "1");
}
}
以上這篇詳談Spring框架之事務(wù)管理就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java接口默認(rèn)方法帶來(lái)的問(wèn)題分析【二義性問(wèn)題】
這篇文章主要介紹了Java接口默認(rèn)方法帶來(lái)的問(wèn)題,結(jié)合實(shí)例形式分析了java接口帶來(lái)的二義性問(wèn)題,需要的朋友可以參考下2019-08-08
Java Web項(xiàng)目中編寫(xiě)定時(shí)任務(wù)的實(shí)現(xiàn)
本篇文章主要介紹了Java Web項(xiàng)目中編寫(xiě)定時(shí)任務(wù)的實(shí)現(xiàn),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
springBoot整合CXF并實(shí)現(xiàn)用戶(hù)名密碼校驗(yàn)的方法
這篇文章主要介紹了springBoot整合CXF并實(shí)現(xiàn)用戶(hù)名密碼校驗(yàn)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Java中的DelayQueue實(shí)現(xiàn)原理及應(yīng)用場(chǎng)景詳解
這篇文章主要介紹了Java中的DelayQueue實(shí)現(xiàn)原理及應(yīng)用場(chǎng)景詳解,DelayQueue是一個(gè)沒(méi)有邊界BlockingQueue實(shí)現(xiàn),加入其中的元素必需實(shí)現(xiàn)Delayed接口,當(dāng)生產(chǎn)者線程調(diào)用put之類(lèi)的方法加入元素時(shí),會(huì)觸發(fā)Delayed接口中的compareTo方法進(jìn)行排序,需要的朋友可以參考下2023-12-12
方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別
這篇文章主要介紹了方法參數(shù)屬性params,@PathVariable和@RequestParam用法及區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
SpringBoot Redisson 集成的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot Redisson 集成的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-05-05

