0

我在 JBoss-EAP 7.2.6 中有一个可能长时间运行的事务,我需要在几个步骤中提交。

我了解 MariaDB 没有嵌套的但链式事务。这是我对它应该如何工作的想法:

public class MyEntityManager implements IMyEntityManager {
    @PersistenceContext(unitName = "org.x.y.z")
    protected EntityManager em;

    @Transactional(timeout = 900) // 15 minutes
    public void doTransaction(MyEntity me) {
        // Spring has a misfeature that @Transactional only applies when called from the outside.
        // Calling through an external reference works around this shortcoming. :-(
        MyEntityManager self = this;

        self.persist(me);
        publish(me.id); // Pseudocode, my read-only client eagerly waiting for id.

        // Fill me with more data
        self.flush();
        // Fill me with more data
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    private void persist(MyEntity me) {
        em.persist(me);
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    private void flush() {
        em.flush();
    }
}

我试图去NESTED,并将驱动程序从 2.4.3 升级到 2.5.4 无济于事。

如何让这样的场景与 InnoDB 一起工作?我愿意自己发布一个低级别COMMIT AND CHAIN,因为在@Spring/Hibernate 中似乎没有这样做的 API。

我试过

em.createNativeQuery("COMMIT AND CHAIN").executeUpdate();

但这导致

XAER_RMFAIL: The command cannot be executed when global transaction is in the  ACTIVE state

尽管

em.unwrap(Session.class).createSQLQuery("COMMIT AND CHAIN").executeUpdate();

导致

GenericJDBCException: could not execute statement
4

1 回答 1

0

仅仅复制thisself(我在其他地方找到的)是不够的。它需要self = applicationContext.getBean(IMyEntityManager.class). 然后NESTED抛出不受支持。而REQUIRES_NEW完成交易,之后me不再管理。

我有 xa 数据源,并且 XA 事务不与正常commit的 . 在我切换到普通数据源后,em.createNativeQuery("COMMIT AND CHAIN").executeUpdate()它就像一个魅力。

顺便提一句。我在 Arjuna 的 C++ 源代码中发现了一个chainTransaction似乎没有导出到 Java 的方法。但我发现它只是做了另一个BEGIN似乎暗示 a 的COMMIT事情,所以这可能是实现这一目标的可移植方式。

于 2020-03-25T11:38:44.943 回答