@Test
  public void existingNonSpringTransaction() {
    MockUOWManager manager = new MockUOWManager();
    manager.setUOWStatus(UOWManager.UOW_STATUS_ACTIVE);
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    assertEquals(
        "result",
        ptm.execute(
            definition,
            new TransactionCallback<String>() {
              @Override
              public String doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                return "result";
              }
            }));

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    assertEquals(0, manager.getUOWTimeout());
    assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
    assertTrue(manager.getJoined());
    assertFalse(manager.getRollbackOnly());
  }
  private void doTestExistingTransactionWithSuspension(final int propagationBehavior) {
    MockUOWManager manager = new MockUOWManager();
    final WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    final DefaultTransactionDefinition definition2 = new DefaultTransactionDefinition();
    definition2.setPropagationBehavior(propagationBehavior);
    definition2.setReadOnly(true);

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    assertEquals(
        "result",
        ptm.execute(
            definition,
            new TransactionCallback<String>() {
              @Override
              public String doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                assertEquals(
                    "result2",
                    ptm.execute(
                        definition2,
                        new TransactionCallback<String>() {
                          @Override
                          public String doInTransaction(TransactionStatus status) {
                            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                            assertEquals(
                                propagationBehavior
                                    == TransactionDefinition.PROPAGATION_REQUIRES_NEW,
                                TransactionSynchronizationManager.isActualTransactionActive());
                            assertTrue(
                                TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                            return "result2";
                          }
                        }));
                return "result";
              }
            }));

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    assertEquals(0, manager.getUOWTimeout());
    if (propagationBehavior == TransactionDefinition.PROPAGATION_REQUIRES_NEW) {
      assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
    } else {
      assertEquals(UOWManager.UOW_TYPE_LOCAL_TRANSACTION, manager.getUOWType());
    }
    assertFalse(manager.getJoined());
    assertFalse(manager.getRollbackOnly());
  }
  @Test
  public void existingTransactionUsingPropagationNotSupported() {
    MockUOWManager manager = new MockUOWManager();
    final WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    final DefaultTransactionDefinition definition2 = new DefaultTransactionDefinition();
    definition2.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
    definition2.setReadOnly(true);

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    assertEquals(
        "result",
        ptm.execute(
            definition,
            new TransactionCallback<String>() {
              @Override
              public String doInTransaction(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                assertEquals(
                    "result2",
                    ptm.execute(
                        definition2,
                        new TransactionCallback<String>() {
                          @Override
                          public String doInTransaction(TransactionStatus status) {
                            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                            assertFalse(
                                TransactionSynchronizationManager.isActualTransactionActive());
                            assertTrue(
                                TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                            return "result2";
                          }
                        }));
                return "result";
              }
            }));

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    assertEquals(0, manager.getUOWTimeout());
    assertEquals(UOWManager.UOW_TYPE_LOCAL_TRANSACTION, manager.getUOWType());
    assertFalse(manager.getJoined());
    assertFalse(manager.getRollbackOnly());
  }
Example #4
0
 /** Retrieve the Spring-managed Session for the current thread, if any. */
 public Session currentSession() throws HibernateException {
   Object value = TransactionSynchronizationManager.getResource(this.sessionFactory);
   if (value instanceof Session) {
     return (Session) value;
   } else if (value instanceof SessionHolder) {
     SessionHolder sessionHolder = (SessionHolder) value;
     Session session = sessionHolder.getSession();
     if (TransactionSynchronizationManager.isSynchronizationActive()
         && !sessionHolder.isSynchronizedWithTransaction()) {
       TransactionSynchronizationManager.registerSynchronization(
           new SpringSessionSynchronization(sessionHolder, this.sessionFactory));
       sessionHolder.setSynchronizedWithTransaction(true);
       // Switch to FlushMode.AUTO, as we have to assume a thread-bound Session
       // with FlushMode.MANUAL, which needs to allow flushing within the transaction.
       FlushMode flushMode = session.getFlushMode();
       if (FlushMode.isManualFlushMode(flushMode)
           && !TransactionSynchronizationManager.isCurrentTransactionReadOnly()) {
         session.setFlushMode(FlushMode.AUTO);
         sessionHolder.setPreviousFlushMode(flushMode);
       }
     }
     return session;
   } else if (this.jtaSessionContext != null) {
     Session session = this.jtaSessionContext.currentSession();
     if (TransactionSynchronizationManager.isSynchronizationActive()) {
       TransactionSynchronizationManager.registerSynchronization(
           new SpringFlushSynchronization(session));
     }
     return session;
   } else {
     throw new HibernateException("No Session found for current thread");
   }
 }
  @Test
  public void newTransactionWithCommitException() {
    final RollbackException rex = new RollbackException();
    MockUOWManager manager =
        new MockUOWManager() {
          @Override
          public void runUnderUOW(int type, boolean join, UOWAction action) throws UOWException {
            throw new UOWException(rex);
          }
        };
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    try {
      ptm.execute(
          definition,
          new TransactionCallback<String>() {
            @Override
            public String doInTransaction(TransactionStatus status) {
              assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
              assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
              assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
              return "result";
            }
          });
      fail("Should have thrown TransactionSystemException");
    } catch (TransactionSystemException ex) {
      // expected
      assertTrue(ex.getCause() instanceof UOWException);
      assertSame(rex, ex.getRootCause());
      assertSame(rex, ex.getMostSpecificCause());
    }

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    assertEquals(0, manager.getUOWTimeout());
  }
  @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());
  }
  private void doTestNewTransactionWithCommit(int propagationBehavior, final int synchMode) {
    MockUOWManager manager = new MockUOWManager();
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    ptm.setTransactionSynchronization(synchMode);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    definition.setPropagationBehavior(propagationBehavior);
    definition.setReadOnly(true);

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    assertEquals(
        "result",
        ptm.execute(
            definition,
            new TransactionCallback<String>() {
              @Override
              public String doInTransaction(TransactionStatus status) {
                if (synchMode != WebSphereUowTransactionManager.SYNCHRONIZATION_NEVER) {
                  assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                  assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
                  assertTrue(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                } else {
                  assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
                  assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
                  assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
                }
                return "result";
              }
            }));

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    assertEquals(0, manager.getUOWTimeout());
    assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
    assertFalse(manager.getJoined());
    assertFalse(manager.getRollbackOnly());
  }
 /**
  * Prepare a transaction on the given EntityManager, if possible.
  *
  * @param em the EntityManager to prepare
  * @param emf the EntityManagerFactory that the EntityManager has been created with
  * @return an arbitrary object that holds transaction data, if any (to be passed into
  *     cleanupTransaction)
  * @see JpaDialect#prepareTransaction
  */
 private static Object prepareTransaction(EntityManager em, EntityManagerFactory emf) {
   if (emf instanceof EntityManagerFactoryInfo) {
     EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf;
     JpaDialect jpaDialect = emfInfo.getJpaDialect();
     if (jpaDialect != null) {
       return jpaDialect.prepareTransaction(
           em,
           TransactionSynchronizationManager.isCurrentTransactionReadOnly(),
           TransactionSynchronizationManager.getCurrentTransactionName());
     }
   }
   return null;
 }
  @Test
  public void newTransactionWithRollback() {
    MockUOWManager manager = new MockUOWManager();
    WebSphereUowTransactionManager ptm = new WebSphereUowTransactionManager(manager);
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    try {
      ptm.execute(
          definition,
          new TransactionCallback<String>() {
            @Override
            public String doInTransaction(TransactionStatus status) {
              assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
              assertTrue(TransactionSynchronizationManager.isActualTransactionActive());
              assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());
              throw new OptimisticLockingFailureException("");
            }
          });
      fail("Should have thrown OptimisticLockingFailureException");
    } catch (OptimisticLockingFailureException ex) {
      // expected
    }

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    assertFalse(TransactionSynchronizationManager.isActualTransactionActive());
    assertFalse(TransactionSynchronizationManager.isCurrentTransactionReadOnly());

    assertEquals(0, manager.getUOWTimeout());
    assertEquals(UOWManager.UOW_TYPE_GLOBAL_TRANSACTION, manager.getUOWType());
    assertFalse(manager.getJoined());
    assertTrue(manager.getRollbackOnly());
  }
 private static boolean isActualNonReadonlyTransactionActive() {
   return TransactionSynchronizationManager.isActualTransactionActive()
       && !TransactionSynchronizationManager.isCurrentTransactionReadOnly();
 }
 @Override
 public boolean isReadOnly() {
   return TransactionSynchronizationManager.isCurrentTransactionReadOnly();
 }