自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring 事務(wù)還能這樣管理?

開發(fā) 架構(gòu)
在實(shí)際開發(fā)中,操作數(shù)據(jù)庫時(shí)都會(huì)涉及到事務(wù)管理問題,為此Spring提供了專門用于事務(wù)處理的API。Spring的事務(wù)管理簡(jiǎn)化了傳統(tǒng)的事務(wù)管理流程,并且在一定程度上減少了開發(fā)者的工作量。

[[431979]]

本文轉(zhuǎn)載自微信公眾號(hào)「程序員千羽」,作者程序員千羽。轉(zhuǎn)載本文請(qǐng)聯(lián)系程序員千羽公眾號(hào)。

GitHub:https://github.com/nateshao/ssm/tree/master/105-spring-transaction

1.Spring事務(wù)管理概述

什么是Spring的事務(wù)管理?

在實(shí)際開發(fā)中,操作數(shù)據(jù)庫時(shí)都會(huì)涉及到事務(wù)管理問題,為此Spring提供了專門用于事務(wù)處理的API。Spring的事務(wù)管理簡(jiǎn)化了傳統(tǒng)的事務(wù)管理流程,并且在一定程度上減少了開發(fā)者的工作量。

Spring事務(wù)管理的三個(gè)核心接口。

在該JAR包的org.springframework.transaction包中,有3個(gè)接口文件PlatformTransactionManager、TransactionDefinition和TransactionStatus

事務(wù)管理的核心接口

Platform TransactionManager

PlatformTransactionManager接口是Spring提供的平臺(tái)事務(wù)管理器,主要用于管理事務(wù)。該接口中提供了三個(gè)事務(wù)操作的方法,具體如下:

  • TransactionStatus getTransaction(TransactionDefinition definition):用于獲取事務(wù)狀態(tài)信息
  • void commit(TransactionStatus status):用于提交事務(wù)
  • void rollback(TransactionStatus status):用于回滾事務(wù)

PlatformTransactionManager接口只是代表事務(wù)管理的接口,并不知道底層是如何管理事務(wù)的,具體如何管理事務(wù)則由它的實(shí)現(xiàn)類來完成。該接口常見的幾個(gè)實(shí)現(xiàn)類如下:

小提示:當(dāng)?shù)讓硬捎貌煌某志脤蛹夹g(shù)時(shí),系統(tǒng)只需使用不同的PlatformTransactionManager實(shí)現(xiàn)類即可。

TransactionDefinition

TransactionDefinition接口是事務(wù)定義(描述)的對(duì)象,該對(duì)象中定義了事務(wù)規(guī)則,并提供了獲取事務(wù)相關(guān)信息的方法,具體如下:

  • String getName( ); 獲取事務(wù)對(duì)象名稱
  • int getIsolationLevel( ); 獲取事務(wù)的隔離級(jí)別
  • int getPropagationBehavior( ); 獲取事務(wù)的傳播行為
  • int getTimeout( );獲取事務(wù)的超時(shí)時(shí)間
  • boolean isReadOnly( ); 獲取事務(wù)是否只讀

上述方法中,事務(wù)的傳播行為是指在同一個(gè)方法中,不同操作前后所使用的事務(wù)。傳播行為有很多種,具體如下表所示:

在事務(wù)管理過程中,傳播行為可以控制是否需要?jiǎng)?chuàng)建事務(wù)以及如何創(chuàng)建事務(wù),通常情況下,數(shù)據(jù)的查詢不會(huì)影響原數(shù)據(jù)的改變,所以不需要進(jìn)行事務(wù)管理,而對(duì)于數(shù)據(jù)的插入、更新和刪除操作,必須進(jìn)行事務(wù)管理。如果沒有指定事務(wù)的傳播行為,Spring默認(rèn)傳播行為是REQUIRED。

TransactionStatus

TransactionStatus接口是事務(wù)的狀態(tài),它描述了某一時(shí)間點(diǎn)上事務(wù)的狀態(tài)信息。該接口中包含6個(gè)方法,具體如下:

  • void flush(); 刷新事務(wù)
  • boolean hasSavepoint(); 獲取是否存在保存點(diǎn)
  • boolean isCompleted(); 獲取事務(wù)是否完成
  • boolean isNewTransaction(); 獲取是否為新事務(wù)
  • boolean isRollbackOnly(); 獲取事務(wù)是否回滾
  • void setRollbackOnly(); 設(shè)置事務(wù)回滾

聲明式事務(wù)管理最大的優(yōu)點(diǎn)在于開發(fā)者無需通過編程的方式來管理事務(wù),只需在配置文件中進(jìn)行相關(guān)的事務(wù)規(guī)則聲明,就可以將事務(wù)應(yīng)用到業(yè)務(wù)邏輯中。這使得開發(fā)人員可以更加專注于核心業(yè)務(wù)邏輯代碼的編寫,在一定程度上減少了工作量,提高了開發(fā)效率,所以在實(shí)際開發(fā)中,通常都推薦使用聲明式事務(wù)管理。

2. 聲明式事務(wù)管理

如何實(shí)現(xiàn)Spring的聲明式事務(wù)管理?

Spring的聲明式事務(wù)管理可以通過兩種方式來實(shí)現(xiàn), **一種是基于XML的方式,另一種是基于Annotation的方式。**接下來的兩個(gè)小節(jié)中,將對(duì)這兩種聲明式事務(wù)管理方式進(jìn)行詳細(xì)講解。

基于XML方式的聲明式事務(wù)

配置< tx:advice >元素的重點(diǎn)是配置< tx:method >子元素,上圖中使用灰色標(biāo)注的幾個(gè)屬性是< tx:method >元素中的常用屬性。其屬性描述具體如下:

Account.java

  1. @Data 
  2. public class Account { 
  3.     private Integer id;       // 賬戶id 
  4.     private String username; // 用戶名 
  5.     private Double balance;  // 賬戶余額 

AccountDao.java

  1. public interface AccountDao { 
  2.     ......... 
  3.     // 轉(zhuǎn)賬 
  4.     public void transfer(String outUser,String inUser,Double money); 

AccountDaoImpl.java

  1. @Transactional(propagation = Propagation.REQUIRED, 
  2.             isolation = Isolation.DEFAULT, readOnly = false
  3.     @Override 
  4.     public void transfer(String outUser, String inUser, Double money) { 
  5.         // 收款時(shí),收款用戶的余額=現(xiàn)有余額+所匯金額 
  6.         this.jdbcTemplate.update("update account set balance = balance +? " 
  7.                 + "where username = ?",money, inUser); 
  8.         // 模擬系統(tǒng)運(yùn)行時(shí)的突發(fā)性問題 
  9. //        int i = 1/0; 
  10.         // 匯款時(shí),匯款用戶的余額=現(xiàn)有余額-所匯金額 
  11.         this.jdbcTemplate.update("update account set balance = balance-? " 
  12.                 + "where username = ?",money, outUser); 
  13.     } 

TransactionTest.java

  1. package com.nateshao.jdbc; 
  2.  
  3. import org.junit.jupiter.api.Test; 
  4. import org.springframework.context.ApplicationContext; 
  5. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  6.  
  7. /** 
  8.  * @date Created by 邵桐杰 on 2021/10/15 22:05 
  9.  * @微信公眾號(hào) 程序員千羽 
  10.  * @個(gè)人網(wǎng)站 www.nateshao.cn 
  11.  * @博客 https://nateshao.gitee.io 
  12.  * @GitHub https://github.com/nateshao 
  13.  * @Gitee https://gitee.com/nateshao 
  14.  * Description: 
  15.  */ 
  16. public class TransactionTest { 
  17.      @Test 
  18.     public void xmlTest() { 
  19.         ApplicationContext applicationContext = 
  20.                 new ClassPathXmlApplicationContext("applicationContext.xml"); 
  21.         // 獲取AccountDao實(shí)例 
  22.         AccountDao accountDao = (AccountDao) applicationContext.getBean("accountDao"); 
  23.         // 調(diào)用實(shí)例中的轉(zhuǎn)賬方法 
  24.         accountDao.transfer("千羽""千尋", 100.0); 
  25.         // 輸出提示信息 
  26.         System.out.println("轉(zhuǎn)賬成功!"); 
  27.     } 

基于Annotation方式的聲明式事務(wù)

在Spring容器中注冊(cè)事務(wù)注解驅(qū)動(dòng);

  1. <tx:annotation-driven transaction-manager="transactionManager"/> 

在需要事務(wù)管理的類或方法上使用@Transactional注解。

如果將注解添加在Bean類上,則表示事務(wù)的設(shè)置對(duì)整個(gè)Bean類的所有方法都起作用;如果將注解添加在Bean類中的某個(gè)方法上,則表示事務(wù)的設(shè)置只對(duì)該方法有效。

使用@Transactional注解時(shí),可以通過參數(shù)配置事務(wù)詳情:

applicationContext-annotation.xml

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <beans xmlns="http://www.springframework.org/schema/beans" 
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop" 
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xmlns:context="http://www.springframework.org/schema/context" 
  7.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  8.     http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
  9.     http://www.springframework.org/schema/tx  
  10.     http://www.springframework.org/schema/tx/spring-tx-4.3.xsd 
  11.     http://www.springframework.org/schema/context  
  12.     http://www.springframework.org/schema/context/spring-context-4.3.xsd 
  13.     http://www.springframework.org/schema/aop  
  14.     http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"> 
  15.     <!-- 1.配置數(shù)據(jù)源 --> 
  16.     <bean id="dataSource"  
  17.     class="org.springframework.jdbc.datasource.DriverManagerDataSource"
  18.       <!--數(shù)據(jù)庫驅(qū)動(dòng) --> 
  19.       <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
  20.       <!--連接數(shù)據(jù)庫的url --> 
  21.       <property name="url" value="jdbc:mysql://localhost/spring?useSSL=false" /> 
  22.       <!--連接數(shù)據(jù)庫的用戶名 --> 
  23.       <property name="username" value="root" /> 
  24.       <!--連接數(shù)據(jù)庫的密碼 --> 
  25.       <property name="password" value="123456" /> 
  26.    </bean> 
  27.    <!-- 2.配置JDBC模板 --> 
  28.    <bean id="jdbcTemplate"  
  29.             class="org.springframework.jdbc.core.JdbcTemplate"
  30.       <!-- 默認(rèn)必須使用數(shù)據(jù)源 --> 
  31.       <property name="dataSource" ref="dataSource" /> 
  32.    </bean> 
  33.    <!--3.定義id為accountDao的Bean --> 
  34.    <bean id="accountDao" class="com.nateshao.jdbc.AccountDaoImpl"
  35.       <!-- 將jdbcTemplate注入到AccountDao實(shí)例中 --> 
  36.       <property name="jdbcTemplate" ref="jdbcTemplate" /> 
  37.    </bean> 
  38.    <!-- 4.事務(wù)管理器,依賴于數(shù)據(jù)源 --> 
  39.    <bean id="transactionManager" class= 
  40.      "org.springframework.jdbc.datasource.DataSourceTransactionManager"
  41.       <property name="dataSource" ref="dataSource" /> 
  42.    </bean>     
  43.     <!-- 5.注冊(cè)事務(wù)管理器驅(qū)動(dòng) --> 
  44.    <tx:annotation-driven transaction-manager="transactionManager"/> 
  45. </beans> 

TransactionTest.java

  1. package com.nateshao.jdbc; 
  2.  
  3. import org.junit.jupiter.api.Test; 
  4. import org.springframework.context.ApplicationContext; 
  5. import org.springframework.context.support.ClassPathXmlApplicationContext; 
  6.  
  7. /** 
  8.  * @date Created by 邵桐杰 on 2021/10/15 22:05 
  9.  * @微信公眾號(hào) 程序員千羽 
  10.  * @個(gè)人網(wǎng)站 www.nateshao.cn 
  11.  * @博客 https://nateshao.gitee.io 
  12.  * @GitHub https://github.com/nateshao 
  13.  * @Gitee https://gitee.com/nateshao 
  14.  * Description: 
  15.  */ 
  16. public class TransactionTest { 
  17.     @Test 
  18.     public void annotationTest() { 
  19.         ApplicationContext applicationContext = 
  20.                 new ClassPathXmlApplicationContext("applicationContext-annotation.xml"); 
  21.         // 獲取AccountDao實(shí)例 
  22.         AccountDao accountDao = 
  23.                 (AccountDao) applicationContext.getBean("accountDao"); 
  24.         // 調(diào)用實(shí)例中的轉(zhuǎn)賬方法 
  25.         accountDao.transfer("千尋111""千羽111", 100.0); 
  26.         // 輸出提示信息 
  27.         System.out.println("轉(zhuǎn)賬成功!"); 
  28.     } 
  29.  

總結(jié)

本章主要對(duì)Spring中的事務(wù)管理進(jìn)行了詳細(xì)講解。

  • 首先講解了Spring事務(wù)管理所涉及的3個(gè)核心接口,
  • 然后對(duì)Spring中事務(wù)管理的兩種方式進(jìn)行了介紹,
  • 最后通過案例分別對(duì)基于XML方式和基于Annotation方式的聲明式事務(wù)處理的使用進(jìn)行了詳細(xì)講解。 

通過本章的學(xué)習(xí),我相信大家可以對(duì)Spring的事務(wù)管理知識(shí)有一定的了解,并能夠掌握Spring聲明式事務(wù)管理的使用。

 

責(zé)任編輯:武曉燕 來源: 程序員千羽
相關(guān)推薦

2024-07-10 11:26:18

2020-11-16 13:38:31

PostMessage

2021-07-28 06:10:47

拖拽設(shè)計(jì)器 transmat

2021-09-05 07:55:37

前端Emoji 表情

2012-07-13 11:32:16

網(wǎng)絡(luò)出口

2020-09-14 11:26:54

BinlogCanal數(shù)據(jù)庫

2024-08-02 08:38:20

Controller接口地址

2009-06-30 16:57:42

Spring事務(wù)管理

2023-10-08 08:28:10

Spring事務(wù)管理

2009-06-17 14:57:11

Spring事務(wù)管理

2014-08-25 09:12:47

Spring事務(wù)管理

2009-06-08 17:56:00

SpringJDBC事務(wù)

2023-03-27 10:40:09

2009-02-11 11:14:31

事務(wù)管理事務(wù)開始Spring

2009-02-11 13:08:29

事務(wù)提交事務(wù)管理Spring

2024-05-13 00:47:37

JSON對(duì)象數(shù)據(jù)

2010-03-29 13:34:15

ibmdwSpring

2010-03-23 08:46:40

Spring

2009-09-25 12:59:53

Hibernate事務(wù)

2024-12-03 09:45:34

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)