Tuesday, February 25, 2014

How to programmatically clear the JPA cache

When I used my JPA with EclipseLink as the implementation together with JBoss AS7.1.1.
I'm facing some issues.

One of the issue is the JPA cache not refresh by itself. 

Scenario:
When a new entity inserted into DB with JPA (EclipseLink).
and the new record is verified already inserted into database.

Then immediately I made a SELECT ALL query to this entity.
=> SELECT a FROM ENTITY a

But unfortunately, the newly inserted record is not appeared in the search result. 

to resolve this issue, I used the following line code the clear the JPA cached entities.


// the clear the entire JPA cache
entityManager.getEntityManagerFactory().getCache().evictAll();


// clear only selected entity from the JPA cache
entityManager.getEntityManagerFactory().getCache().evict(ENTITY.class);



Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...