Hibernate - Bitronix - Spring - 无法登记多个非XA资源

时间:2014-01-25 13:16:32

标签: java spring hibernate bitronix

我最近更新了Hibernate 4.2.8.Final并尝试了4.3.1.Final但是,在使用spring框架3.1在事务上下文中使用find方法时,我已经开始遇到以下异常。我正在使用Bitronix事务管理器2.4.3。我不确定我是否以正确的方式使用hibernate,但正如我所描述的,此问题仅在更新hibernate版本后才开始。

Caused by: org.hibernate.exception.GenericJDBCException: could not prepare statement
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:196)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:160)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.prepareQueryStatement(AbstractLoadPlanBasedLoader.java:257)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeQueryStatement(AbstractLoadPlanBasedLoader.java:201)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:137)
    at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:102)
    at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:186)
    at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4120)
    at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:502)
    at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:467)
    at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:212)
    at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:274)
    at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:150)
    at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1066)
    at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
    at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2533)
    at org.hibernate.internal.SessionImpl.get(SessionImpl.java:1019)
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:1104)
    ... 76 more
Caused by: java.sql.SQLException: error enlisting a JdbcConnectionHandle of a JdbcPooledConnection from datasource defaultDataSource in state ACCESSIBLE with usage count 1 wrapping a JDBC LrcXAConnection on com.mysql.jdbc.JDBC4Connection@11d1bb73 on a JDBC LrcConnectionHandle on a JDBC LrcXAResource in state NO_TX
    at bitronix.tm.resource.jdbc.JdbcConnectionHandle.enlistResource(JdbcConnectionHandle.java:87)
    at bitronix.tm.resource.jdbc.JdbcConnectionHandle.prepareStatement(JdbcConnectionHandle.java:242)
    at sun.reflect.GeneratedMethodAccessor43.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at bitronix.tm.resource.jdbc.BaseProxyHandlerClass.invoke(BaseProxyHandlerClass.java:64)
    at com.sun.proxy.$Proxy23.prepareStatement(Unknown Source)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:162)
    at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186)
    ... 93 more
Caused by: bitronix.tm.internal.BitronixSystemException: cannot enlist more than one non-XA resource, tried enlisting an XAResourceHolderState with uniqueName=defaultDataSource XAResource=a JDBC LrcXAResource in state NO_TX with XID null, already enlisted: an XAResourceHolderState with uniqueName=defaultDataSource XAResource=a JDBC LrcXAResource in state STARTED (started) with XID a Bitronix XID [737072696E672D62746D0000000023E25108000007C6 : 737072696E672D62746D0000000023E2510F000007CC]
    at bitronix.tm.internal.XAResourceManager.enlist(XAResourceManager.java:107)
    at bitronix.tm.BitronixTransaction.enlistResource(BitronixTransaction.java:130)
    at bitronix.tm.resource.common.TransactionContextHelper.enlistInCurrentTransaction(TransactionContextHelper.java:69)
    at bitronix.tm.resource.jdbc.JdbcConnectionHandle.enlistResource(JdbcConnectionHandle.java:85)
    ... 101 more

persistence.xml配置:

<persistence-unit name="default" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>default</jta-data-source>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
</persistence-unit>

Spring dataSource和bitronix配置:

<bean id="defaultDataSource" class="bitronix.tm.resource.jdbc.PoolingDataSource"
    depends-on="transactionManager" init-method="init" destroy-method="close">
    <property name="className" value="bitronix.tm.resource.jdbc.lrc.LrcXADataSource" />
    <property name="uniqueName" value="defaultDataSource" />
    <property name="minPoolSize" value="${ds.default.minPoolSize}" />
    <property name="maxPoolSize" value="${ds.default.maxPoolSize}" />
    <property name="maxIdleTime" value="${ds.default.maxIdleTime}" />
    <property name="useTmJoin" value="true" />
    <property name="deferConnectionRelease" value="true" />
    <property name="automaticEnlistingEnabled" value="true" />
    <property name="allowLocalTransactions" value="true" />
    <property name="isolationLevel" value="READ_COMMITTED" />
    <property name="shareTransactionConnections" value="false"/>
    <property name="driverProperties">
        <props>
            <prop key="driverClassName">${ds.default.driverClass}</prop>
            <prop key="url">${ds.default.url}</prop>
            <prop key="user">${ds.default.user}</prop>
            <prop key="password">${ds.default.password.encrypted}</prop>
        </props>
    </property>
</bean>

<bean id="bitronixConfig" factory-method="getConfiguration" class="bitronix.tm.TransactionManagerServices">
    <property name="serverId" value="spring-btm" />
    <property name="logPart1Filename" value="${catalina.base}/logs/tlog/btm1.tlog" />
    <property name="logPart2Filename" value="${catalina.base}/logs/tlog/btm2.tlog" />
    <property name="defaultTransactionTimeout" value="600" />
    <property name="warnAboutZeroResourceTransaction" value="false" />
    <property name="journal" value="null" />
</bean>

<bean id="bitronixTransactionManager" class="bitronix.tm.TransactionManagerServices"
      factory-method="getTransactionManager" destroy-method="shutdown" depends-on="bitronixConfig" />

1 个答案:

答案 0 :(得分:0)

在新版Hibernate中,提供程序类从org.hibernate.ejb.HibernatePersistence更改为org.hibernate.jpa.HibernatePersistenceProvider,检查是否解决了您的问题。