Search in sources :

Example 26 with TransactionManager

use of javax.transaction.TransactionManager in project hibernate-orm by hibernate.

the class AbstractBasicJtaTestScenarios method testSynchronizationFailure.

@Test
@SuppressWarnings("EmptyCatchBlock")
public void testSynchronizationFailure() throws Exception {
    JtaTransactionCoordinatorImpl transactionCoordinator = new JtaTransactionCoordinatorImpl(transactionCoordinatorBuilder, owner, true, JtaPlatformStandardTestingImpl.INSTANCE, preferUserTransactions(), true);
    // pre conditions
    final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
    assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
    assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    transactionCoordinator.getLocalSynchronizations().registerSynchronization(SynchronizationErrorImpl.forBefore());
    transactionCoordinator.getTransactionDriverControl().begin();
    assertEquals(TransactionStatus.ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    try {
        transactionCoordinator.getTransactionDriverControl().commit();
    } catch (Exception expected) {
    } finally {
        assertEquals(TransactionStatus.NOT_ACTIVE, transactionCoordinator.getTransactionDriverControl().getStatus());
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) JtaTransactionCoordinatorImpl(org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl) TransactionException(org.hibernate.TransactionException) TransactionRequiredForJoinException(org.hibernate.resource.transaction.TransactionRequiredForJoinException) SystemException(javax.transaction.SystemException) Test(org.junit.Test)

Example 27 with TransactionManager

use of javax.transaction.TransactionManager in project geode by apache.

the class JNDIInvoker method doTransactionLookup.

/*
   * Helps in locating TransactionManager in presence of application server scenario. Stores the
   * value of TransactionManager for reference in GemFire system.
   */
private static void doTransactionLookup() throws NamingException {
    Object jndiObject = null;
    LogWriterI18n writer = TransactionUtils.getLogWriterI18n();
    for (int i = 0; i < knownJNDIManagers.length; i++) {
        try {
            jndiObject = ctx.lookup(knownJNDIManagers[i][0]);
        } catch (NamingException e) {
            String exception = "JNDIInvoker::doTransactionLookup::Couldn't lookup [" + knownJNDIManagers[i][0] + " (" + knownJNDIManagers[i][1] + ")]";
            if (writer.finerEnabled())
                writer.finer(exception);
        }
        if (jndiObject instanceof TransactionManager) {
            transactionManager = (TransactionManager) jndiObject;
            String exception = "JNDIInvoker::doTransactionLookup::Found TransactionManager for " + knownJNDIManagers[i][1];
            if (writer.fineEnabled())
                writer.fine(exception);
            return;
        } else {
            String exception = "JNDIInvoker::doTransactionLookup::Found TransactionManager of class " + (jndiObject == null ? "null" : jndiObject.getClass()) + " but is not of type javax.transaction.TransactionManager";
            if (writer.fineEnabled())
                writer.fine(exception);
        }
    }
    Class clazz;
    try {
        if (writer.finerEnabled())
            writer.finer("JNDIInvoker::doTransactionLookup::Trying WebSphere 5.1: " + WS_FACTORY_CLASS_5_1);
        clazz = ClassPathLoader.getLatest().forName(WS_FACTORY_CLASS_5_1);
        if (writer.fineEnabled())
            writer.fine("JNDIInvoker::doTransactionLookup::Found WebSphere 5.1: " + WS_FACTORY_CLASS_5_1);
    } catch (ClassNotFoundException ex) {
        try {
            if (writer.finerEnabled())
                writer.finer("JNDIInvoker::doTransactionLookup::Trying WebSphere 5.0: " + WS_FACTORY_CLASS_5_0);
            clazz = ClassPathLoader.getLatest().forName(WS_FACTORY_CLASS_5_0);
            if (writer.fineEnabled())
                writer.fine("JNDIInvoker::doTransactionLookup::Found WebSphere 5.0: " + WS_FACTORY_CLASS_5_0);
        } catch (ClassNotFoundException ex2) {
            try {
                clazz = ClassPathLoader.getLatest().forName(WS_FACTORY_CLASS_4);
                String exception = "JNDIInvoker::doTransactionLookup::Found WebSphere 4: " + WS_FACTORY_CLASS_4;
                if (writer.fineEnabled())
                    writer.fine(exception, ex);
            } catch (ClassNotFoundException ex3) {
                if (writer.finerEnabled())
                    writer.finer("JNDIInvoker::doTransactionLookup::Couldn't find any WebSphere TransactionManager factory class, neither for WebSphere version 5.1 nor 5.0 nor 4");
                throw new NoInitialContextException();
            }
        }
    }
    try {
        Method method = clazz.getMethod("getTransactionManager", (Class[]) null);
        transactionManager = (TransactionManager) method.invoke(null, (Object[]) null);
    } catch (Exception ex) {
        writer.warning(LocalizedStrings.JNDIInvoker_JNDIINVOKER_DOTRANSACTIONLOOKUP_FOUND_WEBSPHERE_TRANSACTIONMANAGER_FACTORY_CLASS_0_BUT_COULDNT_INVOKE_ITS_STATIC_GETTRANSACTIONMANAGER_METHOD, clazz.getName(), ex);
        throw new NameNotFoundException(LocalizedStrings.JNDIInvoker_JNDIINVOKER_DOTRANSACTIONLOOKUP_FOUND_WEBSPHERE_TRANSACTIONMANAGER_FACTORY_CLASS_0_BUT_COULDNT_INVOKE_ITS_STATIC_GETTRANSACTIONMANAGER_METHOD.toLocalizedString(new Object[] { clazz.getName() }));
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) LogWriterI18n(org.apache.geode.i18n.LogWriterI18n) Method(java.lang.reflect.Method) DataSourceCreateException(org.apache.geode.internal.datasource.DataSourceCreateException) SystemException(javax.transaction.SystemException)

Example 28 with TransactionManager

use of javax.transaction.TransactionManager in project geode by apache.

the class RestartJUnitTest method testCleanUp.

@Test
public void testCleanUp() {
    TransactionManager tm1 = null;
    TransactionManager tm2 = null;
    try {
        props = new Properties();
        props.setProperty(MCAST_PORT, "0");
        String path = TestUtil.getResourcePath(RestartJUnitTest.class, "/jta/cachejta.xml");
        props.setProperty(CACHE_XML_FILE, path);
        ds1 = DistributedSystem.connect(props);
        cache = CacheFactory.create(ds1);
        tm1 = JNDIInvoker.getTransactionManager();
        cache.close();
        ds1.disconnect();
        ds1 = DistributedSystem.connect(props);
        cache = CacheFactory.create(ds1);
        tm2 = JNDIInvoker.getTransactionManager();
        assertNotSame("TransactionManager are same in before restart and after restart", tm1, tm2);
        ds1.disconnect();
    } catch (Exception e) {
        fail("Failed in restarting the distributed system");
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 29 with TransactionManager

use of javax.transaction.TransactionManager in project ignite by apache.

the class CacheJtaManager method start0.

/** {@inheritDoc} */
@Override
protected void start0() throws IgniteCheckedException {
    super.start0();
    if (cctx.txConfig() != null) {
        tmFactory = cctx.txConfig().getTxManagerFactory();
        if (tmFactory != null) {
            cctx.kernalContext().resource().injectGeneric(tmFactory);
            if (tmFactory instanceof LifecycleAware)
                ((LifecycleAware) tmFactory).start();
            Object txMgr;
            try {
                txMgr = tmFactory.create();
            } catch (Exception e) {
                throw new IgniteCheckedException("Failed to create transaction manager [tmFactory=" + tmFactory + "]", e);
            }
            if (txMgr == null)
                throw new IgniteCheckedException("Failed to create transaction manager (transaction manager " + "factory created null-value transaction manager) [tmFactory=" + tmFactory + "]");
            if (!(txMgr instanceof TransactionManager))
                throw new IgniteCheckedException("Failed to create transaction manager (transaction manager " + "factory created object that is not an instance of TransactionManager) [tmFactory=" + tmFactory + ", txMgr=" + txMgr + "]");
            jtaTm = (TransactionManager) txMgr;
        } else {
            String txLookupClsName = cctx.txConfig().getTxManagerLookupClassName();
            if (txLookupClsName != null)
                tmLookupRef.set(createTmLookup(txLookupClsName));
        }
        useJtaSync = cctx.txConfig().isUseJtaSynchronization();
    }
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) LifecycleAware(org.apache.ignite.lifecycle.LifecycleAware) TransactionManager(javax.transaction.TransactionManager) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException)

Example 30 with TransactionManager

use of javax.transaction.TransactionManager in project ignite by apache.

the class CacheJndiTmFactorySelfTest method testFactoryException.

/**
     * @throws Exception If failed.
     */
public void testFactoryException() throws Exception {
    final CacheJndiTmFactory f = new CacheJndiTmFactory("wrongJndiName", NOT_TM_JNDI_NAME, "wrongJndiName2");
    GridTestUtils.assertThrows(log, new Callable<TransactionManager>() {

        @Override
        public TransactionManager call() throws Exception {
            return f.create();
        }
    }, IgniteException.class, "Failed to lookup TM by");
}
Also used : TransactionManager(javax.transaction.TransactionManager) CacheJndiTmFactory(org.apache.ignite.cache.jta.jndi.CacheJndiTmFactory) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) IgniteException(org.apache.ignite.IgniteException) InvalidTransactionException(javax.transaction.InvalidTransactionException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) HeuristicMixedException(javax.transaction.HeuristicMixedException)

Aggregations

TransactionManager (javax.transaction.TransactionManager)110 Test (org.junit.Test)40 Transaction (javax.transaction.Transaction)24 SystemException (javax.transaction.SystemException)22 TransactionSynchronizationRegistry (javax.transaction.TransactionSynchronizationRegistry)15 UserTransaction (javax.transaction.UserTransaction)14 JtaTransactionCoordinatorImpl (org.hibernate.resource.transaction.backend.jta.internal.JtaTransactionCoordinatorImpl)12 JtaTransactionManager (org.springframework.transaction.jta.JtaTransactionManager)11 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)9 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)9 Method (java.lang.reflect.Method)7 EntityManager (javax.persistence.EntityManager)7 NotSupportedException (javax.transaction.NotSupportedException)7 RollbackException (javax.transaction.RollbackException)7 SynchronizationCollectorImpl (org.hibernate.test.resource.common.SynchronizationCollectorImpl)6 TestForIssue (org.hibernate.testing.TestForIssue)6 IOException (java.io.IOException)5 InitialContext (javax.naming.InitialContext)5 DataSource (javax.sql.DataSource)5 JtaPlatform (org.hibernate.engine.transaction.jta.platform.spi.JtaPlatform)5