Created
May 22, 2016 14:09
-
-
Save abhirockzz/b0a03bddf11162a80f4ecc3cef711f2f to your computer and use it in GitHub Desktop.
Interrogating the JPA L2 cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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