ctx.close();
jndiServer.stop();
}
public void testPersistAndLoadUnderJta() throws Exception {
Item item;
SessionFactory sessionFactory = buildSessionFactory();
try {
UserTransaction ut = (UserTransaction) ctx.lookup("UserTransaction");
ut.begin();
try {
Session session = sessionFactory.openSession();
session.getTransaction().begin();
item = new Item("anItem", "An item owned by someone");
session.persist(item);
session.getTransaction().commit();
session.close();
} catch(Exception e) {
ut.setRollbackOnly();
throw e;
} finally {
if (ut.getStatus() == Status.STATUS_ACTIVE)
ut.commit();
else
ut.rollback();
}
ut = (UserTransaction) ctx.lookup("UserTransaction");
ut.begin();
try {
Session session = sessionFactory.openSession();
session.getTransaction().begin();
Item found = (Item) session.load(Item.class, item.getId());
Statistics stats = session.getSessionFactory().getStatistics();
log.info(stats.toString());
assertEquals(item.getDescription(), found.getDescription());
assertEquals(0, stats.getSecondLevelCacheMissCount());
assertEquals(1, stats.getSecondLevelCacheHitCount());
session.delete(found);
session.getTransaction().commit();
session.close();