Author: galder.zamarreno(a)jboss.com
Date: 2010-02-23 07:09:45 -0500 (Tue, 23 Feb 2010)
New Revision: 18856
Modified:
core/trunk/cache-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/JBossStandaloneJtaExampleTest.java
Log:
[HHH-4640] (Add test with JNDI bound JBoss Transactions Transaction Manager) Test was not
closing SessionFactory instance.
Modified:
core/trunk/cache-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/JBossStandaloneJtaExampleTest.java
===================================================================
---
core/trunk/cache-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/JBossStandaloneJtaExampleTest.java 2010-02-23
02:11:31 UTC (rev 18855)
+++
core/trunk/cache-infinispan/src/test/java/org/hibernate/test/cache/infinispan/tm/JBossStandaloneJtaExampleTest.java 2010-02-23
12:09:45 UTC (rev 18856)
@@ -91,48 +91,72 @@
public void testPersistAndLoadUnderJta() throws Exception {
Item item;
SessionFactory sessionFactory = buildSessionFactory();
- 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;
+ 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();
+ } 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();
+ assertNull(session.get(Item.class, item.getId()));
+ session.getTransaction().commit();
+ session.close();
+ } catch(Exception e) {
+ ut.setRollbackOnly();
+ throw e;
+ } finally {
+ if (ut.getStatus() == Status.STATUS_ACTIVE)
+ ut.commit();
+ else
+ ut.rollback();
+ }
} finally {
- if (ut.getStatus() == Status.STATUS_ACTIVE)
- ut.commit();
- else
- ut.rollback();
+ if (sessionFactory != null)
+ sessionFactory.close();
}
- 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();
- } catch(Exception e) {
- ut.setRollbackOnly();
- throw e;
- } finally {
- if (ut.getStatus() == Status.STATUS_ACTIVE)
- ut.commit();
- else
- ut.rollback();
- }
}
public static class ExtendedXADataSource extends StandardXADataSource { // XAPOOL
@@ -197,7 +221,7 @@
private void bindDataSource() throws Exception {
ExtendedXADataSource xads = new ExtendedXADataSource();
xads.setDriverName("org.hsqldb.jdbcDriver");
- xads.setUrl("jdbc:hsqldb:mem:/example");
+ xads.setUrl("jdbc:hsqldb:mem:/test");
ctx.bind("java:/MyDatasource", xads);
}
@@ -238,6 +262,7 @@
}
private SessionFactory buildSessionFactory() {
+ // Extra options located in src/test/resources/hibernate.properties
Configuration cfg = new Configuration();
cfg.setProperty(Environment.DIALECT,
"org.hibernate.dialect.HSQLDialect");
cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
@@ -264,6 +289,6 @@
Collection coll = (Collection) iter.next();
cfg.setCollectionCacheConcurrencyStrategy(coll.getRole(),
"transactional");
}
- return cfg.buildSessionFactory();
+ return cfg.buildSessionFactory();
}
}
Show replies by date