0

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

Peter Jerald
  • 613
  • 2
  • 7
  • 22
  • You could use a connection pool like [HikariCP](http://brettwooldridge.github.io/HikariCP/) and turn on leak detection, that should pinpoint where in the code the connection is being allocated and never returned. – brettw Nov 01 '13 at 14:37

4 Answers4

4

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:


Check related articles:

Vlad Mihalcea
  • 103,297
  • 39
  • 432
  • 788
Justinas Jakavonis
  • 6,374
  • 5
  • 52
  • 88
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).

Rafi
  • 109
  • 4
-1

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

phury
  • 1,819
  • 1
  • 18
  • 32
  • 1
    The page you've linked to doesn't contain the word "leak", what parts are you referring to? – Amir T May 31 '19 at 13:55
-1

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.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388