Understanding the first level JPA cache

I can bet that every Java developer at least heard about L1 (aka EntityManager or Session) cache. But is your level of understanding it good enough? If you're not sure, consider going through this post.

At first, we need to know what the persistence context is. Following EntityManager JavaDoc we know, that:
"A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle are managed." 
In fact, the first level cache is the same as the persistence context. That means operations such as persist(), merge(), remove() are changing only internal collections in the context and are not synchronized to the underlying database. What is the mosts important here is what happens when you invoke the clear() method. It clears the L1 cache. But we know L1 == persistence context. Does it mean clearing L1 removes all entities? In fact yes - all entities will be dropped and never synchronized to the database. That's not a secret, it states in the documentation - "Unflushed changes made to the entity (...) will not be synchronized to the database." But who cares about the docs? :)

So how does it look in practice? Take a look at the code below:

em.persist(myEntity); // saves entity to the context
em.flush(); // triggers insert into database
em.clear(); // removes entity from the context == entity is no longer managed

If you omit flush() the entity won't hit the database. It will live only in your code, and after leaving method which created this object will be lost. Let's take a look at the next sample:

myEntity.setName("old name");
myEntity.setName("new name");

What will be the value of the name property after finishing this code? Of course still "old name", because in the moment when setName() has been invoked the entity is no longer managed and it has to be merged with the persistent context (by invoking em.merge(myEntity) method) to be the subject of the dirty checking.

But I'm not calling flush() method in my code and everything works!? But do you call clear()? That's what I thought. What is the default flush behavior? JPA by default flushes changes on commit as well as every query execution (FlushModeType.AUTO). If you change it to COMMIT (with em.setFlushMode(FlushModeType.COMMIT) method) then flush will occur (as name suggests) only on commit.

Deep understanding of the L1 behavior is especially important when dealing with batch operations. Why? If you want such operation to be effective, we must manually flush changes from time to time (let's say every 100 operations). Do you know, that flush() doesn't clear the persistence context? So what? Flush is not cheap because it must process all entities in the context to see if there is anything to synchronize with the database. If you won't clear the context manually immediately after flush(), every next operation will take longer and longer. To be honest this time grows exponentially, which seems to be sufficient reason to remember about mentioned technique.

If you're interested in deeper dive into persistence context please feel free to clone and play with this repository, which contains all described cases and examples.


Unknown said…
Thanks a lot! This will be useful for every yii2 developers, as well as for people, who are new to Yii2 Development. It is well known yii2 methods are very helpful in project management and software development. It has everything to improve software development process.
Naveen S said…
Thanks for the useful information of software testing courses, give more updates on software testing development, First time I visit your blog really nice, I bookmark your blog here after a daily visit. Learn software testing course in Chennai
Unknown said…
Your good knowledge and kindness in playing with all the pieces were
very useful. I don’t know what I would have done if I had not
encountered such a step like this.

Digital Marketing Course In Chennai
Digital Marketing Training In Chennai
Seo Training in Chennai
Roger Binny said…
The blog or and best that is extremely useful to keep I can share the ideas of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.

Digital Marketing Course in Chennai
Digital Marketing Training in Chennai
Online Digital Marketing Training
SEO Training in Chennai
Greetings! Very helpful advice within this article! It’s the little changes that produce the biggest changes. Thanks for sharing!
Abinitio Online Training Instittue
Microsoft Azure Online Training Instittue
App V Online Training Instittue
Unknown said…
This comment has been removed by the author.
w3webschool said…
Nice post By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision.
Digital Marketing Training
Thanks for sharing the steps to write an e-commerce blog. I think this is very useful especially for beginners.
All those who are into e-commerce and wants to know more about it and re-commerce.
Mainframe Training in Hyderabad
Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.
Thanks & Regards,
VRIT Professionals,
No.1 Leading Web Designing Training Institute In Chennai.

And also those who are looking for
Web Designing Training Institute in Chennai
SEO Training Institute in Chennai
Photoshop Training Institute in Chennai
PHP & Mysql Training Institute in Chennai
Android Training Institute in Chennai
priya sharma said…
Such A nice post... thanks For Sharing !!Great information for new guy like Happy New year 2020
pavithra dass said…
Good job! Fruitful article. I like this very much. It is very useful for my research. It shows your interest in this topic very well. I hope you will post some more information about the software. Please keep sharing!!
SEO Training in Chennai
SEO Training in Bangalore
SEO Training in Coimbatore
SEO Training in Madurai
SEO Course in Chennai
SEO Course in Chennai
SEO Course in Bangalore
SEO Course in Coimbatore
Sinelogix said…
NICE BLOG!!! Your blog is very informative for us. I would really like to come back again right here for likewise good articles or blog posts.

online store developer in USA |Web Developer in USA | E-Store Developer in USA | Website Designer in California |Website Developer in California |Portal Developer in California |
Sinelogix said…
NICE BLOG!!! Your blog is very informative for us. I would really like to come back again right here for likewise good articles or blog posts.

online store developer in USA |Web Developer in USA | E-Store Developer in USA | Website Designer in California |Website Developer in California |Portal Developer in California |
jony blaze said…
Thanks for sharing nice information....
AI Training in Hyderabad

Popular posts from this blog

Understanding Spring Web Initialization

Including Java agent in standalone Spring Boot application