Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Interrogating the JPA L2 cache
@Stateless
public class JPACacheOps{
@persistenceContext("emp_PU")
EntityManager em;
public void testL2Cache(String id){
String id = "007";
String anotherID = "42";
em.find(Employee.class, id);
em.find(Employee.class, id);
//both '007' and '42' are in L2 cache
String orgID = "org123";
em.find(Org.class, orgID); //'org123' is now in L2 cache
Cache sharedL2Cache = em.getEntityManagerFactory().getCache();
sharedL2Cache.contains(Employee.class , id); //returns true
sharedL2Cache.evict(Employee.class, id); //'007' removed from shared cache
sharedL2Cache.contains(Employee.class, id); //returns false
sharedL2Cache.evict(Employee.class); //ALL 'Employee' entities removed
sharedL2Cache.contains(Employee.class, anotherID); //returns false
sharedL2Cache.contains(Org.class , orgID); //returns true
sharedL2Cache.evictAll(); //ALL entities removed
sharedL2Cache.contains(Ord.class, orgID); //returns false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment