2011-04-21 28 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註釋或部署描述符。