詳談Spring框架之事務(wù)管理
一、編程式事務(wù)
二、聲明式事務(wù)
1、基于XML的事務(wù)
1.1 Spring配置文件
<!-- 配置c3p0數(shù)據(jù)源,只是進(jì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> <!-- 配置開啟事務(wù)的切入點(diǎn),使用AOP進(jìn)行切入點(diǎn)的配置,并與事務(wù)管理器屬性關(guān)聯(lián)起來 --> <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ù)類
和下面注解方式使用的類一樣,不過是去掉了注解,我將所有的Service層放在一個(gè)包下,這樣便于AOP 切入點(diǎn)表達(dá)式的書寫
2、基于注解的事務(wù)
2.1、Sprin配置文件
<!-- 配置自動掃描 --> <context:component-scan base-package="com.zhoujian.spring"></context:component-scan> <!-- 配置c3p0數(shù)據(jù)源,只是進(jì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>
<!-- 啟動事務(wù)注解 --> <tx:annotation-driven transaction-manager="transactionManager"/>
2.2、業(yè)務(wù)類(Spring 在 Service 層上開啟事務(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; //這里是在指定方法上面開啟事務(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ù)隔離級別,事務(wù)傳播行為,事務(wù)異常控制,事務(wù)強(qiáng)制回滾時(shí)間控制,事務(wù)是否只讀)
一般情況下,不需要進(jìn)行手動改變事務(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í),面對的事務(wù)怎么使用的問題), * 常用的有兩種(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í)會創(chuàng)建一個(gè)新的事務(wù),在這里則都使用方法A所持有的事務(wù), * 對于該配置,如果B過程中發(fā)生異常需要回滾,那么A中所進(jìn)行的所有數(shù)據(jù)庫操作也將同時(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、測試類
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ù)管理就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IntelliJ?IDEA?2021.3?正式發(fā)布之支持遠(yuǎn)程開發(fā)、IDE故障排查等多項(xiàng)優(yōu)化改進(jìn)
IntelliJ?IDEA?2021.3?正式發(fā)布:支持遠(yuǎn)程開發(fā)、IDE故障排查等多項(xiàng)優(yōu)化改進(jìn)問題,在這個(gè)版本中的遠(yuǎn)程開發(fā)還不是一個(gè)正式版本,而是BETA版,但通過這個(gè)BETA版本,也可以體驗(yàn)IDEA“遠(yuǎn)程開發(fā)”給我們帶來的全新體驗(yàn)2021-12-12SpringBoot使用Redis實(shí)現(xiàn)消息隊(duì)列的方法小結(jié)
在應(yīng)用中把Redis當(dāng)成消息隊(duì)列來使用已經(jīng)屢見不鮮了,我想主要原因是當(dāng)代應(yīng)用十有八九都會用到 Redis,因此不用再引入其他消息隊(duì)列系統(tǒng),而且Redis提供了好幾種實(shí)現(xiàn)消息隊(duì)列的方法,用起來也簡單,本文給大家介紹了SpringBoot使用Redis實(shí)現(xiàn)消息隊(duì)列的方法小結(jié)2024-04-04關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例
這篇文章主要介紹了關(guān)于SpringBoot集成Lettuce連接Redis的方法和案例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04spring boot+jwt實(shí)現(xiàn)api的token認(rèn)證詳解
這篇文章主要給大家介紹了關(guān)于spring boot+jwt實(shí)現(xiàn)api的token認(rèn)證的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一學(xué)習(xí)學(xué)習(xí)吧2018-12-12新建Maven工程出現(xiàn)Process?Terminated的問題解決
當(dāng)Maven出現(xiàn)"Process terminated"錯(cuò)誤時(shí),這通常是由于配置文件或路徑錯(cuò)誤導(dǎo)致的,本文主要介紹了新建Maven工程出現(xiàn)Process?Terminated的問題解決,感興趣的可以了解一下2024-04-04springBoot定時(shí)任務(wù)處理類的實(shí)現(xiàn)代碼
這篇文章主要介紹了springBoot定時(shí)任務(wù)處理類,需要的朋友可以參考下2018-06-06