仅在确定要提交但在提交之前拦截事务 [英] Intercept transaction only when is sure to be committed but before is committed

查看:198
本文介绍了仅在确定要提交但在提交之前拦截事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Context是Java - 带有Hibernate和Spring的JPA。

Context is Java - JPA with Hibernate and Spring.

让我们采用两阶段提交协议的场景(但只有一个资源):

Let's take the scenario of two-phase commit protocol (but only with one resource):


  1. 从申请提交的查询

  1. Query to commit from application

投票是/否(来自我们案例中的数据库)

Vote Yes/No (from database in our case)

3.1。如果是,则来自数据库

3.1. If yes from database

3.1.1。 (在代码中进行回调) - 不是协议的一部分

3.1.1. (Make callback in code) - not part of the protocol

3.1.2。提交到数据库

3.1.2. Commit to database

3.2如果没有

3.2.1回滚到数据库

3.2.1 Rollback to database

我想要的是一种在代码中从3.1.1进行回调的方法,但只有当知道事务将被提交但在实际提交之前。此外,如果此处抛出异常,则应回滚该事务。

What I want is a way to do the callback from 3.1.1 in code, but only when it is known that the transaction will be committed, but before is actually committed. Also, if an exception is thrown here, then the transaction should be be rolled-back.

使用 TransactionSynchronization ( *)来自Spring,允许您在交易提交/完成之前或提交/完成之后拦截交易。

Using TransactionSynchronization (*) from Spring, allows you to intercept a transaction before it is committed/completed or after it was committed/completed.


  • beforeCommit() callback表示在调用方法后仍然可以发生回滚;

  • 即使交易失败,也会调用beforeComplete()

  • afterCommit / Complete()在事务实际提交到数据库后被调用,并且无法回滚。

  • beforeCommit() callback says that a rollback can still occur after the method was called;
  • beforeComplete() is called even if transaction is failing
  • afterCommit/Complete() is called after transaction was actually committed to database and there is no way to rollback.

现在看来,我想要的是另一个完整的两阶段提交协议;但我想知道Spring中是否有解决方法。区别在于回调中完成的调用无法回滚。

Now that I look it seems that what I want is another in a full two-phase commit protocol; but I'm wondering if there is a workaround in Spring. The difference is that the call done in the callback cannot be rolled back.

(*)来自Spring 4.2非常简单,只需 @TransactionalEventListener TransactionPhase 哪些很好地抽象 TransactionSynchronization

(*) from Spring 4.2 is very simple with @TransactionalEventListener and TransactionPhase which nicely abstracts TransactionSynchronization

推荐答案

您的情况是您的某个资源与两阶段提交不兼容(不是支持XA的)。您的想法是针对 XA和最后资源开发 段落中描述的模式的方向2077963 / open-source-tools / distributed-transactions-in-spring - with-and-without-xa.htmlrel =nofollow noreferrer> http://www.javaworld.com/article/2077963/open- source-tools / distributed-transactions-in-spring - with-and-without-xa.html

Your case is that one of your resource is not compatible with two-phase-commit (not XA-capable). Your idea goes in the direction of the pattern described in paragraph XA and the Last Resource Gambit of http://www.javaworld.com/article/2077963/open-source-tools/distributed-transactions-in-spring--with-and-without-xa.html

使用最后一个资源在 -jms-connection>如何设置Spring Boot + Bitronix +非XA数据源+ XA JMS连接

The use of the last resource gambit is briefly explained in the answer of How to set up Spring Boot + Bitronix + non-XA Datasource + XA JMS Connection

顺便问一下,你的问题不是'提到你使用的事务管理器的实现(JBossTS,Bitronix JTA,Atomikos Transaction Essentials,...)。

By the way, your question doesn't mention which implementation of a transaction manager you use (JBossTS, Bitronix JTA, Atomikos Transaction Essentials, ...).

这篇关于仅在确定要提交但在提交之前拦截事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆