hibernate异常:无法插入集合行

时间:2012-02-24 23:28:14

标签: java database hibernate c3p0

我正在使用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  -->

这种情况并非一直发生,但有时也是如此。 有人可以给我一些提示吗?

3 个答案:

答案 0 :(得分:2)

正如您在跟踪中所看到的,它在顶部显示org.hibernate.exception.JDBCConnectionException。偶尔,您的数据库连接可能会成为您的问题。如果您确定它不是连接问题,那么您必须启用showSql标志以查看更详细的跟踪以找出究竟是什么原因。我希望有所帮助。

答案 1 :(得分:0)

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

由于MySQL在一段时间后关闭了连接,你可能会遇到这个问题。请阅读autoReconnect here。您应该将autoReconnect=true添加到JDBC连接URI。

答案 2 :(得分:0)

终于解决了问题。这是因为我将c3p0 max_statements设置为50.它只缓存50.现在我将其设置为0,这意味着没有缓存。现在我把它设置为0,它完美无缺! 谢谢你的帮助。