Skip to content

Instantly share code, notes, and snippets.

@abhirockzz
Created May 22, 2016 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhirockzz/b0a03bddf11162a80f4ecc3cef711f2f to your computer and use it in GitHub Desktop.
Save abhirockzz/b0a03bddf11162a80f4ecc3cef711f2f to your computer and use it in GitHub Desktop.
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