@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(); }
@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(); }
@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); }
@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); }
@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); }
@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); }
@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(); }
@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); }
@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); }
@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()); }
@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()); }
@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); }
@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); }
@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()); }
@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); }
@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()); }
@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 } }
@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 } }
@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(); }
@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 } }
@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 } }
@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 } }
@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 } }
@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(); }
@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(); }
@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(); }
@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); }
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()); } }
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()); } } }
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); } } }
@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(); }
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()); } }
@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")); }
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(); }
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); }
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(); }
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); }