Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate - First query always loads longer

Hibernate newbie here.

From what i understand is that, first level cache is only available when a session is open. When session is closed, all cached entities in first level are evicted/removed. Is this correct?

I have a simple CRUD app developed in Java using the Hibernate framework. And every time my application starts, loads and executes its first query operation, the execution time is usually longer than the succeeding query operations. The first query usually takes 17ms to execute and succeeding are 1-2ms.

My question is this, is this really the behavior of Hibernate upon the start of the application? Is the data loaded from the first query operation stored in a cache somewhere? (Definitely not session cache because after executing my first query operation, session is closed right after) Does eager loading affect this behavior?

I really have no clue where to begin since the Hibernate documentation didn't cover this. Correct me if I am wrong.

I appreciate any help as I really don't know where to begin reading about this.

EDIT: For more info, here's the hibernate statistic of the first and second query operations:

First:

100222 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
23238430 nanoseconds spent preparing 3 JDBC statements;
8333256 nanoseconds spent executing 3 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
40215588 nanoseconds spent executing 1 flushes (flushing a total of 3 entities and 3 collections);
135213 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)

Second:

168597 nanoseconds spent acquiring 1 JDBC connections;
0 nanoseconds spent releasing 0 JDBC connections;
2332976 nanoseconds spent preparing 3 JDBC statements;
6427565 nanoseconds spent executing 3 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
1095389 nanoseconds spent executing 1 flushes (flushing a total of 3 entities and 3 collections);
17600 nanoseconds spent executing 1 partial-flushes (flushing a total of 0 entities and 0 collections)

Same query execution but different execution time length.

like image 989
Heisenberg Avatar asked Oct 31 '22 16:10

Heisenberg


1 Answers

My question is this, is this really the behavior of Hibernate upon the start of the application?

When you open a Word document for example, it will take much longer the first time, than when you close it and open it again. So, this is really not a Hibernate specific behavior.

Is the data loaded from the first query operation stored in a cache somewhere?

It is cached 'everywhere'. Disk has its cache levels. Operating systems cache things. Database will cache frequently/recently accessed data for sure. Even processors have their own caches.

But besides all of that, Java has its warm up time by its nature. When you access a class for the first time, it is loaded from disk, compiled by JIT, etc.

We are talking about 17ms here; it is a quite good warm up time considering all of the above.

like image 122
Dragan Bozanovic Avatar answered Nov 15 '22 10:11

Dragan Bozanovic