我最近已更新为Hibernate 4.2.8.Final
,并且也尝试了4.3.1.Final
,但是,当使用Spring Framework 3.1在交易上下文中使用find
方法时,我已经开始体验以下例外。我正在使用Bitronix Transaction Manager 2.4.3。我不确定我是否以正确的方式使用冬眠,但是正如我所描述的,只有在更新冬眠版本之后才开始。
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" />
提供商类从 org.hibernate.ejb.HibernatePersistence
更改为新版本的Hibernate中的org.hibernate.jpa.HibernatePersistenceProvider
检查是否解决了您的问题。