1. 程式人生 > >Spring事務管理——回滾(rollback-for)控制

Spring事務管理——回滾(rollback-for)控制

探討spring事務控制中,異常觸發事務回滾原理。文章進行了6種情況下的Spring事務是否回滾。 
以下程式碼都是基於Spring與Mybatis整合,使用Spring宣告式事務配置事務方法。

1.不捕獲異常(一般處理方式)

程式碼,其中contentMappger.updateWithErrTest(31L); 是SQL語句錯誤,用來測試回滾。

    /**
     * 刪除多條記錄
     */
    @Override
    public ShopResult deleteContentGroup(String[] ids) {
        if (null == ids || ids.length == 0
) { return ShopResult.error(); } for (String idStr : ids) { Long id = new Long(idStr); contentMappger.deleteByPrimaryKey(id); } contentMappger.updateWithErrTest(31L); //錯誤程式碼,SQL語句錯誤。用來測試事務,看是否回滾 return ShopResult.ok(); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

執行結果:報錯,事務發生了回滾,即由於錯誤程式碼,前面的for迴圈刪除記錄事務被回滾了。

### SQL: delete form tb_content    where kid = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for
the right syntax to use near 'tb_content where kid = 31' at line 1 ; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tb_content where kid = 31' at line 1] with root cause com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tb_content where kid = 31' at line 1 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) at com.mysql.jdbc.Util.getInstance(Util.java:381) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3536) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3468) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1957) .................. at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:53) at com.sun.proxy.$Proxy35.updateWithErrTest(Unknown Source) at com.shop.manager.service.impl.ContentServiceImpl.deleteContentGroup(ContentServiceImpl.java:94) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) .......
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

2. 捕獲異常,但不處理,不丟擲

程式碼

    /**
     * 刪除多條記錄
     */
    @Override
    public ShopResult deleteContentGroup(String[] ids) {
        if (null == ids || ids.length == 0)
        {
            return ShopResult.error();
        }
        for (String idStr : ids)
        {
            Long id = new Long(idStr);
            contentMappger.deleteByPrimaryKey(id);
        }
        try {
            contentMappger.updateWithErrTest(31L);   //錯誤程式碼,SQL語句錯誤。用來測試事務,看是否回滾
        } catch (Exception e) {
            //捕獲異常,但不處理
            System.out.println("-----nothing to do-------");
        }
        return ShopResult.ok();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

執行結果:事務提交,未回滾。 
事務提交

### The error occurred while setting parameters
### SQL: delete form tb_content    where kid = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tb_content
    where kid = 31' at line 1
]
-----nothing to do-------
2017-06-18 14:27:59,493 [http-bio-8080-exec-4] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization committing SqlSession //(事務提交)
[[email protected]616a85a9]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

3. 捕獲異常,並丟擲RuntimeException異常

Spring碰到Unchecked Exceptions都會回滾,不僅是RuntimeException,也包括Error。 
程式碼

    /**
     * 刪除多條記錄
     */
    @Override
    public ShopResult deleteContentGroup(String[] ids) {
        if (null == ids || ids.length == 0)
        {
            return ShopResult.error();
        }
        for (String idStr : ids)
        {
            Long id = new Long(idStr);
            contentMappger.deleteByPrimaryKey(id);
        }
        try {
            contentMappger.updateWithErrTest(31L);   //錯誤程式碼,SQL語句錯誤。用來測試事務,看是否回滾
        } catch (Exception e) {
            System.out.println("----throw Exception-----");
            throw new RuntimeException();
        }
        return ShopResult.ok();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

執行結果:如預期的一樣,丟擲RuntimeException,事務發生回滾。

### SQL: delete form tb_content    where kid = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tb_content
    where kid = 31' at line 1
]
----throw Exception-----
2017-06-18 14:21:27,928 [http-bio-8080-exec-1] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization deregistering SqlSession [[email protected]3ef56e3a]
...............
2017-06-18 14:21:27,941 [http-bio-8080-exec-1] [org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver]-[DEBUG] Resolving exception from handler [public com.shop.common.pojo.ShopResult com.shop.manager.controller.ContentController.deleteContentGroup(java.lang.String)]: java.lang.RuntimeException
2017-06-18 14:21:27,941 [http-bio-8080-exec-1] [org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver]-[DEBUG] Resolving exception from handler [public com.shop.common.pojo.ShopResult com.shop.manager.controller.ContentController.deleteContentGroup(java.lang.String)]: java.lang.RuntimeException
2017-06-18 14:21:27,942 [http-bio-8080-exec-1] [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver]-[DEBUG] Resolving exception from handler [public com.shop.common.pojo.ShopResult com.shop.manager.controller.ContentController.deleteContentGroup(java.lang.String)]: java.lang.RuntimeException
2017-06-18 14:21:27,942 [http-bio-8080-exec-1] [org.springframework.web.servlet.DispatcherServlet]-[DEBUG] Could not complete request
java.lang.RuntimeException
    at com.shop.manager.service.impl.ContentServiceImpl.deleteContentGroup(ContentServiceImpl.java:98)  //異常
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4.捕獲異常,並繼續丟擲原捕獲的異常

程式碼:

    /**
     * 刪除多條記錄
     */
    @Override
    public ShopResult deleteContentGroup(String[] ids) {
        if (null == ids || ids.length == 0)
        {
            return ShopResult.error();
        }
        for (String idStr : ids)
        {
            Long id = new Long(idStr);
            contentMappger.deleteByPrimaryKey(id);
        }
        try {
            contentMappger.updateWithErrTest(31L);   //錯誤程式碼,SQL語句錯誤。用來測試事務,看是否回滾
        } catch (Exception e) {
            //捕獲異常,繼續丟擲
            System.out.println("-----throw Exception-------");
            throw e;
        }
        return ShopResult.ok();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

執行結果:丟擲異常,事務發生回滾

### The error occurred while setting parameters
### SQL: delete form tb_content    where kid = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tb_content
    where kid = 31' at line 1
]
-----throw Exception-------
2017-06-18 14:36:25,308 [http-bio-8080-exec-9] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization deregistering SqlSession [[email protected]45fe0f70]
2017-06-18 14:36:25,308 [http-bio-8080-exec-9] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization closing SqlSession //事務回滾
[[email protected]45fe0f70]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

5. 捕獲異常,並丟擲新new的異常(或自定義Exception異常) new Exception

程式碼:

    /**
     * 刪除多條記錄
     * @throws Exception 
     */
    @Override
    public ShopResult deleteContentGroup(String[] ids) throws Exception {
        if (null == ids || ids.length == 0)
        {
            return ShopResult.error();
        }
        for (String idStr : ids)
        {
            Long id = new Long(idStr);
            contentMappger.deleteByPrimaryKey(id);
        }
        try {
            contentMappger.updateWithErrTest(31L);   //錯誤程式碼,SQL語句錯誤。用來測試事務,看是否回滾
        } catch (Exception e) {
            //捕獲異常,丟擲新異常
            System.out.println("-----throw new Exception(e)-------");
            throw new Exception(e);
        }       
        return ShopResult.ok();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

執行結果:事務提交,未回滾。(Spring的預設回滾異常型別不包括Exception)

### The error occurred while setting parameters
### SQL: delete form tb_content    where kid = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tb_content
    where kid = 31' at line 1
]
-----throw new Exception(e) -------
2017-06-18 14:43:16,098 [http-bio-8080-exec-10] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization committing SqlSession //事務提交
[[email protected]32c4821]
2017-06-18 14:43:16,098 [http-bio-8080-exec-10] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization deregistering SqlSession [[email protected]32c4821]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

6. 在事務配置中沒有設定rollback-for異常型別為Exception

<!-- 事務管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 事務行為控制 -->
        <tx:attributes>
            <tx:method name="save" propagation="REQUIRED" rollback-for="Exception"/>
            <tx:method name="insert*" propagation="REQUIRED"  rollback-for="Exception"/>
            <tx:method name="add*" propagation="REQUIRED"  rollback-for="Exception"/>
            <tx:method name="create*" propagation="REQUIRED"  rollback-for="Exception"/>
            <tx:method name="delete*" propagation="REQUIRED"  rollback-for="Exception"/>
            <tx:method name="update*" propagation="REQUIRED"  rollback-for="Exception"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- 配置切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.shop.manager.service.*.*(..))" />
    </aop:config>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
    /**
     * 刪除多條記錄
     * @throws Exception 
     */
    @Override
    public ShopResult deleteContentGroup(String[] ids) throws Exception {
        if (null == ids || ids.length == 0)
        {
            return ShopResult.error();
        }
        for (String idStr : ids)
        {
            Long id = new Long(idStr);
            contentMappger.deleteByPrimaryKey(id);
        }
        try {
            contentMappger.updateWithErrTest(31L);   //錯誤程式碼,SQL語句錯誤。用來測試事務,看是否回滾
        } catch (Exception e) {
            //捕獲異常,繼續丟擲
            System.out.println("-----throw new Exception-------");
            throw new Exception("---自定義Exception,事務中已配置rollback-for---");
        }
        return ShopResult.ok();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

執行結果:如預期一樣發生回滾

### The error may involve com.shop.manager.mapper.TbContentMapper.updateWithErrTest-Inline
### The error occurred while setting parameters
### SQL: delete form tb_content    where kid = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'tb_content
    where kid = 31' at line 1
]
-----throw new Exception-------
2017-06-18 15:07:02,273 [http-bio-8080-exec-8] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization deregistering SqlSession [[email protected]]
2017-06-18 15:07:02,273 [http-bio-8080-exec-8] [org.mybatis.spring.SqlSessionUtils]-[DEBUG] Transaction synchronization closing SqlSession [[email protected]]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

總結:

  1. Spring事務管理是根據異常來進行回滾操作;
  2. Spring與Mybatis整合時,雖然在Service方法中並沒有check異常,但是如果資料庫有異常發生,預設會進行事務回滾。
  3. Spring 如果不新增rollbackFor等屬性,Spring碰到Unchecked Exceptions都會回滾,不僅是RuntimeException,也包括Error。
  4. 如果在事務方法中捕獲異常並進行處理,一定要繼續丟擲異常並在Spring事務管理中進行rollbak-for配置。

相關推薦

Spring事務管理——(rollback-for)控制

探討spring事務控制中,異常觸發事務回滾原理。文章進行了6種情況下的Spring事務是否回滾。  以下程式碼都是基於Spring與Mybatis整合,使用Spring宣告式事務配置事務方法。 1.不捕獲異常(一般處理方式) 程式碼,其中contentMappg

spring 事務管理——之service層(事務控制層)程式碼互調

spring事務管理相關的文章已經有很多了,本人寫此文章主要為自己的實驗做一個記錄,年紀大了,記性不好 首先先貼幾個地址,有興趣研讀的同學可以參考一下: 初級使用: 初級容易犯的錯:事務中catch異常 官方介紹: 預設回滾配置實驗: 以上幾個地址是從不同的角度來

Spring 事務手動: 用於事務管理的業務方法中使用了try...catch...的事務

在使用Spring 事務(@Transactional())時,被事務管理的業務類方法中如果使用try...catch...來捕獲異常的話,如果出現異常,事務不會回滾,這個時候我們可以手動回滾事務.如下: //假設這是被事務管理的service類中的一個方法

spring事務

檢查 出現異常 alt clas ransac service info 手動 ons 最近遇見一個問題,用spring管理實務,在service層處理數據,保存數據時出現異常,但沒有回滾,檢查了一下,發現是因為我用try catch將異常進行捕獲了,沒有拋出導

Spring事務異常,捕獲異常不拋出就不會

actions .info time tpi detail ava ogg ren tool 最近遇到了事務不回滾的情況,我還考慮說JPA的事務有bug? 我想多了....... 為了打印清楚日誌,很多方法我都加tyr catch,在catch中打印日誌。但是這邊情

springmvc mybatis 聲明式事務管理失效,(checked)捕捉異常,傳輸錯誤信息

title HR 業務邏輯 mybatis分頁 能力 csdn before 取值 request 一、知識點及問題 後端框架: Spring 、Spring

(轉)Spring事務異常機制(出處在文末,轉自李慕白大神)

Spring事務異常回滾,捕獲異常不丟擲就不會回滾 推薦:Spring transaction 事務 --Isolation & Progation Java異常處理主要通過5個關鍵字控制:try、catch、throw、throws和finally。try的意思是試試它所包含的

Spring事務異常。TransactionAspectSupport(學習理解)

例:      類似這樣的方法不會回滾 (一個方法出錯,另一個方法不會回滾) :   if(userSave){    try {    userDao.save(user);

java事務異常——Spring事務異常,捕獲異常不丟擲就不會

最近遇到了事務不回滾的情況,我還考慮說JPA的事務有bug? 我想多了.......     為了列印清楚日誌,很多方法我都加tyr catch,在catch中列印日誌。但是這邊情況來了,當這個方法異常時候 日誌是列印了,但是加的事務卻沒有回滾。   例:      類

Spring事務,捕獲異常後不丟擲不

專案中一個對外提供的介面用try catch捕獲異常後不會滾 類似這樣的方法不會回滾 (一個方法出錯,另一個方法不會回滾) : if(userSave){ try { userDao.save

十六、Spring事務異常,捕獲異常不丟擲就不會

最近遇到了事務不回滾的情況,我還考慮說JPA的事務有bug? 我想多了……. 為了列印清楚日誌,很多方法我都加tyr catch,在catch中列印日誌。但是這邊情況來了,當這個方法異常時候 日誌是列印了,但是加的事務卻沒有回滾。 例: 類似這樣的方法不會回滾 (一個方法出錯,另一個方

Spring事務異常,捕獲異常不丟擲就不會

最近遇到了事務不回滾的情況,我還考慮說JPA的事務有bug? 我想多了.......     為了列印清楚日誌,很多方法我都加tyr catch,在catch中列印日誌。但是這邊情況來了,當這個方法異常時候 日誌是列印了,但是加的事務卻沒有回滾。   例:      

Spring事務處理案例總結 rollback-for使用

spring只是控制資料庫的事務提交和回滾,藉助於java的反射機制,在事務控制的方法(通常是service層的方法)前後獲取事務開啟session,然後執行你的資料操作,如果你的方法內有異常被丟擲,spring會捕獲異常並回滾你在這個方法內所有的資料操作,如果成功則提交所有

Spring事務管理異常rollback-for

        <!-- 事務配置 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="d

spring boot開啟事務管理,使用事務機制,使兩條插入語句一致

value nbsp tcl true 管理 配置 AI let dao spring boot 事務管理,使用事務的回滾機制 1:配置事務管理 在springboot 啟動類中添加 @EnableTransactionManagement //開啟事務管

spring事務管理,基於xml配置完成事務spring中資料庫表中欄位名和pojo中屬性名不一致時候,實現RowMapper介面手動封裝

宣告使用JDK8,spring5.0.7, 測試說明: service 層 宣告介面進行轉賬,從A轉賬B ,然後對AB 進行更新操作,在事務中對find方法開啟 只讀許可權,無法進行更新操作,造成事務回滾進行測試事務; 主要測試方法:* void tra

Spring事務管理只對出現執行期異常進行

使用spring難免要用到spring的事務管理,要用事務管理又會很自然的選擇宣告式的事務管理,在spring的文件中說道,spring宣告式事務管理預設對非檢查型異常和執行時異常進行事務回滾,而對檢查型異常則不進行回滾操作。那麼什麼是檢查型異常什麼又是非檢查型異常呢?最簡

Spring管理事務預設的異常是什麼?

問題: Spring管理事務預設(即沒有rollBackFor的情況下)可以回滾的異常是什麼? 回答: RuntimeException或者Error。 丟擲執行時異常,是否回滾?Yes @Trans

關於MySQL中的事務rollback)、提交(commit)

0 場景 需要連續對同一資料表進行增刪改操作時,我們會分別執行多條sql語句。 此時可能會出現兩類問題: 所有的語句執行完成後,發現其中一句執行錯了 某一句執行完成後,後悔了想要重新執行 遇到上述問題,能夠時光倒流和吃後悔藥嗎? MySQL的Innodb引

spring事務——try{...}catch{...}中事務的幾種處理方式

當希望在某個方法中新增事務時,我們常常在方法頭上新增@Transactional註解 @ResponseBody @RequestMapping(value = "/payment", method = RequestMethod.POST, produces = MediaType