Java 类org.springframework.transaction.jta.JtaTransactionManager 实例源码

项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithCommitAndSynchronizationNever() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(
    Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
    ptm.afterPropertiesSet();

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).begin();
    verify(ut).commit();
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithRollbackAndSynchronizationNever() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronizationName("SYNCHRONIZATION_NEVER");
    tt.setTimeout(10);
    ptm.afterPropertiesSet();

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setTransactionTimeout(10);
    verify(ut).begin();
    verify(ut, atLeastOnce()).getStatus();
    verify(ut).rollback();
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithExistingTransactionAndRollbackOnly() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithExistingTransactionAndCommitException() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);
    willThrow(new OptimisticLockingFailureException("")).given(synch).beforeCommit(false);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    try {
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
                TransactionSynchronizationManager.registerSynchronization(synch);
            }
        });
        fail("Should have thrown OptimisticLockingFailureException");
    }
    catch (OptimisticLockingFailureException ex) {
        // expected
    }
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithExistingTransactionAndRollbackOnlyAndNoGlobalRollback() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    ptm.setGlobalRollbackOnParticipationFailure(false);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithExistingTransactionAndSynchronizationOnActual() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithExistingTransactionAndSynchronizationNever() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
    ptm.afterPropertiesSet();

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithExistingAndPropagationSupports() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithPropagationSupports() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithPropagationSupportsAndSynchronizationOnActual() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    ptm.afterPropertiesSet();

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithPropagationSupportsAndSynchronizationNever() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
    ptm.afterPropertiesSet();

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithPropagationNotSupported() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);

    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(tm).resume(tx);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExisting() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);

    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).begin();
    verify(ut).commit();
    verify(tm).resume(tx);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndExistingWithSuspendException() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    TransactionManager tm = mock(TransactionManager.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    willThrow(new SystemException()).given(tm).suspend();

    JtaTransactionManager ptm = newJtaTransactionManager(ut, tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    try {
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            }
        });
        fail("Should have thrown TransactionSystemException");
    }
    catch (TransactionSystemException ex) {
        // expected
    }
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndAdapter() throws Exception {
    TransactionManager tm = mock(TransactionManager.class);
    Transaction tx = mock(Transaction.class);
    given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
    given(tm.suspend()).willReturn(tx);

    JtaTransactionManager ptm = newJtaTransactionManager(tm);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(tm).begin();
    verify(tm).commit();
    verify(tm).resume(tx);
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithPropagationRequiresNewAndSuspensionNotSupported() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    try {
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
            }
        });
        fail("Should have thrown TransactionSuspensionNotSupportedException");
    }
    catch (TransactionSuspensionNotSupportedException ex) {
        // expected
    }
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithIsolationLevel() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);

    try {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.setIsolationLevel(TransactionDefinition.ISOLATION_SERIALIZABLE);
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                // something transactional
            }
        });
        fail("Should have thrown InvalidIsolationLevelException");
    }
    catch (InvalidIsolationLevelException ex) {
        // expected
    }
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithSystemExceptionOnIsExisting() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willThrow(new SystemException("system exception"));

    try {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                // something transactional
            }
        });
        fail("Should have thrown TransactionSystemException");
    }
    catch (TransactionSystemException ex) {
        // expected
    }
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithNestedBegin() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            // something transactional
        }
    });

    verify(ut).begin();
    verify(ut).commit();
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithNotSupportedExceptionOnNestedBegin() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    willThrow(new NotSupportedException("not supported")).given(ut).begin();

    try {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                // something transactional
            }
        });
        fail("Should have thrown NestedTransactionNotSupportedException");
    }
    catch (NestedTransactionNotSupportedException ex) {
        // expected
    }
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithUnsupportedOperationExceptionOnNestedBegin() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    willThrow(new UnsupportedOperationException("not supported")).given(ut).begin();

    try {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NESTED);
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                // something transactional
            }
        });
        fail("Should have thrown NestedTransactionNotSupportedException");
    }
    catch (NestedTransactionNotSupportedException ex) {
        // expected
    }
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithSystemExceptionOnBegin() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION);
    willThrow(new SystemException("system exception")).given(ut).begin();

    try {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                // something transactional
            }
        });
        fail("Should have thrown CannotCreateTransactionException");
    }
    catch (CannotCreateTransactionException ex) {
        // expected
    }
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithIllegalStateExceptionOnRollbackOnly() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);
    willThrow(new IllegalStateException("no existing transaction")).given(ut).setRollbackOnly();

    try {
        JtaTransactionManager ptm = newJtaTransactionManager(ut);
        TransactionTemplate tt = new TransactionTemplate(ptm);
        tt.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                status.setRollbackOnly();
            }
        });
        fail("Should have thrown TransactionSystemException");
    }
    catch (TransactionSystemException ex) {
        // expected
    }
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithDoubleCommit() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION,
            Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
    assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
    // first commit
    ptm.commit(status);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    try {
        // second commit attempt
        ptm.commit(status);
        fail("Should have thrown IllegalTransactionStateException");
    }
    catch (IllegalTransactionStateException ex) {
        // expected
    }

    verify(ut).begin();
    verify(ut).commit();
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithDoubleRollback() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
    assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
    // first rollback
    ptm.rollback(status);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    try {
        // second rollback attempt
        ptm.rollback(status);
        fail("Should have thrown IllegalTransactionStateException");
    }
    catch (IllegalTransactionStateException ex) {
        // expected
    }

    verify(ut).begin();
    verify(ut).rollback();
}
项目:spring4-understanding    文件:JtaTransactionManagerTests.java   
@Test
public void jtaTransactionManagerWithRollbackAndCommit() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    TransactionStatus status = ptm.getTransaction(new DefaultTransactionDefinition());
    assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
    // first: rollback
    ptm.rollback(status);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    try {
        // second: commit attempt
        ptm.commit(status);
        fail("Should have thrown IllegalTransactionStateException");
    }
    catch (IllegalTransactionStateException ex) {
        // expected
    }

    verify(ut).begin();
    verify(ut).rollback();
}
项目:spring4-understanding    文件:HibernateTransactionManagerTests.java   
@Test
public void testSetJtaTransactionManager() throws Exception {
    DataSource ds = mock(DataSource.class);
    TransactionManager tm = mock(TransactionManager.class);
    UserTransaction ut = mock(UserTransaction.class);
    TransactionSynchronizationRegistry tsr = mock(TransactionSynchronizationRegistry.class);
    JtaTransactionManager jtm = new JtaTransactionManager();
    jtm.setTransactionManager(tm);
    jtm.setUserTransaction(ut);
    jtm.setTransactionSynchronizationRegistry(tsr);
    LocalSessionFactoryBuilder lsfb = new LocalSessionFactoryBuilder(ds);
    lsfb.setJtaTransactionManager(jtm);
    Object jtaPlatform = lsfb.getProperties().get(AvailableSettings.JTA_PLATFORM);
    assertNotNull(jtaPlatform);
    assertSame(tm, jtaPlatform.getClass().getMethod("retrieveTransactionManager").invoke(jtaPlatform));
    assertSame(ut, jtaPlatform.getClass().getMethod("retrieveUserTransaction").invoke(jtaPlatform));
    assertTrue(lsfb.getProperties().get(AvailableSettings.TRANSACTION_STRATEGY) instanceof CMTTransactionFactory);
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:HibernateJpaAutoConfiguration.java   
private void configureJtaPlatform(Map<String, Object> vendorProperties)
        throws LinkageError {
    JtaTransactionManager jtaTransactionManager = getJtaTransactionManager();
    if (jtaTransactionManager != null) {
        if (runningOnWebSphere()) {
            // We can never use SpringJtaPlatform on WebSphere as
            // WebSphereUowTransactionManager has a null TransactionManager
            // which will cause Hibernate to NPE
            configureWebSphereTransactionPlatform(vendorProperties);
        }
        else {
            configureSpringJtaPlatform(vendorProperties, jtaTransactionManager);
        }
    }
    else {
        vendorProperties.put(JTA_PLATFORM, getNoJtaPlatformManager());
    }
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:HibernateJpaAutoConfiguration.java   
private void configureSpringJtaPlatform(Map<String, Object> vendorProperties,
        JtaTransactionManager jtaTransactionManager) {
    try {
        vendorProperties.put(JTA_PLATFORM,
                new SpringJtaPlatform(jtaTransactionManager));
    }
    catch (LinkageError ex) {
        // NoClassDefFoundError can happen if Hibernate 4.2 is used and some
        // containers (e.g. JBoss EAP 6) wraps it in the superclass LinkageError
        if (!isUsingJndi()) {
            throw new IllegalStateException("Unable to set Hibernate JTA "
                    + "platform, are you using the correct "
                    + "version of Hibernate?", ex);
        }
        // Assume that Hibernate will use JNDI
        if (logger.isDebugEnabled()) {
            logger.debug("Unable to set Hibernate JTA platform : " + ex.getMessage());
        }
    }
}
项目:ByteTCC    文件:CompensableManagerPostProcessor.java   
private void configureTransactionManager() {
    Iterator<Object> itr = this.beanList.iterator();
    while (itr.hasNext()) {
        Object object = itr.next();
        if (UserCompensableImpl.class.isInstance(object)) {
            itr.remove();

            UserCompensableImpl compensable = (UserCompensableImpl) object;
            compensable.setTransactionManager(this.transactionManager);
        } else if (LocalXADataSource.class.isInstance(object)) {
            itr.remove();

            LocalXADataSource dataSource = (LocalXADataSource) object;
            dataSource.setTransactionManager(this.transactionManager);
        } else if (JtaTransactionManager.class.isInstance(object)) {
            itr.remove();

            JtaTransactionManager jtaTxMgr = (JtaTransactionManager) object;
            jtaTxMgr.setTransactionManager(this.transactionManager);
        }
    }
}
项目:spring-boot-concourse    文件:HibernateJpaAutoConfiguration.java   
private void configureJtaPlatform(Map<String, Object> vendorProperties)
        throws LinkageError {
    JtaTransactionManager jtaTransactionManager = getJtaTransactionManager();
    if (jtaTransactionManager != null) {
        if (runningOnWebSphere()) {
            // We can never use SpringJtaPlatform on WebSphere as
            // WebSphereUowTransactionManager has a null TransactionManager
            // which will cause Hibernate to NPE
            configureWebSphereTransactionPlatform(vendorProperties);
        }
        else {
            configureSpringJtaPlatform(vendorProperties, jtaTransactionManager);
        }
    }
    else {
        vendorProperties.put(JTA_PLATFORM, getNoJtaPlatformManager());
    }
}
项目:spring-boot-concourse    文件:HibernateJpaAutoConfiguration.java   
private void configureSpringJtaPlatform(Map<String, Object> vendorProperties,
        JtaTransactionManager jtaTransactionManager) {
    try {
        vendorProperties.put(JTA_PLATFORM,
                new SpringJtaPlatform(jtaTransactionManager));
    }
    catch (LinkageError ex) {
        // NoClassDefFoundError can happen if Hibernate 4.2 is used and some
        // containers (e.g. JBoss EAP 6) wraps it in the superclass LinkageError
        if (!isUsingJndi()) {
            throw new IllegalStateException("Unable to set Hibernate JTA "
                    + "platform, are you using the correct "
                    + "version of Hibernate?", ex);
        }
        // Assume that Hibernate will use JNDI
        if (logger.isDebugEnabled()) {
            logger.debug("Unable to set Hibernate JTA platform : " + ex.getMessage());
        }
    }
}
项目:mybatis-spring-1.2.2    文件:MyBatisSpringTest.java   
@Test
public void testWithJtaTxManager() {
  JtaTransactionManager jtaManager = new JtaTransactionManager(new MockUserTransaction());

  DefaultTransactionDefinition txDef = new DefaultTransactionDefinition();
  txDef.setPropagationBehaviorName("PROPAGATION_REQUIRED");

  TransactionStatus status = jtaManager.getTransaction(txDef);

  session = SqlSessionUtils.getSqlSession(sqlSessionFactory);
  session.getMapper(TestMapper.class).findTest();
  SqlSessionUtils.closeSqlSession(session, sqlSessionFactory);

  jtaManager.commit(status);

  // assume a real JTA tx would enlist and commit the JDBC connection
  assertNoCommitJdbc();
  assertCommitSession();
  assertSingleConnection();
}
项目:contestparser    文件:HibernateJpaAutoConfiguration.java   
private void configureJtaPlatform(Map<String, Object> vendorProperties)
        throws LinkageError {
    JtaTransactionManager jtaTransactionManager = getJtaTransactionManager();
    if (jtaTransactionManager != null) {
        if (runningOnWebSphere()) {
            // We can never use SpringJtaPlatform on WebSphere as
            // WebSphereUowTransactionManger has a null TransactionManager
            // which will cause Hibernate to NPE
            configureWebSphereTransactionPlatform(vendorProperties);
        }
        else {
            configureSpringJtaPlatform(vendorProperties, jtaTransactionManager);
        }
    }
    else {
        vendorProperties.put(JTA_PLATFORM, getNoJtaPlatformManager());
    }
}
项目:contestparser    文件:HibernateJpaAutoConfiguration.java   
private void configureSpringJtaPlatform(Map<String, Object> vendorProperties,
        JtaTransactionManager jtaTransactionManager) {
    try {
        vendorProperties.put(JTA_PLATFORM,
                new SpringJtaPlatform(jtaTransactionManager));
    }
    catch (LinkageError ex) {
        // NoClassDefFoundError can happen if Hibernate 4.2 is used and some
        // containers (e.g. JBoss EAP 6) wraps it in the superclass LinkageError
        if (!isUsingJndi()) {
            throw new IllegalStateException("Unable to set Hibernate JTA "
                    + "platform, are you using the correct "
                    + "version of Hibernate?", ex);
        }
        // Assume that Hibernate will use JNDI
        if (logger.isDebugEnabled()) {
            logger.debug("Unable to set Hibernate JTA platform : " + ex.getMessage());
        }
    }
}
项目:contestparser    文件:JmsAutoConfigurationTests.java   
@Test
public void testDefaultContainerFactoryWithJtaTransactionManager() {
    this.context = createContext(TestConfiguration7.class,
            EnableJmsConfiguration.class);
    JmsListenerContainerFactory<?> jmsListenerContainerFactory = this.context.getBean(
            "jmsListenerContainerFactory", JmsListenerContainerFactory.class);
    assertEquals(DefaultJmsListenerContainerFactory.class,
            jmsListenerContainerFactory.getClass());
    DefaultMessageListenerContainer listenerContainer = ((DefaultJmsListenerContainerFactory) jmsListenerContainerFactory)
            .createListenerContainer(mock(JmsListenerEndpoint.class));
    assertFalse("wrong session transacted flag with JTA transactions",
            listenerContainer.isSessionTransacted());
    assertSame(this.context.getBean(JtaTransactionManager.class),
            new DirectFieldAccessor(listenerContainer)
                    .getPropertyValue("transactionManager"));
}
项目:class-guard    文件:JtaTransactionManagerTests.java   
public void testJtaTransactionManagerWithCommitAndSynchronizationNever() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(
    Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE, Status.STATUS_ACTIVE);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_NEVER);
    ptm.afterPropertiesSet();

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).begin();
    verify(ut).commit();
}
项目:class-guard    文件:JtaTransactionManagerTests.java   
public void testJtaTransactionManagerWithRollbackAndSynchronizationOnActual() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);
    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronization(JtaTransactionManager.SYNCHRONIZATION_ON_ACTUAL_TRANSACTION);
    tt.setTimeout(10);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setTransactionTimeout(10);
    verify(ut).begin();
    verify(ut).rollback();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
}
项目:class-guard    文件:JtaTransactionManagerTests.java   
public void testJtaTransactionManagerWithRollbackAndSynchronizationNever() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_NO_TRANSACTION, Status.STATUS_ACTIVE);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    ptm.setTransactionSynchronizationName("SYNCHRONIZATION_NEVER");
    tt.setTimeout(10);
    ptm.afterPropertiesSet();

    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setTransactionTimeout(10);
    verify(ut).begin();
    verify(ut, atLeastOnce()).getStatus();
    verify(ut).rollback();
}
项目:class-guard    文件:JtaTransactionManagerTests.java   
public void testJtaTransactionManagerWithExistingTransactionAndRollbackOnly() throws Exception {
    UserTransaction ut = mock(UserTransaction.class);
    given(ut.getStatus()).willReturn(Status.STATUS_ACTIVE);

    final TransactionSynchronization synch = mock(TransactionSynchronization.class);

    JtaTransactionManager ptm = newJtaTransactionManager(ut);
    TransactionTemplate tt = new TransactionTemplate(ptm);
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
    tt.execute(new TransactionCallbackWithoutResult() {
        @Override
        protected void doInTransactionWithoutResult(TransactionStatus status) {
            assertTrue(TransactionSynchronizationManager.isSynchronizationActive());
            TransactionSynchronizationManager.registerSynchronization(synch);
            status.setRollbackOnly();
        }
    });
    assertFalse(TransactionSynchronizationManager.isSynchronizationActive());

    verify(ut).setRollbackOnly();
    verify(synch).beforeCompletion();
    verify(synch).afterCompletion(TransactionSynchronization.STATUS_UNKNOWN);
}