詳談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)文章
IntelliJ?IDEA?2021.3?正式發(fā)布之支持遠(yuǎn)程開(kāi)發(fā)、IDE故障排查等多項(xiàng)優(yōu)化改進(jìn)
IntelliJ?IDEA?2021.3?正式發(fā)布:支持遠(yuǎn)程開(kāi)發(fā)、IDE故障排查等多項(xiàng)優(yōu)化改進(jìn)問(wèn)題,在這個(gè)版本中的遠(yuǎn)程開(kāi)發(fā)還不是一個(gè)正式版本,而是BETA版,但通過(guò)這個(gè)BETA版本,也可以體驗(yàn)IDEA“遠(yuǎn)程開(kāi)發(fā)”給我們帶來(lái)的全新體驗(yàn)2021-12-12
SpringBoot使用Redis實(shí)現(xiàn)消息隊(duì)列的方法小結(jié)
在應(yīng)用中把Redis當(dāng)成消息隊(duì)列來(lái)使用已經(jīng)屢見(jiàn)不鮮了,我想主要原因是當(dāng)代應(yīng)用十有八九都會(huì)用到 Redis,因此不用再引入其他消息隊(duì)列系統(tǒng),而且Redis提供了好幾種實(shí)現(xiàn)消息隊(duì)列的方法,用起來(lái)也簡(jiǎn)單,本文給大家介紹了SpringBoot使用Redis實(shí)現(xiàn)消息隊(duì)列的方法小結(jié)2024-04-04
關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例
這篇文章主要介紹了關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Spring Cloud 系列之注冊(cè)中心 Eureka詳解
Netflix Eureka 是由 Netflix 開(kāi)源的一款基于 REST 的服務(wù)發(fā)現(xiàn)組件,包括 Eureka Server 及 Eureka Client。這篇文章主要介紹了Spring Cloud 系列之注冊(cè)中心 Eureka,需要的朋友可以參考下2020-11-11
spring boot+jwt實(shí)現(xiàn)api的token認(rèn)證詳解
這篇文章主要給大家介紹了關(guān)于spring boot+jwt實(shí)現(xiàn)api的token認(rèn)證的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一學(xué)習(xí)學(xué)習(xí)吧2018-12-12
新建Maven工程出現(xiàn)Process?Terminated的問(wèn)題解決
當(dāng)Maven出現(xiàn)"Process terminated"錯(cuò)誤時(shí),這通常是由于配置文件或路徑錯(cuò)誤導(dǎo)致的,本文主要介紹了新建Maven工程出現(xiàn)Process?Terminated的問(wèn)題解決,感興趣的可以了解一下2024-04-04
springBoot定時(shí)任務(wù)處理類(lèi)的實(shí)現(xiàn)代碼
這篇文章主要介紹了springBoot定時(shí)任務(wù)處理類(lèi),需要的朋友可以參考下2018-06-06

