spring控制事務的三種方式小結
首先準備環(huán)境,目錄結構如下

數據庫準備

業(yè)務層代碼
@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);
}
}
運行結果

現在來用三種方式進行事務控制
方式一:編碼方式(需要修改源代碼,基本不會用)
添加事務管理類和事務模板類
<!-- 事務核心管理器,封裝了所有事務操作. 依賴于連接池 -->
<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>
修改業(yè)務層代碼
@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將事務切面切入程序,最大的好處是大大減少了代碼量。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringBoot發(fā)現最新版Druid重大問題(坑)
這篇文章主要介紹了SpringBoot發(fā)現最新版Druid重大問題(坑),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
Java編程發(fā)展歷史(動力節(jié)點Java學院整理)
Java的歷史可以追溯到1991年4月,Sun公司的James Gosling領導的綠色計劃(Green Project)開始著力發(fā)展一種分布式系統結構,使其能夠在各種消費性電子產品上運行,他們使用了C/C++/Oak語言。由于多種原因,綠色計劃逐漸陷于停滯狀態(tài)2017-03-03
Mybatis-Plus中updateById方法不能更新空值問題解決
本文主要介紹了Mybatis-Plus中updateById方法不能更新空值問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-08-08

