@After public void tearDown() throws Exception { if (this.transactionStatus != null && !this.transactionStatus.isCompleted()) { endTransaction(); } assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly()); assertFalse(TransactionSynchronizationManager.isActualTransactionActive()); }
@Override public void commit() { if (txLogger.isInfoEnabled()) { txLogger.info("commit"); } ThreadContext.Context ctx = ThreadContext.getContext(this.metaData.getName()); if (ctx == null) { if (txLogger.isWarnEnabled()) { txLogger.warn("ignoring commit - no tx in progress"); if (txLogger.isDebugEnabled()) { logStackTrace(); } } return; } TransactionStatus txStatus = ctx.getTransactionStatus(); try { if (txStatus == null) { if (txLogger.isWarnEnabled()) { txLogger.warn("ignoring commit - no tx status"); if (txLogger.isDebugEnabled()) { logStackTrace(); } } } else { this.txMgr.commit(txStatus); } } finally { ctx.setTransactionStatus(null); ThreadContext.unsetContext(this.metaData.getName()); HashMap<String, ThreadContext.Context> contextHash = ThreadContext.getThreadLocalHash(); if (contextHash != null && contextHash.size() > 0) { if (!TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager.initSynchronization(); } } else { if (TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager.clear(); Map map = TransactionSynchronizationManager.getResourceMap(); for (Object entry : map.keySet()) { TransactionSynchronizationManager.unbindResource(entry); } } } } }
/* * Test method for * 'org.springframework.orm.jpa.EntityManagerFactoryUtils.doGetEntityManager(EntityManagerFactory)' */ @Test public void testDoGetEntityManager() { // test null assertion try { EntityManagerFactoryUtils.doGetTransactionalEntityManager(null, null); fail("expected exception"); } catch (IllegalArgumentException ex) { // it's okay } EntityManagerFactory factory = mock(EntityManagerFactory.class); // no tx active assertNull(EntityManagerFactoryUtils.doGetTransactionalEntityManager(factory, null)); assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); }
@Test public void testDoGetEntityManagerWithTx() throws Exception { try { EntityManagerFactory factory = mock(EntityManagerFactory.class); EntityManager manager = mock(EntityManager.class); TransactionSynchronizationManager.initSynchronization(); given(factory.createEntityManager()).willReturn(manager); // no tx active assertSame(manager, EntityManagerFactoryUtils.doGetTransactionalEntityManager(factory, null)); assertSame( manager, ((EntityManagerHolder) TransactionSynchronizationManager.unbindResource(factory)) .getEntityManager()); } finally { TransactionSynchronizationManager.clearSynchronization(); } assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); }
protected void tearDown() { assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty()); assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); }