[HHH-4640] (Add test with JNDI bound JBoss Transactions Transaction Manager) Test was not closing SessionFactory instance.
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@18856 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
2b72b05522
commit
011ef60c10
|
@ -91,48 +91,72 @@ public class JBossStandaloneJtaExampleTest extends TestCase {
|
||||||
public void testPersistAndLoadUnderJta() throws Exception {
|
public void testPersistAndLoadUnderJta() throws Exception {
|
||||||
Item item;
|
Item item;
|
||||||
SessionFactory sessionFactory = buildSessionFactory();
|
SessionFactory sessionFactory = buildSessionFactory();
|
||||||
UserTransaction ut = (UserTransaction) ctx.lookup("UserTransaction");
|
|
||||||
ut.begin();
|
|
||||||
try {
|
try {
|
||||||
Session session = sessionFactory.openSession();
|
UserTransaction ut = (UserTransaction) ctx.lookup("UserTransaction");
|
||||||
session.getTransaction().begin();
|
ut.begin();
|
||||||
item = new Item("anItem", "An item owned by someone");
|
try {
|
||||||
session.persist(item);
|
Session session = sessionFactory.openSession();
|
||||||
session.getTransaction().commit();
|
session.getTransaction().begin();
|
||||||
session.close();
|
item = new Item("anItem", "An item owned by someone");
|
||||||
} catch(Exception e) {
|
session.persist(item);
|
||||||
ut.setRollbackOnly();
|
session.getTransaction().commit();
|
||||||
throw e;
|
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 {
|
} finally {
|
||||||
if (ut.getStatus() == Status.STATUS_ACTIVE)
|
if (sessionFactory != null)
|
||||||
ut.commit();
|
sessionFactory.close();
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ExtendedXADataSource extends StandardXADataSource { // XAPOOL
|
public static class ExtendedXADataSource extends StandardXADataSource { // XAPOOL
|
||||||
|
@ -197,7 +221,7 @@ public class JBossStandaloneJtaExampleTest extends TestCase {
|
||||||
private void bindDataSource() throws Exception {
|
private void bindDataSource() throws Exception {
|
||||||
ExtendedXADataSource xads = new ExtendedXADataSource();
|
ExtendedXADataSource xads = new ExtendedXADataSource();
|
||||||
xads.setDriverName("org.hsqldb.jdbcDriver");
|
xads.setDriverName("org.hsqldb.jdbcDriver");
|
||||||
xads.setUrl("jdbc:hsqldb:mem:/example");
|
xads.setUrl("jdbc:hsqldb:mem:/test");
|
||||||
ctx.bind("java:/MyDatasource", xads);
|
ctx.bind("java:/MyDatasource", xads);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -238,6 +262,7 @@ public class JBossStandaloneJtaExampleTest extends TestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
private SessionFactory buildSessionFactory() {
|
private SessionFactory buildSessionFactory() {
|
||||||
|
// Extra options located in src/test/resources/hibernate.properties
|
||||||
Configuration cfg = new Configuration();
|
Configuration cfg = new Configuration();
|
||||||
cfg.setProperty(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
|
cfg.setProperty(Environment.DIALECT, "org.hibernate.dialect.HSQLDialect");
|
||||||
cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
|
cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
|
||||||
|
@ -264,6 +289,6 @@ public class JBossStandaloneJtaExampleTest extends TestCase {
|
||||||
Collection coll = (Collection) iter.next();
|
Collection coll = (Collection) iter.next();
|
||||||
cfg.setCollectionCacheConcurrencyStrategy(coll.getRole(), "transactional");
|
cfg.setCollectionCacheConcurrencyStrategy(coll.getRole(), "transactional");
|
||||||
}
|
}
|
||||||
return cfg.buildSessionFactory();
|
return cfg.buildSessionFactory();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue