Search in sources :

Example 1 with JdbcCoordinatorImpl

use of org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl in project hibernate-orm by hibernate.

the class CloseEntityManagerWithActiveTransactionTest method testCommitReleasesLogicalConnection.

@Test
@TestForIssue(jiraKey = "HHH-11099")
public void testCommitReleasesLogicalConnection() throws Exception {
    TestingJtaPlatformImpl.INSTANCE.getTransactionManager().begin();
    EntityManager em = getOrCreateEntityManager();
    try {
        Box box = new Box();
        box.setColor("red-and-white");
        em.persist(box);
        final SessionImpl session = (SessionImpl) em.unwrap(Session.class);
        final JdbcCoordinatorImpl jdbcCoordinator = (JdbcCoordinatorImpl) session.getJdbcCoordinator();
        em.close();
        TestingJtaPlatformImpl.INSTANCE.getTransactionManager().commit();
        assertThat("The logical connection is still open after commit", jdbcCoordinator.getLogicalConnection().isOpen(), is(false));
    } catch (Exception e) {
        final TransactionManager transactionManager = TestingJtaPlatformImpl.INSTANCE.getTransactionManager();
        if (transactionManager.getTransaction() != null && transactionManager.getTransaction().getStatus() == Status.STATUS_ACTIVE) {
            TestingJtaPlatformImpl.INSTANCE.getTransactionManager().rollback();
        }
        throw e;
    } finally {
        if (em.isOpen()) {
            em.close();
        }
    }
}
Also used : JdbcCoordinatorImpl(org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl) EntityManager(javax.persistence.EntityManager) TransactionManager(javax.transaction.TransactionManager) SessionImpl(org.hibernate.internal.SessionImpl) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Aggregations

EntityManager (javax.persistence.EntityManager)1 TransactionManager (javax.transaction.TransactionManager)1 Session (org.hibernate.Session)1 JdbcCoordinatorImpl (org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl)1 SessionImpl (org.hibernate.internal.SessionImpl)1 TestForIssue (org.hibernate.testing.TestForIssue)1 Test (org.junit.Test)1