首先準備環境,目錄結構如下
數據庫準備
業務層代碼
@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; //此處引發異常 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); } }
運行結果
現在來用三種方式進行事務控制
方式一:編碼方式(需要修改源代碼,基本不會用)
添加事務管理類和事務模板類
<!-- 事務核心管理器,封裝了所有事務操作. 依賴于連接池 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name="dataSource" ref="dataSource" ></property> </bean> <!-- 事務模板對象 --> <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" > <property name="transactionManager" ref="transactionManager" ></property> </bean>
修改業務層代碼
@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)
<!-- 配置事務通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager" > <tx:attributes> <!-- 以方法為單位,指定方法應用什么事務屬性 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事務,然后打注解
<!-- 開啟使用注解管理aop事務 --> <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是如何控制事務的?
Spring 的事務,可以說是 Spring AOP 的一種實現。
AOP面向切面編程,即在不修改源代碼的情況下,對原有功能進行擴展,通過代理類來對具體類進行操作。
spring是一個容器,通過spring這個容器來對對象進行管理,根據配置文件來實現spring對對象的管理。
spring的事務聲明有兩種方式,編程式和聲明式。spring主要是通過“聲明式事務”的方式對事務進行管理,即在配置文件中進行聲明,通過AOP將事務切面切入程序,最大的好處是大大減少了代碼量。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_38634814/article/details/82429756