Hibernate错误:无法在活动的全局事务中打开自动提交

时间:2013-10-11 06:45:47

标签: oracle hibernate jboss

我在部署应用程序时遇到错误。

环境是:

  • Jboss 7.1.3 + Hibernate 4 + JPA
  • OJDBC:ojdbc6.jar

堆栈跟踪如下,

09:13:30,703 WARN  [org.hibernate.engine.jdbc.internal.JdbcServicesImpl] (ServerService Thread Pool -- 127) HHH000341: Could not obtain connection metadata : could not turn on auto-commit in an active global transaction
09:13:30,703 INFO  [org.hibernate.engine.jdbc.internal.LobCreatorBuilder] (ServerService Thread Pool -- 127) HHH000422: Disabling contextual LOB creation as connection was null
09:13:30,704 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 127) MSC00001: Failed to start service jboss.persistenceunit."css-ear-0.5.3-SIT.ear/css-submission-0.5.3-SIT.war#css-submission-PU": org.jboss.msc.service.StartException in service jboss.persistenceunit."css-ear-0.5.3-SIT.ear/css-submission-0.5.3-SIT.war#css-submission-PU": java.lang.NullPointerException
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:100) [jboss-as-jpa-7.1.3.Final.jar:7.1.3.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_21]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_21]
    at java.lang.Thread.run(Thread.java:722) [rt.jar:1.7.0_21]
    at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.0.0.GA.jar:2.0.0.GA]
Caused by: java.lang.NullPointerException
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:207)
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159)
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131)
    at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:73)
    at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2279)
    at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2275)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1744)
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:890)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:197) [jboss-as-jpa-7.1.3.Final.jar:7.1.3.Final]
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.access$500(PersistenceUnitServiceImpl.java:57) [jboss-as-jpa-7.1.3.Final.jar:7.1.3.Final]
    at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:96) [jboss-as-jpa-7.1.3.Final.jar:7.1.3.Final

standalone.xml中的数据源配置是

<xa-datasource jta="true" jndi-name="java:jboss/datasources/PsaDS" pool-name="PsaDS" enabled="true" use-java-context="true" use-ccm="true">
  <xa-datasource-property name="URL">
        jdbc:oracle:thin:@<hsot>
  </xa-datasource-property>
  <driver>oracle</driver>
  <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
  <xa-pool>
        <min-pool-size>2</min-pool-size>
        <max-pool-size>50</max-pool-size>
        <prefill>true</prefill>
  </xa-pool>
  <security>
        <user-name>user</user-name>
        <password>pwd</password>
  </security>

我们使用的驱动程序是,

<drivers>
  <driver name="oracle" module="com.oracle.ojdbc6">
        <xa-datasource-class>oracle.jdbc.xa.client.OracleXADataSource</xa-datasource-class>
  </driver></drivers>

2 个答案:

答案 0 :(得分:1)

我从未使用过JBoss + Hibernate。所以我不尝试这种调整。只阅读一些文档。请尝试这些调整:

A)Hibernate属性

需要调整hibernate.properties。我在文档中找到了这个可选属性:

  

hibernate.connection.autocommit

     

为JDBC池连接启用自动提交(不是   推荐)。

     

e.g。是的|假

documentation link

B)standalone.xml

  

<share-prepared-statements> - 目的是解决方法   驱动程序应用自动提交时可疑的驱动程序行为   语义到本地事务。准备好语句缓存   同一事务中的两个请求是否应该返回相同   声明(来自jboss-4.0.2 - 默认为false)。

Connection c = dataSource.getConnection(); // auto-commit == false 
PreparedStatement ps1 = c.prepareStatement(...);    
ResultSet rs1 = ps1.executeQuery();    
PreparedStatement ps2 = c.prepareStatement(...);    
ResultSet rs2 = ps2.executeQuery();
  

假设准备好的陈述是相同的。对于一些司机,   ps2.executeQuery()会自动关闭rs1,所以我们确实需要   幕后的两个真实准备的陈述。这应该只是   对于自动提交语义,其中重新运行查询开始一个新的   交易自动。对于遵循规范的驱动程序,您可以   将其设置为true以共享相同的真实预处理语句。

documentation link

答案 1 :(得分:1)

尝试对<xa-datasource>定义标记中的standalone.xml进行以下更改:

<xa-pool>标记内添加以下内容:

<is-same-rm-override>false</is-same-rm-override>
<no-tx-separate-pools>true</no-tx-separate-pools>

根据文件:

  

no-tx-separate-pools:此元素的存在表示需要两个连接池来隔离与JTA事务一起使用的连接与不使用JTA事务的连接。游泳池在首次使用时可以舒适地建造。它的用例适用于Oracle(以及可能的其他供应商)XA实现,它们不喜欢在有和没有JTA事务的情况下使用XA连接。

如果这不能单独使用,请尝试在<xa-datasource>定义中添加:

<validation>
      <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleValidConnectionChecker"/>
      <stale-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleStaleConnectionChecker"/>
      <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter"/>
</validation>