/** * Clear the entire transaction synchronization state for the current thread: registered * synchronizations as well as the various transaction characteristics. * * @see #clearSynchronization() * @see #setCurrentTransactionName * @see #setCurrentTransactionReadOnly * @see #setCurrentTransactionIsolationLevel * @see #setActualTransactionActive */ public static void clear() { clearSynchronization(); setCurrentTransactionName(null); setCurrentTransactionReadOnly(false); setCurrentTransactionIsolationLevel(null); setActualTransactionActive(false); }
@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()); }
public void upgradeLevels() throws Exception { TransactionSynchronizationManager.initSynchronization(); Connection c = DataSourceUtils.getConnection(dataSource); c.setAutoCommit(false); try { List<User> users = userDao.getAll(); for (User user : users) { if (canUpgradeLevel(user)) { upgradeLevel(user); } } c.commit(); } catch (Exception e) { c.rollback(); throw e; } finally { DataSourceUtils.releaseConnection(c, dataSource); TransactionSynchronizationManager.unbindResource(this.dataSource); TransactionSynchronizationManager.clearSynchronization(); } }
@Before public void prepareTestedInstance() throws Exception { if (TransactionSynchronizationManager.isSynchronizationActive()) { TransactionSynchronizationManager.clearSynchronization(); } }