spring控制事務(wù)的三種方式小結(jié)
首先準備環(huán)境,目錄結(jié)構(gòu)如下
數(shù)據(jù)庫準備
業(yè)務(wù)層代碼
@Service("accountService") public class AccountServiceImpl implements AccountService { @Resource(name = "accountDao") AccountDao accountDao; public void transfer(Integer from, Integer to, Float money) { accountDao.subMoney(from,money); int i = 1/0; //此處引發(fā)異常 accountDao.addMoney(to,money); } }
持久層代碼
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao { public void addMoney(Integer id, Float money) { getJdbcTemplate().update("update account set money=money+? where id=?", money , id); } public void subMoney(Integer id, Float money) { getJdbcTemplate().update("update account set money=money-? where id=?", money , id); } }
測試代碼
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class Test { @Resource(name="accountService") private AccountService accountService; @org.junit.Test public void test(){ accountService.transfer(1,2,100f); } }
運行結(jié)果
現(xiàn)在來用三種方式進行事務(wù)控制
方式一:編碼方式(需要修改源代碼,基本不會用)
添加事務(wù)管理類和事務(wù)模板類
<!-- 事務(wù)核心管理器,封裝了所有事務(wù)操作. 依賴于連接池 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource" ></property> </bean> <!-- 事務(wù)模板對象 --> <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" > <property name="transactionManager" ref="transactionManager" ></property> </bean>
修改業(yè)務(wù)層代碼
@Service("accountService") public class AccountServiceImpl implements AccountService { @Resource(name = "accountDao") AccountDao accountDao; @Resource(name="transactionTemplate") private TransactionTemplate transactionTemplate; public void transfer(final Integer from, final Integer to, final Float money) { transactionTemplate.execute(new TransactionCallbackWithoutResult() { @Override protected void doInTransactionWithoutResult(TransactionStatus status) { accountDao.subMoney(from,money); int i = 1/0; accountDao.addMoney(to,money); } }); } }
方式二:xml配置(不需要改動代碼,直接配置xml)
<!-- 配置事務(wù)通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager" > <tx:attributes> <!-- 以方法為單位,指定方法應(yīng)用什么事務(wù)屬性 isolation:隔離級別 propagation:傳播行為 read-only:是否只讀 --> <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /> <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> </tx:attributes> </tx:advice> <!-- 配置織入 --> <aop:config > <!-- 配置切點表達式 --> <aop:pointcut expression="execution(* cn.swun.service.*ServiceImpl.*(..))" id="txPc"/> <!-- 配置切面 : 通知+切點 advice-ref:通知的名稱 pointcut-ref:切點的名稱 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" /> </aop:config>
方式三:注解
首先開啟注解管理aop事務(wù),然后打注解
<!-- 開啟使用注解管理aop事務(wù) --> <tx:annotation-driven/>
/* * 該注解可以打在方法上,也可以打在類上 */ @Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false) public void transfer(final Integer from, final Integer to, final Float money) { accountDao.subMoney(from,money); int i = 1/0; accountDao.addMoney(to,money); }
spring是如何控制事務(wù)的?
Spring 的事務(wù),可以說是 Spring AOP 的一種實現(xiàn)。
AOP面向切面編程,即在不修改源代碼的情況下,對原有功能進行擴展,通過代理類來對具體類進行操作。
spring是一個容器,通過spring這個容器來對對象進行管理,根據(jù)配置文件來實現(xiàn)spring對對象的管理。
spring的事務(wù)聲明有兩種方式,編程式和聲明式。spring主要是通過“聲明式事務(wù)”的方式對事務(wù)進行管理,即在配置文件中進行聲明,通過AOP將事務(wù)切面切入程序,最大的好處是大大減少了代碼量。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot打War包上傳到阿里云的LINUX服務(wù)器的操作方法
這篇文章主要介紹了SpringBoot打War包上傳到阿里云的LINUX服務(wù)器,本文通過圖文并茂的形式給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02深入理解Java8新特性之新日期時間API的應(yīng)用
自從 14 年發(fā)布 Java 8 以后,我們古老 java.util.Date 終于不再是我們 Java 里操作日期時間的唯一的選擇,其實 Java 里的日期時間的相關(guān) API 一直為世猿詬病,今天我們來了解新的日期時間API是怎么使用吧2021-11-11Spring Boot報錯:No session repository could be auto-configured
這篇文章主要給大家介紹了關(guān)于Spring Boot報錯:No session repository could be auto-configured, check your configuration的解決方法,文中給出了詳細的解決方法,對遇到這個問題的朋友們具有一定參考價值,需要的朋友下面來一起看看吧。2017-07-07Spring Security源碼解析之權(quán)限訪問控制是如何做到的
Spring Security 中對于權(quán)限控制默認已經(jīng)提供了很多了,但是,一個優(yōu)秀的框架必須具備良好的擴展性,下面小編給大家介紹Spring Security源碼解析之權(quán)限訪問控制是如何做到的,感興趣的朋友跟隨小編一起看看吧2021-05-05Springboot+rabbitmq實現(xiàn)延時隊列的兩種方式
這篇文章主要介紹了Springboot+rabbitmq實現(xiàn)延時隊列的兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05SpringBoot與SpringMVC中參數(shù)傳遞的原理解析
這篇文章主要介紹了SpringBoot與SpringMVC中參數(shù)傳遞的原理,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07