2011-04-21 63 views
1

,我发现了以下异常,当我尝试提交到数据库Hibernate | JBOSS MSSQL数据源错误

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配置文件(喜bernate.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 --> 

我试图寻遍网络,但无法找到这个问题的任何解决方案。

谢谢。

+0

你的hibernate.cfg.xml/persistence.xml如何看起来像? – jpkrohling 2011-04-21 13:08:14

+0

我已经更新了问题 – Switch 2011-04-22 05:56:41

回答

0

您是否尝试过设置

hibernate.connection.autocommit = false?

取自hibernate documentation

+0

在哪里把这个,在hibernate.cfg.xml? – Switch 2011-04-22 06:32:41

+0

嗯,这取决于。你使用.properties还是.xml?如果它是.xml,那么这就是它的发展方向。 – pengtuck 2011-04-27 03:57:25

0

编程事务管理代码让我害怕。我建议你尝试使用Spring Declarative Transaction Management

你也应该把hibernate.cfg.xml设置:

<property name="hibernate.connection.autocommit">false</property> 
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 

为了克服在您关闭连接之前,您应该将自动提交的设置恢复为之前的值,在您的所有自定义jdbc代码中

1

当您从由JavaEE容器管理的DataSource中唱一个Connection。

默认情况下,当您的业务代码结束时,容器会决定是否提交(或回滚),或者将它写为Servlet,MessageDrivenBean onMessage或EJB方法。

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

对于EJB,您可以通过context实例接收相应设置器的UserTransaction对象。但是它需要将事务模式设置为“注释管理”,或者使用TransactionManagement注释或部署描述符。