使用Hibernate多租户时不会提交JTA事务

使用Hibernate多租户时不会提交JTA事务,hibernate,jboss,wildfly,multi-tenant,jta,Hibernate,Jboss,Wildfly,Multi Tenant,Jta,这个问题被发布在Hibernate论坛上,没有很好的解决方案。希望在这里获得更多的能见度: 我们使用多租户的Wildfly 18,使用每个租户的数据库策略。 数据源是使用commons-dbcp2动态创建的,未在Wildfly XML文件中配置。 由于各种原因,我们无法通过典型的Wildfly配置来配置数据源 应用程序使用带有@Transactional 问题: Wildfly/Hibernate从不在成功的事务上调用提交。 如果我们将连接设置为autoCommit=false,则它们永远不会

这个问题被发布在Hibernate论坛上,没有很好的解决方案。希望在这里获得更多的能见度:

我们使用多租户的Wildfly 18,使用每个租户的数据库策略。 数据源是使用commons-dbcp2动态创建的,未在Wildfly XML文件中配置。 由于各种原因,我们无法通过典型的Wildfly配置来配置数据源

应用程序使用带有
@Transactional

问题: Wildfly/Hibernate从不在成功的事务上调用提交。 如果我们将连接设置为autoCommit=false,则它们永远不会提交。 如果我们将它们设置为autoCommit=true,则它们永远不会回滚

黑客: 我们必须创建一个自定义拦截器来提交事务。希望有一个更干净的方法

相关persistence.xml:

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
    version="2.1">
    <persistence-unit name="default" transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.multiTenancy" value="DATABASE" />
            <property name="hibernate.multi_tenant_connection_provider" value="com.blah.DeploymentIdConnectionProviderImpl" />
            <property name="hibernate.tenant_identifier_resolver" value="com.blah.DeploymentIdIdentifierResolverImpl" />
            <property name="hibernate.connection.provider_disables_autocommit" value="true"/>
            <property name="hibernate.connection.handling_mode" value="DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION"/>
            <property name="hibernate.session_factory.session_scoped_interceptor" value="com.blah.TransactionInterceptor"/>
            <property name="jboss.entity.manager.factory.jndi.name" value="java:jboss/EntityManagerFactory"/>
            <property name="jboss.entity.manager.jndi.name" value="java:jboss/EntityManager"/>
        </properties>
    </persistence-unit>
</persistence>
public class TransactionInterceptor extends EmptyInterceptor {

    @Override
    public void beforeTransactionCompletion(Transaction tx) {
        EntityManager entityManager = // lookup from JNDI
        Session session = entityManager.unwrap(Session.class);
        if (entityManager.isJoinedToTransaction()) {
            session.flush();
            session.doWork(Connection::commit);
        }
    }
}


您可能正在Hibernate上使用乐观锁机制,该机制基于版本控制对象。在事务之前使用悲观锁定。