use of org.jpwh.model.cache.Item in project microservices by pwillhan.
the class SecondLevel method cacheBehavior.
@Test
public void cacheBehavior() throws Exception {
CacheTestData testData = storeTestData();
Long USER_ID = testData.users.getFirstId();
Long ITEM_ID = testData.items.getFirstId();
UserTransaction tx = TM.getUserTransaction();
try {
{
tx.begin();
EntityManager em = JPA.createEntityManager();
// Get the statistics API
Statistics stats = JPA.getEntityManagerFactory().unwrap(SessionFactory.class).getStatistics();
SecondLevelCacheStatistics itemCacheStats = stats.getSecondLevelCacheStatistics(Item.class.getName());
assertEquals(itemCacheStats.getElementCountInMemory(), 3);
assertEquals(itemCacheStats.getHitCount(), 0);
// Hit the second-level cache with entity lookup by identifier
Item item = em.find(Item.class, ITEM_ID);
assertEquals(itemCacheStats.getHitCount(), 1);
// Initializing a proxy will also hit the second-level cache
SecondLevelCacheStatistics userCacheStats = stats.getSecondLevelCacheStatistics(User.class.getName());
assertEquals(userCacheStats.getElementCountInMemory(), 3);
assertEquals(userCacheStats.getHitCount(), 0);
User seller = item.getSeller();
// Initialize proxy
assertEquals(seller.getUsername(), "johndoe");
assertEquals(userCacheStats.getHitCount(), 1);
// Get the Item#bids collection and its referenced Bid entity instances
/*
The statistics tell you that there are three <code>Item#bids</code>
collections in the cache (one for each <code>Item</code>). No
successful cache lookups have occurred so far.
*/
SecondLevelCacheStatistics bidsCacheStats = stats.getSecondLevelCacheStatistics(Item.class.getName() + ".bids");
assertEquals(bidsCacheStats.getElementCountInMemory(), 3);
assertEquals(bidsCacheStats.getHitCount(), 0);
/*
The entity cache of <code>Bid</code> has five records, and you
haven't accessed it either.
*/
SecondLevelCacheStatistics bidCacheStats = stats.getSecondLevelCacheStatistics(Bid.class.getName());
assertEquals(bidCacheStats.getElementCountInMemory(), 5);
assertEquals(bidCacheStats.getHitCount(), 0);
/*
Initializing the collection will read the data from both caches.
*/
Set<Bid> bids = item.getBids();
assertEquals(bids.size(), 3);
/*
The cache found one collection, as well as the data for
its three <code>Bid</code> elements.
*/
assertEquals(bidsCacheStats.getHitCount(), 1);
assertEquals(bidCacheStats.getHitCount(), 3);
tx.commit();
em.close();
}
} finally {
TM.rollback();
}
}
use of org.jpwh.model.cache.Item in project microservices by pwillhan.
the class SecondLevel method storeTestData.
public CacheTestData storeTestData() throws Exception {
UserTransaction tx = TM.getUserTransaction();
tx.begin();
EntityManager em = JPA.createEntityManager();
Long[] itemIds = new Long[3];
Long[] userIds = new Long[3];
User johndoe = new User("johndoe");
em.persist(johndoe);
userIds[0] = johndoe.getId();
User janeroe = new User("janeroe");
em.persist(janeroe);
userIds[1] = janeroe.getId();
User robertdoe = new User("robertdoe");
em.persist(robertdoe);
userIds[2] = robertdoe.getId();
Item item = new Item("Item One", CalendarUtil.TOMORROW.getTime(), johndoe);
em.persist(item);
itemIds[0] = item.getId();
for (int i = 1; i <= 3; i++) {
Bid bid = new Bid(item, robertdoe, new BigDecimal(9 + i));
item.getBids().add(bid);
em.persist(bid);
}
item = new Item("Item Two", CalendarUtil.TOMORROW.getTime(), johndoe);
em.persist(item);
itemIds[1] = item.getId();
for (int i = 1; i <= 1; i++) {
Bid bid = new Bid(item, janeroe, new BigDecimal(2 + i));
item.getBids().add(bid);
em.persist(bid);
}
item = new Item("Item_Three", CalendarUtil.AFTER_TOMORROW.getTime(), janeroe);
em.persist(item);
itemIds[2] = item.getId();
for (int i = 1; i <= 1; i++) {
Bid bid = new Bid(item, johndoe, new BigDecimal(3 + i));
item.getBids().add(bid);
em.persist(bid);
}
tx.commit();
em.close();
// Warm up the cache properly
tx.begin();
em = JPA.createEntityManager();
// We load all the User instances into the second-level cache
// here because NONSTRICT_READ_WRITE strategy doesn't insert
// them into the second-level cache when persist() is called,
// only when they are loaded from the database. And we need
// to do this in a new transaction, or TwoPhaseLoad doesn't
// put it in the cache.
em.createQuery("select u from User u").getResultList();
// collection is loaded.
for (Long itemId : itemIds) {
em.find(Item.class, itemId).getBids().size();
}
tx.commit();
em.close();
JPA.getEntityManagerFactory().unwrap(SessionFactory.class).getStatistics().clear();
CacheTestData testData = new CacheTestData();
testData.items = new TestData(itemIds);
testData.users = new TestData(userIds);
return testData;
}
use of org.jpwh.model.cache.Item in project microservices by pwillhan.
the class SecondLevel method cacheModes.
@Test
public void cacheModes() throws Exception {
CacheTestData testData = storeTestData();
Long USER_ID = testData.users.getFirstId();
Long ITEM_ID = testData.items.getFirstId();
UserTransaction tx = TM.getUserTransaction();
try {
{
tx.begin();
EntityManager em = JPA.createEntityManager();
Statistics stats = JPA.getEntityManagerFactory().unwrap(SessionFactory.class).getStatistics();
SecondLevelCacheStatistics itemCacheStats = stats.getSecondLevelCacheStatistics(Item.class.getName());
// Bypass the cache when retrieving an entity instance by identifier
{
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
// Hit the database
Item item = em.find(Item.class, ITEM_ID, properties);
assertEquals(itemCacheStats.getHitCount(), 0);
}
// Bypass the cache when storing an entity instance
assertEquals(itemCacheStats.getElementCountInMemory(), 3);
em.setProperty("javax.persistence.cache.storeMode", CacheStoreMode.BYPASS);
Item item = new Item(// ...
"Some Item", CalendarUtil.TOMORROW.getTime(), em.find(User.class, USER_ID));
// Not stored in the cache
em.persist(item);
em.flush();
// Unchanged
assertEquals(itemCacheStats.getElementCountInMemory(), 3);
tx.commit();
em.close();
}
} finally {
TM.rollback();
}
}
Aggregations