Connection Leak in Hibernate application

十年热恋 提交于 2021-01-01 17:38:49

问题


I am working in java enterprise application. The application has hibernate framework for backend. Due to recent changes in application some code consumes all JDBC connection pools from weblogic server.

The application connection was property handled in code, for each thread we are create each session using threadlocal class. So there was no issues with creating connection. The application was live more than 5 years.

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?

This is very critical/serious situation. So suggest me some tips to solve this..

Thanks


回答1:


Query your database:

select * from pg_stat_activity;

And check which queries are long lasting with idle in transaction status. Try to locate them in your code and research why transaction is not completed.


Add to 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. 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. Please note that @Transactional works only for public methods.

  • If you're using Hibernate 5.1.0.Final, then persistence.xml should contain:

<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:

  • org.hibernate.LazyInitializationException - could not initialize proxy - no Session
  • Solve Hibernate Lazy-Init issue with hibernate.enable_lazy_load_no_trans

Check related articles:

  • The best way to detect database connection leaks
  • How we fixed all database connection leaks



回答2:


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).




回答3:


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:

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




回答4:


Please check for all entities name used in session.createQuery(), if it is used, may be the class name is not matched which is used in query.



来源:https://stackoverflow.com/questions/19407327/connection-leak-in-hibernate-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!