Level 2 cache not hit in @ManyToOne

#1

Hi!

I have 2 entities: User and Item, both @Cacheable. Item has @ManyToOne private User user. Level 2 JPA cache is activated and it works ok for find by id (tested by changing data in explorer and reading in my app).

Problem 1: find by id on Item it's good, it hits the cache, but not for its field "user". "user" is loaded each time from the DB (tested by changing data in explorer and reading in my app). "user" should also be taken from L2 cache, shouldn't it? Otherwise...it's a huge performance hit.

Problem 2: executing a query that returns User with id 5,6,7 (or any other) takes fresh data each time, even though user 5 has been previously loaded by find by id and its class is @Cacheable. Shouldn't just 6 and 7 be returned fresh (the first time, then loaded from L2 Cache each time)?

I didn't encounter P1 and 2 while using EclipseLink & MySQL.

Thanks in advance.

 

#2

The purpose of the L2 cache is to improve performance mainly by minimizing client server round trips. Therefore, retrieval by find always uses the L2 cache, since it can save a client-server round trip. Similarly, traversal between objects which requires transparent retrieval of referenced objects, always uses the L2 cache.

Queries work differently, because every query execution requires at least one client-server round trip (the query caches are on the server side). Therefore, the server can return all the results in that round trip, bypassing the L2 cache, usually without performance penalty.

You can change this behavior by setting the "objectdb.result-fetch" query hint to LAZY. By doing that you instruct ObjectDB to return object IDs rather than full objects in query results in the query execution round trip. However, in that case, if not all the results are cached, retrieving them later may require additional round trips.

Regarding problem 1, if Item is the owner of the relationship then the referenced User should be retrieved from the L2 cache. If User is the owner, then accessing the User of an Item requires query execution, and as explained above, there may no benefit in using the L2 cache in that case.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

I understand. Thank you.


Post Reply

To post a reply and/or subscribe to update notifications - please login