Transaction rolled back because it hasbeen marked as rollback-only
时间: 2023-11-07 19:15:16 浏览: 99
当出现 "Transaction rolled back because it has been marked as rollback-only" 的异常时,通常是因为事务在某个方法中被标记为只能回滚,但后续的代码却试图提交该事务,从而导致异常的抛出。
一个例子是在方法A中捕获了方法B抛出的异常,并继续执行后续的代码,最后正常提交事务。然而,由于AB使用同一个事务,在方法B执行时,该事务会被标记为rollback-only,然后方法A继续使用该事务并执行事务提交的操作,因此最终会抛出异常。
要解决这个问题,可以在方法A中将异常继续往上抛出,而不是在catch块中处理异常,这样事务就会正确地回滚。例如,在方法A的catch块中将异常重新抛出,或者将异常声明为方法A抛出的异常之一。
另外,还可以通过在方法B中使用@Transactional注解来确保方法B在出现异常时事务正确回滚,以避免这个异常的出现。
总之,"Transaction rolled back because it has been marked as rollback-only" 异常通常是由于事务在被标记为rollback-only后继续提交引起的,可以通过将异常继续往上抛出或使用@Transactional注解来解决这个问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [COS——R.log](https://download.csdn.net/download/ktc7000/4424623)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [Transaction rolled back because it has been marked as rollback-only](https://blog.csdn.net/hingli/article/details/118415444)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文