2012-02-24 66 views
0

我正在使用hibernate進行數據庫映射。 但遇到了以下錯誤:休眠異常:無法插入收集行

A exec job config finished. 
org.hibernate.exception.JDBCConnectionException: could not insert collection rows: [com.myCompany.jobsrc.ExecJob.subRunningIDs#1] 
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99) 
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1454) 
at org.hibernate.action.CollectionUpdateAction.execute(CollectionUpdateAction.java:86) 
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273) 
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265) 
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:187) 
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) 
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) 
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216) 
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383) 
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133) 
at com.myCompany.jobsrc.BasicDaoImpl.saveOrUpdate(BasicDaoImpl.java:38) 
at com.myCompany.jobBatch.ExecJobRoutine.generateExecJob(ExecJobRoutine.java:100) 
at com.com.myCompany.jobBatch.MarkerRoutine.execute(MarkerRoutine.java:33) 
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) 
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525) 
Caused by: java.sql.BatchUpdateException: No operations allowed after statement closed. 
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1269) 
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:955) 
at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeBatch(NewProxyPreparedStatement.java:1723) 
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70) 
at org.hibernate.jdbc.BatchingBatcher.addToBatch(BatchingBatcher.java:56) 
at org.hibernate.persister.collection.AbstractCollectionPersister.insertRows(AbstractCollectionPersister.java:1427) 
... 14 more 

我saveOrUpdate的方法:

public void saveOrUpdate(T t){ 
    Session session = HibernateUtil.getSession(); 
    Transaction transaction = session.beginTransaction(); 
    session.saveOrUpdate(t); 
    transaction.commit(); 
} 

這是我的hibernate.cfg.xml:

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> 
    <property name="hibernate.connection.password">eboxroot</property> 
    <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306 /jobtest?autoReconnect=true</property> 
    <property name="hibernate.connection.username">root</property> 
<property name="hibernate.hbm2ddl.auto">update</property> 
<property name="hibernate.cache.use_second_level_cache">false</property> 
    <property name="hibernate.cache.use_query_cache">false</property> 
<property name="hibernate.connection.autoReconnect">true</property> 
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property> 
<property name="c3p0.min_size">5</property> 
<property name="c3p0.max_size">30</property> 
<property name="c3p0.time_out">1800</property> 
<!--property name="c3p0.max_statement">50</property--> 
<property name="c3p0.acquire_increment">5</property> 
<property name="c3p0.idle_test_period">10</property> 
<property name="c3p0.preferredTestQuery">select 1;</property> 
<property name="c3p0.debugUnreturnedConnectionStackTraces">true</property> 
<property name="hibernate.connection.autoReconnectForPools">true</property> 
<!--property name="show_sql">true</property --> 

這是不會發生的時候,但有時候像。 任何人都可以給我一些提示嗎?

+0

如果您的代碼是線程化的,可能會解釋問題。 – 2012-02-28 00:06:40

回答

0

最後解決問題。這是因爲我將c3p0 max_statements設置爲50.它只緩存50個。現在我將它設置爲0,這意味着沒有緩存。現在我將它設置爲0,它完美無缺! 感謝您的幫助。

2

正如你可以看到在你的蹤跡,它說org.hibernate.exception.JDBCConnectionException在頂部。偶爾有可能你的數據庫連接可能成爲你的問題。如果您確定它沒有連接問題,那麼您必須啓用showSql標誌才能查看更詳細的跟蹤信息,以找出究竟是什麼原因。我希望有所幫助。

+0

謝謝你的回答。我很確定這不是連接問題。並不是所有的時候都很罕見。很長一段時間我用它來做同樣類型的物體,這很好。我將啓用showSql來查看。謝謝你的建議。祝你週末愉快! – 2012-02-25 01:20:58

0
java.sql.BatchUpdateException: No operations allowed after statement closed 

由於MySQL關閉連接一段時間後,您可能會遇到問題。請閱讀有關autoReconnecthere。您應該將autoReconnect=true添加到您的JDBC連接URI。

+0

謝謝!但我試圖添加autoReconnect = true,它仍然不起作用。 – 2012-02-28 01:16:29