Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to Use EntityManager.clear()?

Tags:

orm

hibernate

A custom JPA mapper class has a method:

removeUser()  1. execute 'DELETE' HQL query to remove user 2. call getEntityManager().flush(); 3. call getEntityManager().clear(); 

If I understand clear() correctly, it will remove from context all persistent entities. -source

However, I also read here,

you should define clear architecture- and design guidelines about where a  clear() can be called.  

What are clear guidelines on when to call clear()?

like image 977
Kevin Meredith Avatar asked Dec 14 '12 21:12

Kevin Meredith


People also ask

What will happen when the clear () of an EntityManager class is invoked?

clear. Clear the persistence context, causing all managed entities to become detached. Changes made to entities that have not been flushed to the database will not be persisted.

What is difference between EntityManagerFactory and SessionFactory?

Using EntityManagerFactory approach allows us to use callback method annotations like @PrePersist, @PostPersist,@PreUpdate with no extra configuration. Using similar callbacks while using SessionFactory will require extra efforts.

How do I delete EntityManager?

To delete a record from database, EntityManager interface provides remove() method. The remove() method uses primary key to delete the particular record.

What are the methods of EntityManager?

JPA EntityManager is used to access a database in a particular application. It is used to manage persistent entity instances, to find entities by their primary key identity, and to query over all entities.


2 Answers

The articles explains it. Clearing the entity manager empties its associated cache, forcing new database queries to be executed later in the transaction. It's almost never necessary to clear the entity manager when using a transaction-bound entity manager. I see two reasons to clear:

  • when doing batch processing, in order to avoid having a giant cache eating memory and increasing the time to flush because of long dirty checks
  • when you're doing DML or SQL queries, which completely bypass the entity manager cache (as in your example). In this case, the state held by the cache doesn't reflect what is in the database because of the queries, so you want to clear the cache to avoid this inconsistency.
like image 142
JB Nizet Avatar answered Oct 25 '22 06:10

JB Nizet


Yeas, it's exactly depends on the architecture style of the platform as documentation points.

  • For example if in your application EM is thread's associated, then one of the solutions for transaction management is to implement Session-Per-Request pattern which starts a transaction on each start of user's request and commit & clear the cache in each end of the request in order to prevent dirty reads. This is just a simple example
  • Other example is in a SOA platform. For each service could be also open a transaction in the beginning and commit it at the end with clearing (in case same EM is used by other service and avoiding of dirty reads is required)
  • I will replay the previous two common cases - batch processing one and bypassing in all possible cases the EM. So in this case queries will be forced to query from the DB , not from the cache.
  • You should know that you work with L1 and L2 caches and L1 is the cache cleared by EM. When you reads huge set of data (should be not the case)( then also clearing is required in a period.

As you see depends on the cases, architecture and style for your platform. Directly for your method - it's not a good practice to flush and clear cache per method, it's a anti pattern method.

like image 34
Simeon Angelov Avatar answered Oct 25 '22 05:10

Simeon Angelov