休眠| JBOSS MSSQL数据源错误

时间:2011-04-21 06:36:40

标签: sql-server hibernate jboss

当我尝试提交数据库时,我得到以下异常

    java.sql.SQLException: You cannot commit with autocommit set!
        at org.jboss.resource.adapter.jdbc.BaseWrapperManagedConnection.jdbcComm
it(BaseWrapperManagedConnection.java:545)
        at org.jboss.resource.adapter.jdbc.WrappedConnection.commit(WrappedConne
ction.java:334)
        at net.sf.hibernate.id.TableGenerator.generate(TableGenerator.java:126)
        at net.sf.hibernate.id.TableHiLoGenerator.generate(TableHiLoGenerator.ja
va:59)

这是我的JBOSS(jboss-4.0.5.GA)mssql-ds.xml: -

<?xml version="1.0" encoding="UTF-8"?>
<datasources>
  <local-tx-datasource>
  <jndi-name>TESTDS</jndi-name>
  <connection-url>jdbc:sqlserver://localhost:1433;DatabaseName=TESTDB</connection-url>
  <driver-class>com.microsoft.sqlserver.jdbc.SQLServerDriver</driver-class>
  <user-name>sa</user-name>
  <password>password</password>
  <check-valid-connection-sql>SELECT 1 FROM sysobjects</check-valid-connection-sql>
  <metadata>
      <type-mapping>MS SQLSERVER2000</type-mapping>
  </metadata>
  </local-tx-datasource>
</datasources> 

以及我的提交操作(带事务): -

  public synchronized void commitTransaction() throws TransactionException {
    Transaction tx = (Transaction) localTransaction.get();
    Session session = (Session) localSession.get();
    try {
      tx.commit();
    }
    catch (HibernateException e) {
      log.error("Error closing the persistence when commiting.", e);
      rollbackTransaction();
      throw new TransactionException(e);
    }
    finally {
      try {
        session.close();
      }
      catch (HibernateException e) {
        log.fatal("Session could not be closed !!!", e);
      }
      localSession.set(null);
      localTransaction.set(null);
    }
    log.info("Commiting transaction with thread : " + Thread.currentThread());
  }

UPDATE: Hibernate配置文件(hibernate.cfg.xml)

<!-- SQLSERVER configuration -->

<property name="dialect">net.sf.hibernate.dialect.SQLServerDialect</property>

<property name="connection.datasource">java:/TESTDS</property>

<property name="connection.pool_size">100</property>
<property name="statement_cache.size">200</property>

    <property name="transaction.factory_class">net.sf.hibernate.transaction.JDBCTransactionFactory</property>

<property name="show_sql">false</property>
<property name="use_outer_join">true</property>

    <!-- Hibernate mapping files configuration -->

           ..............

    <!-- Hibernate mapping files configuration -->

我尝试在网上搜索,但无法找到解决此问题的任何方法。

感谢。

4 个答案:

答案 0 :(得分:1)

使用来自JavaEE容器管理的DataSource的连接时,无法使用JDBC API控制事务。

默认情况下,容器决定在业务代码结束时提交(或回滚),或者将其写为Servlet,MessageDrivenBean onMessage或EJB方法。

如果要检查事务状态并最终决定将其标记为回滚 - 因为默认容器行为是提交 - 您必须在Servlet中从JDNI获取UserTransaction对象。

对于EJB,您可以使用相应的setter从实例接收的context获取UserTransaction对象。但它需要使用TransactionManagement注释或部署描述符将事务模式设置为“bean-managed”。

答案 1 :(得分:0)

您是否尝试过设置

hibernate.connection.autocommit = false?

取自hibernate documentation

答案 2 :(得分:0)

程序化交易管理代码让我感到害怕。

我建议你尝试使用Spring Declarative Transaction Management

您还应该在hibernate.cfg.xml中设置:

<property name="hibernate.connection.autocommit">false</property>

答案 3 :(得分:0)

几年前我遇到过类似的问题,因为我记得正确的情况如下

1) some ejb or web applications grabs a connection from jboss pool
2) it sets autocommit to true does some operations with it and the puts it back to pool
3) your ejb grabs the same connection from jboss pool
4) it's previous state of autocommit is set to true, thus transactions using these connection fail

要解决此问题,您应该在关闭连接之前将autocommit设置回以前的值,在所有自定义jdbc代码中