add closeEM() helper which handles open transactions before closing the EM

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1022161 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald Woods 2010-10-13 16:06:14 +00:00
parent f3e45e5e54
commit 54e64edaa2
1 changed files with 19 additions and 8 deletions

View File

@ -238,6 +238,23 @@ public abstract class AbstractPersistenceTestCase extends TestCase {
return brc; return brc;
} }
/**
* Safely close the given EM
*
* @param em
* @return
*/
protected boolean closeEM(EntityManager em) {
if (em == null || !em.isOpen()) {
return false;
}
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
return true;
}
/** /**
* Closes all open entity managers after first rolling back any open transactions. * Closes all open entity managers after first rolling back any open transactions.
*/ */
@ -249,10 +266,7 @@ public abstract class AbstractPersistenceTestCase extends TestCase {
for (Broker b : ((AbstractBrokerFactory) JPAFacadeHelper.toBrokerFactory(emf)).getOpenBrokers()) { for (Broker b : ((AbstractBrokerFactory) JPAFacadeHelper.toBrokerFactory(emf)).getOpenBrokers()) {
if (b != null && !b.isClosed()) { if (b != null && !b.isClosed()) {
EntityManager em = JPAFacadeHelper.toEntityManager(b); EntityManager em = JPAFacadeHelper.toEntityManager(b);
if (em.getTransaction().isActive()) { closeEM(em);
em.getTransaction().rollback();
}
em.close();
} }
} }
} }
@ -327,10 +341,7 @@ public abstract class AbstractPersistenceTestCase extends TestCase {
} catch (Exception e) { } catch (Exception e) {
// ignore // ignore
} finally { } finally {
if (em.getTransaction().isActive()) { closeEM(em);
em.getTransaction().rollback();
}
em.close();
} }
} }