首先準(zhǔn)備環(huán)境,目錄結(jié)構(gòu)如下
數(shù)據(jù)庫(kù)準(zhǔn)備
業(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); } }
測(cè)試代碼
@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); } }
運(yùn)行結(jié)果
現(xiàn)在來(lái)用三種方式進(jìn)行事務(wù)控制
方式一:編碼方式(需要修改源代碼,基本不會(huì)用)
添加事務(wù)管理類(lèi)和事務(wù)模板類(lèi)
<!-- 事務(wù)核心管理器,封裝了所有事務(wù)操作. 依賴(lài)于連接池 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource" ></property> </bean> <!-- 事務(wù)模板對(duì)象 --> <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配置(不需要改動(dòng)代碼,直接配置xml)
<!-- 配置事務(wù)通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager" > <tx:attributes> <!-- 以方法為單位,指定方法應(yīng)用什么事務(wù)屬性 isolation:隔離級(jí)別 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 > <!-- 配置切點(diǎn)表達(dá)式 --> <aop:pointcut expression="execution(* cn.swun.service.*ServiceImpl.*(..))" id="txPc"/> <!-- 配置切面 : 通知+切點(diǎn) advice-ref:通知的名稱(chēng) pointcut-ref:切點(diǎn)的名稱(chēng) --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc" /> </aop:config>
方式三:注解
首先開(kāi)啟注解管理aop事務(wù),然后打注解
<!-- 開(kāi)啟使用注解管理aop事務(wù) --> <tx:annotation-driven/>
/* * 該注解可以打在方法上,也可以打在類(lèi)上 */ @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ù),可以說(shuō)是 Spring AOP 的一種實(shí)現(xiàn)。
AOP面向切面編程,即在不修改源代碼的情況下,對(duì)原有功能進(jìn)行擴(kuò)展,通過(guò)代理類(lèi)來(lái)對(duì)具體類(lèi)進(jìn)行操作。
spring是一個(gè)容器,通過(guò)spring這個(gè)容器來(lái)對(duì)對(duì)象進(jìn)行管理,根據(jù)配置文件來(lái)實(shí)現(xiàn)spring對(duì)對(duì)象的管理。
spring的事務(wù)聲明有兩種方式,編程式和聲明式。spring主要是通過(guò)“聲明式事務(wù)”的方式對(duì)事務(wù)進(jìn)行管理,即在配置文件中進(jìn)行聲明,通過(guò)AOP將事務(wù)切面切入程序,最大的好處是大大減少了代碼量。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/qq_38634814/article/details/82429756