简体   繁体   English

Hibernate 应用程序中的连接泄漏

[英]Connection Leak in Hibernate application

I am working in java enterprise application.我在 Java 企业应用程序中工作。 The application has hibernate framework for backend.该应用程序具有后端的休眠框架。 Due to recent changes in application some code consumes all JDBC connection pools from weblogic server.由于最近应用程序的变化,一些代码消耗了来自 weblogic 服务器的所有 JDBC 连接池。

The application connection was property handled in code, for each thread we are create each session using threadlocal class.应用程序连接是在代码中处理的属性,对于每个线程,我们使用 threadlocal 类创建每个会话。 So there was no issues with creating connection.所以创建连接没有问题。 The application was live more than 5 years.该应用程序已使用超过 5 年。

We are suspecting the recent code changes causes this major issue.我们怀疑最近的代码更改导致了这个主要问题。 Finally we decided to use profiler tool for investigate this issue.最后我们决定使用分析器工具来调查这个问题。

Before that I am going to review the recent code changes, so what are the key points i need to keep in mind in hibernate while reviewing?在此之前,我将回顾最近的代码更改,那么我在回顾时需要记住的 hibernate 关键点是什么?

This is very critical/serious situation.这是非常危急/严重的情况。 So suggest me some tips to solve this..所以建议我一些提示来解决这个问题..

Thanks谢谢

Query your database:查询您的数据库:

select * from pg_stat_activity;

And check which queries are long lasting with idle in transaction status.并检查哪些查询idle in transaction状态idle in transaction是持久的。 Try to locate them in your code and research why transaction is not completed.尝试在您的代码中找到它们并研究交易未完成的原因。


Add to persistence.xml :添加到persistence.xml

  <property name="hibernate.c3p0.unreturnedConnectionTimeout" value="60"/>
  <property name="hibernate.c3p0.debugUnreturnedConnectionStackTraces" value="true"/>

unreturnedConnectionTimeout value should be greater than 0 because default is 0 - unlimited. unreturnedConnectionTimeout值应大于 0,因为默认值为 0 - 无限制。 These properties are more testing/debugging purposes and ideally shouldn't be used in a production environment.这些属性更多用于测试/调试目的,理想情况下不应在生产环境中使用。


Few things to check in a code/configuration:检查代码/配置的几件事:

  • Commit transactions explicitly or use @Transactional .显式提交事务或使用@Transactional Please note that @Transactional works only for public methods.请注意,@ @Transactional仅适用于公共方法。

  • If you're using Hibernate 5.1.0.Final, then persistence.xml should contain:如果您使用的是 Hibernate 5.1.0.Final,那么 persistence.xml 应该包含:

<property name="hibernate.connection.provider_class" value="org.hibernate.c3p0.internal.C3P0ConnectionProvider" />

Instead of:而不是:

<property name="connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider" />

  • If you're using如果您正在使用

<property name="hibernate.enable_lazy_load_no_trans" value="true" />

it may cause connection leaks when lazy loading.延迟加载时可能会导致连接泄漏。 Related discussions:相关讨论:


Check related articles:查看相关文章:

Typically this is due to sessions that were not closed.通常这是由于会话未关闭所致。 Simple way that I personally use is to create a table (map) of running sessions.我个人使用的简单方法是创建一个正在运行的会话的表(映射)。 On create add an entry in a table, and on session close remove (or mark) entry on map.在创建时在表中添加一个条目,并在会话关闭时删除(或标记)地图上的条目。 This way you can identify which sessions were not closed.通过这种方式,您可以确定哪些会话未关闭。

Good logger (slf4/log4j) may also be useful, especially NDC ( http://wiki.apache.org/logging-log4j/NDCvsMDC ).好的记录器 (slf4/log4j) 也可能有用,尤其是 NDC ( http://wiki.apache.org/logging-log4j/NDCvsMDC )。

I had to investigate issues with hibernate myself in the past and I found this page from the official documentation gives a lot of good advices already:过去我不得不自己调查 hibernate 的问题,我发现官方文档中的这个页面已经提供了很多很好的建议:

http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html

请检查 session.createQuery() 中使用的所有实体名称,如果使用,可能是查询中使用的类名称不匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM