コード例 #1
0
 /**
  * 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);
 }
コード例 #2
0
  @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());
  }
コード例 #3
0
  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();
   }
 }