Hibernate - first request always takes longer

Hibernate is new here.

As I understand it, the first level cache is only available if the session is open. When the session is closed, all cached objects in the first level are unloaded / deleted. Is it correct?

I have a simple CRUD application developed in Java using the Hibernate platform. And each time my application launches, downloads, and executes its first request operation, the execution time is usually longer than subsequent request operations. The first request usually takes 17 ms to complete, and the next 1-2 ms.

My question is, is this Hibernate behavior at the beginning of the application? Are data loaded from the first query operation stored in the cache? (Definitely not a session cache, because after completing my first request operation, the session closes right after that) Does the load affect the load?

I really don't know where to start, as the Hibernate documentation did not cover this. Correct me if I am wrong.

I appreciate any help since I really don't know where to start reading about it.

EDIT: For more information, see sleep statistics for the first and second query operations:

Firstly:

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) 

Secondly:

 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) 

The execution of the same request, but the different duration of the execution time.

+5
source share
1 answer

My question is, is this Hibernate behavior at the beginning of the application?

When you open a Word document, for example, it will take a lot longer than when you close it and open it again. So this is really not a specific hibernation behavior.

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

It is cached "everywhere." The drive has cache levels. Operating systems cache things. The database will cache frequently / recently retrieved data accurately. Even processors have their own caches.

But besides all this, Java has its own warm-up time in nature. When you first access a class, it loads from disk, compiled JIT, etc.

We are talking about 17ms here; This is a pretty good warm-up time, considering all of the above.

+1
source

Source: https://habr.com/ru/post/1235001/


All Articles