在我的项目中,我有很多嵌套对象,几天后服务器在每次执行查询时变得非常慢。
我已经从object.save(flush:true)中删除了flush:true,这应该避免对象立即与数据库一起保存,因为我不需要对象id,我认为最好让hibernate决定何时刷新并提交。
但即使在此之后,我也会遇到与db有关的死锁问题,我收到了错误:
Timeout waiting for idle object. Stacktrace follows:
org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session for transaction; nested exception is org.hibernate.TransactionException: JDBC beg
in failed:
at SecurityFilters$_closure1_closure2_closure4.doCall(SecurityFilters.groovy:13)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: org.hibernate.TransactionException: JDBC begin failed:
... 4 more
Caused by: org.apache.commons.dbcp.SQLNestedException: Cannot get a connection, pool error Timeout waiting for idle object
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:114)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
... 4 more
Caused by: java.util.NoSuchElementException: Timeout waiting for idle object
at org.apache.commons.pool.impl.GenericObjectPool.borrowObject(GenericObjectPool.java:1167)
at org.apache.commons.dbcp.PoolingDataSource.getConnection(PoolingDataSource.java:106)
... 5 more
在对问题进行深入研究后,有人建议在执行几次后我应该手动刷新hibernate会话。
由于我已从保存调用中删除了对象刷新,我应该选择
session.flush()
session.clear()
清除hibernate会话,如果是,则这两个方面有什么区别。
答案 0 :(得分:1)
看起来您有连接泄漏,或者您需要调整最大打开连接数。这可以在properties
中的DataSource.groovy
块中配置。
但要回答关于冲洗的问题,没有区别。您无法刷新单个对象 - Hibernate会刷新所有缓存的更改。 save(flush:true)
只是调用save()
然后session.flush()