c3p0 说 - 启动休眠事务时“java.lang.Exception: DEBUG ONLY: Overdue resource check-out stack trace”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19337638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c3p0 says - "java.lang.Exception: DEBUG ONLY: Overdue resource check-out stack trace" on starting a hibernate transaction
提问by coding_idiot
Recently, my tomcat started hanging up. The requests were never replied. I figured out that it was due to connections never being returned to the connection pool.
最近,我的tomcat开始挂了。这些请求从未得到答复。我发现这是由于连接永远不会返回到连接池。
I have used c3p0 with hibernate and the database is mysql 5.5
我在 hibernate 中使用了 c3p0,数据库是 mysql 5.5
In order to debug the connection leaks, I added the following properties in my hibernate.cfg.xml
为了调试连接泄漏,我在我的 hibernate.cfg.xml
<property name="hibernate.c3p0.unreturnedConnectionTimeout">30</property>
<property name="hibernate.c3p0.debugUnreturnedConnectionStackTraces">true</property>
After adding them, in the logs it says :
添加它们后,在日志中它说:
[2013-10-12 23:40:22.487] [ INFO] BasicResourcePool.removeResource:1392 - A checked-out resource is overdue, and will be destroyed: com.mchange.v2.c3p0.impl.NewPooledConnection@1f0c0dd
[2013-10-12 23:40:22.487] [ INFO] BasicResourcePool.removeResource:1395 - Logging the stack trace by which the overdue resource was checked-out.
java.lang.Exception: DEBUG ONLY: Overdue resource check-out stack trace.
Pointing to at dao.DAOBasicInfo.getBean(DAOBasicInfo.java:69)
指向 at dao.DAOBasicInfo.getBean(DAOBasicInfo.java:69)
public static Basicinfo getBean(Integer iduser) {
Basicinfo u = null;
Session sess = NewHibernateUtil.getSessionFactory().openSession();
try {
Transaction tx = sess.beginTransaction(); //line 69
Query q = sess.createQuery("from Basicinfo where iduser=" + iduser);
u = (Basicinfo) q.uniqueResult();
if (u == null) {
u = new Basicinfo();
u.setIduser(iduser);
}
tx.commit();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
sess.close();
}
return u;
}
I cross checked and Mysql says it supports transactions with InnoDB
我交叉检查,Mysql 说它支持与 InnoDB 的事务
Because of the above error, I'm having un-returned connections and then they pile up making the app unresponsive.
由于上述错误,我有未返回的连接,然后它们堆积在一起,使应用程序无响应。
Please let me know what's wrong in starting a transaction and even I'm using finally and there's no exception thrown.
请让我知道开始交易时出了什么问题,即使我正在使用 finally 并且没有抛出异常。
采纳答案by software.wikipedia
Some suggestions to debug it
调试的一些建议
As Steve mentioned in comments. Try to see what happens when you remove the unreturnedConnectionTimeout option.
May be your queries are taking too long. Try to log some performance stats on your code and see how much time your query is taking. May be you need to tune your query. and for short term you can also increase the unreturnedConnectionTimeout to be more than the response time on your queries.
Also try transaction timeout option in hibernate. May be set tx.setTimeout(20) and play with the timeout numbers and see if some queries timeout.
You may also want to use some profiling tool. Try VisualVMin case your Java version is supported on it. Otherwise (if on linux or mac) you may want to try Java Debugging commandson older version of java. Some of those commands are also available from JDK.
正如史蒂夫在评论中提到的那样。尝试查看删除 unreturnedConnectionTimeout 选项时会发生什么。
可能是您的查询花费的时间太长。尝试在您的代码上记录一些性能统计信息,并查看您的查询花费了多少时间。可能是您需要调整您的查询。并且在短期内,您还可以将 unreturnedConnectionTimeout 增加到超过查询的响应时间。
还可以在休眠中尝试事务超时选项。可以设置 tx.setTimeout(20) 并使用超时数字并查看某些查询是否超时。
您可能还想使用一些分析工具。如果您的 Java 版本受支持,请尝试使用VisualVM。否则(如果在 linux 或 mac 上)您可能想在旧版本的 java 上尝试Java 调试命令。其中一些命令也可从 JDK 获得。
Small improvements on the code
代码的小改进
Not sure if it will really fix your issue however you may want to add rollback for transaction in exception block. Added another try catch for tx.close to avoid another exception.
Also added a null check for session close. You may already know that one condition when finally may not completely execute - if another exception is thrown in finally block. Currently it may not be applicable in your code however in case you add more than one line in finally block make sure any exceptions are covered so next line can execute.
One more suggestion is to reduce the scope of transaction itself. Looking at the code it seems you may need the transaction only in case a uid is not found. How about limiting the transaction code inside if(u==null) block. Not sure if helps but you need not have transaction for read.
不确定它是否真的能解决您的问题,但是您可能希望在异常块中为事务添加回滚。为 tx.close 添加了另一个 try catch 以避免另一个异常。
还为会话关闭添加了空检查。您可能已经知道 finally 时的一个条件可能不会完全执行 - 如果在 finally 块中抛出另一个异常。目前它可能不适用于您的代码,但是如果您在 finally 块中添加多于一行,请确保覆盖任何异常,以便下一行可以执行。
另一种建议是缩小交易范围本身。查看代码似乎只有在未找到 uid 的情况下才需要事务。如何限制 if(u==null) 块内的事务代码。不确定是否有帮助,但您不需要读取事务。
Below is my sample code
下面是我的示例代码
public static Basicinfo getBean(Integer iduser) {
Basicinfo u = null;
Transaction tx = null;
Session sess = NewHibernateUtil.getSessionFactory().openSession();
try {
Query q = sess.createQuery("from Basicinfo where iduser=" + iduser);
u = (Basicinfo) q.uniqueResult();
if (u == null) {
tx = sess.beginTransaction(); //line 69
u = new Basicinfo();
u.setIduser(iduser);
tx.commit();
}
} catch (Exception ex) {
ex.printStackTrace();
if(tx != null) {
try {
tx.rollback();
} catch(Exception e){e.printStackTrace;}
}
} finally {
if(sess!=null) {
sess.close();
}
}
return u;
}
回答by gndps
One of the reasons this error comes up is when you don't make sure to
出现此错误的原因之一是您不确定
transaction.commit();
交易。提交();
not an answer to this question, but someone who forgot to commit will also land on this page after googling the error
不是这个问题的答案,但忘记提交的人在谷歌搜索错误后也会登陆此页面