Spring中事務管理方案和事務管理器及事務控制的API詳解
一、事務管理方案
聲明式事務底層采用AOP技術,在service層手動添加事務
1. 修改業(yè)務層代碼
添加一個SqlSessionTemplate對象,讓我們對業(yè)務方法進行try catch,沒有異常則進行提交,捕捉到異?;貪L即可。
package com.example.service; import com.example.dao.AccountDao; import com.example.pojo.Account; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; @Service public class AccountService { @Autowired private AccountDao accountDao; @Autowired private SqlSessionTemplate sessionTemplate; /** * * @param id1 轉出人id * @param id2 轉入人id * @param price 金額 */ // 作用方法上時,該方法都將具有該類型事務的事務屬性 public void transfer(int id1,int id2, double price){ try { // 轉出人減少余額 Account account1 = accountDao.findById(id1); account1.setBalance(account1.getBalance() - price); accountDao.update(account1); // 模擬程序出錯 int i = 1 / 0; // 轉入人增加余額 Account account2 = accountDao.findById(id2); account2.setBalance(account2.getBalance() + price); accountDao.update(account2); sessionTemplate.commit(); } catch (Exception e){ e.printStackTrace(); sessionTemplate.rollback(); } } }
2. 測試
OK,可以看到這里程序是出現(xiàn)異常中斷了的。現(xiàn)在觀看數(shù)據(jù)庫里面的情況是怎么樣的。
OK,可以看到這里張三確實沒有被扣錢啊,所以手動提交事務也是可以的,但是這樣我們的try catch就太多了。因此有了事務管理器。
二、事務管理器
1. 簡介
Spring依賴事務管理器進行事務管理,事務管理器即一個通知類,我們?yōu)樵撏ㄖ愒O置切點為service層方法即可完成事務自動管理。
由于不同技術操作數(shù)據(jù)庫,進行事務操作的方法不同。
如:JDBC提交事務是 connection.commit() ,MyBatis提交事務是 sqlSession.commit() ,所以Spring提供了多個事務管理器。
事務管理器名稱 | 作用 |
org.springframework.jdbc.datasource.DataSourceTransactionManager | 針對JDBC技術提供的事務管理器。適用于JDBC和MyBatis。 |
org.springframework.orm.hibernate3.HibernateTransactionManager | 針對于Hibernate框架提供的事務管理器。適用于Hibernate框架。 |
org.springframework.orm.jpa.JpaTransactionManager | 針對于JPA技術提供的事務管理器。適用于JPA技術。 |
org.springframework.transaction.jta.JtaTransactionManager | 跨越了多個事務管理源。適用在兩個或者是多個不同的數(shù)據(jù)源中實現(xiàn)事務控制。 |
我們使用MyBatis操作數(shù)據(jù)庫,接下來使用 DataSourceTransactionManager 進行事務管理。
2. 在配置文件中引入約束
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
3. 進行事務配置
在applicationContext.xml文件新增配置
三、事務控制的API
事務管理器都實現(xiàn)了PlatformTransactionManager接口,Spring進行事務控制的功能是由三個接口提供的,這三個接口是Spring實現(xiàn)的,在開發(fā)中我們很少使用到,只需要了解他們的作用即可:
1.PlatformTransactionManager接口
PlatformTransactionManager是Spring提供的事務管理器接口,所有事務管理器都實現(xiàn)了該接口。
該接口中提供了三個事務操作方法:
- TransactionStatus getTransaction(TransactionDefinition definition):獲取事務狀態(tài)信息。
- void commit(TransactionStatus status):事務提交
- void rollback(TransactionStatus status):事務回滾
2.TransactionDefinition接口
TransactionDefinition是事務的定義信息對象,它有如下方法:
- String getName():獲取事務對象名稱
- 。int getIsolationLevel():獲取事務的隔離級別。
- int getPropagationBehavior():獲取事務的傳播行為。
- int getTimeout():獲取事務的超時時間。
- boolean isReadOnly():獲取事務是否只讀。
3.TransactionStatus接口
TransactionStatus是事務的狀態(tài)接口,它描述了某一時間點上事務的狀態(tài)信息。
它有如下方法:
- void flush() 刷新事務
- boolean hasSavepoint() 獲取是否存在保存點
- boolean isCompleted() 獲取事務是否完成
- boolean isNewTransaction() 獲取是否是新事務
- boolean isRollbackOnly() 獲取是否回滾
- void setRollbackOnly() 設置事務回滾
到此這篇關于Spring中事務管理方案和事務管理器及事務控制的API詳解的文章就介紹到這了,更多相關Spring事務API內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
MyBatis批量插入/修改/刪除MySql數(shù)據(jù)
這篇文章主要給大家介紹了關于MyBatis批量插入/修改/刪除MySql數(shù)據(jù)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05springboot jasypt2.x與jasypt3.x的使用方式
在軟件開發(fā)中,將配置文件中的敏感信息(如數(shù)據(jù)庫密碼)進行加密是保障安全的有效手段,jasypt框架提供了這一功能,支持通過加密工具類或命令行工具生成密文,并通過修改配置文件和啟動參數(shù)的方式使用密文和密鑰,這樣即便配置文件被泄露2024-09-09基于Javamail實現(xiàn)發(fā)送郵件(QQ/網(wǎng)易郵件服務器)
這篇文章主要介紹了基于Javamail實現(xiàn)發(fā)送郵件,分別使用QQ郵箱作為smtp郵件服務器發(fā)送郵件,使用網(wǎng)易郵箱作為smtp郵件服務器發(fā)送郵件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-08-08SpringBoot 整合 Shiro 密碼登錄與郵件驗證碼登錄功能(多 Realm 認證)
這篇文章主要介紹了SpringBoot 整合 Shiro 密碼登錄與郵件驗證碼登錄(多 Realm 認證),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02