Role of hibernate queries in heap dump
Asked Answered
C

2

8

I'm JProfiling my application to analyze high CPU usage. CPU usage is 100% (at server) at the time of user login. So started profiling my application.

The below query strings I found in the heap dumps. Not only these 4 queries, there are hundreds of queries like this in the dump.

java.lang.String (0x3262b1) ["/* load com.v4common.shared.beans.transaction.ControlTransaction */ select controltra0_.id as id47_48_, controltra0_.form_transaction_id as form2_47_48_, controltra0_.string_value as string3_47_48_, c"]     129 kB (0 %)
java.lang.String (0x310b2f) ["/* load com.v4common.shared.beans.transaction.ReportTransaction */ select reporttran0_.id as id158_45_, reporttran0_.report_id as report2_158_45_, reporttran0_.norm_id as norm3_158_45_, reporttran0_.d"]     124 kB (0 %)
java.lang.String (0x312222) ["/* load com.v4common.shared.beans.transaction.ReportItemTransaction */ select reportitem0_.id as id160_41_, reportitem0_.report_structure as report2_160_41_, reportitem0_.grid_row_criteria as grid3_16"]     110 kB (0 %)
java.lang.String (0x30c104) ["/* load com.v4common.shared.beans.Reports.EsenderCSReport */ select esendercsr0_.id as id117_36_, esendercsr0_.name as name117_36_, esendercsr0_.report_type as report3_117_36_, esendercsr0_.is_show_pr"]     94,248 bytes (0 %)
java.lang.String (0x30d1dc) ["/* load com.v4common.shared.beans.Reports.ReportStructure */ select reportstru0_.id as id120_35_, reportstru0_.name as name120_35_, reportstru0_.xml as xml120_35_, reportstru0_.esender_format as esend"]     90,736 bytes (0 %)

I'm just logged in the system and I'm not touching the beans at all, still I'm able to see them in the dumps.

Any ideas why those strings are there in the dump?

Or what does that line mean even?

Coven answered 7/3, 2014 at 15:34 Comment(11)
AFAIK this is caused by hibernate query cache, but don't know how to make Hibernate not storing these queries.Methanol
@LuiggiMendoza Thank you. But I haven't fired these queries not even once yet. Still those are cached ?? or at the time of application start up hibernate do this ??? Can you please tell me this ?? I'm not aware of that. And can we disable that query cache through some configuration ??Coven
Are you sure you don't load initial data when the application is deployed?Methanol
@LuiggiMendoza I thought the same and cross checked the queries firing on loading data after login by enabling mysql=true. I'm not at all touching the above beans (database tables). Still I'm able to see these line in heap dump.Coven
1) In my view, as soon as CPU high, I start with threads than heap. Based on my experience and understanding, if one high other stay low (there are some rare exceptions too). 2) Check your XML/constructors to make sure no queries are being executed at initialization time.Birmingham
If there's no other process that fires these queries, then maybe Hibernate is firing them on purpose? Yes, it is very odd and we have a similar problem here as well. We took a heap dump and analyze it with MAT and found our memory leak was due to this Hibernate feature, but still are unable to fix it.Methanol
Unless we explicitly set, hibernate doesn't cache queries. I strongly suspect, there were not GCed from previous calls.Birmingham
@Nambari I already tried disabling this feature but didn't work either...Methanol
I have a suspicion that these strings are unrelated and are actually not used for queries (yet), just stored in the memory. Can you enable some kind of query log to confirm if the queries are really fired? Something like this, or then on the database side? If the queries are not yet fired, it would likely mean they're not causing your 100% CPU usage.Mosque
First I worried about CPU usage. But later I surprised about why these queries are there. Though I'm worrying about CPU usage these unused string's eat my memory right ??Coven
these queries cant be responsible for the high CPU usage, it must be something else. worst case they would consume a lot of memory but not CPU, but memory consumption should not be an issue either. Try to use visualvm that comes installed with the JVM to profile the CPU usage , it's a very user friendly tool visualvm.java.netHypocrite
H
5

This is normal, these are the Hibernate pre-prepared queries that are prepared at server startup time.

Take for example the ControlTransaction class. Hibernate already knows that will probably need queries to select entities by ID, delete them, etc.

So it generates beforehand a series of SQL prepared statements to do these operations. The comments at the beginning of each query indicate why they where generated.

For example this query was generated to load a ControlTransaction by Id:

/* load com.v4common.shared.beans.transaction.ControlTransaction */ 
select controltra0_.id as id47_48_, controltra0_.form_transaction_id as form2_47_48_, controltra0_.string_value as string3_47_48_, c 

Queries that start with comments of one-to-many or one-to-one, are used for lazy loading, etc. Named queries in JPQL/HQL are also compiled into a SQL query at server startup and the comment identifies which named query originated the SQL query.

Each entity will give rise to several of these queries, depending on the mapping annotations used.

So it's actually normal that these queries are there in the heap at user first login time.

Hypocrite answered 11/3, 2014 at 18:5 Comment(0)
H
0

Do you have these queries as @NamedQueries (or @NamedQuery) on any of your Entities?

Hibernate could be loading the Named Queries into it's cache at server startup time. They are certainly parsed at startup to check syntax, etc.

Hitchhike answered 17/3, 2014 at 21:48 Comment(1)
No. I don't have any queries. I'm using criteria API.Coven

© 2022 - 2024 — McMap. All rights reserved.