Search in sources :

Example 41 with RollbackException

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

the class LocalRegion method getJTAEnlistedTX.

private TXStateInterface getJTAEnlistedTX() {
    if (this.ignoreJTA) {
        // fixes bug 45541
        return null;
    }
    TXStateInterface txState = getTXState();
    if (txState != null) {
        return txState;
    } else {
        try {
            if (!this.ignoreJTA && this.cache.getJTATransactionManager() != null) {
                Transaction jtaTransaction = this.cache.getJTATransactionManager().getTransaction();
                if (jtaTransaction == null || jtaTransaction.getStatus() == Status.STATUS_NO_TRANSACTION) {
                    return null;
                }
                txState = this.cache.getTXMgr().beginJTA();
                jtaTransaction.registerSynchronization(txState);
                return txState;
            } else {
                return null;
            }
        } catch (SystemException se) {
            // this can be thrown when the system is shutting down (see bug #39728)
            this.stopper.checkCancelInProgress(se);
            jtaEnlistmentFailureCleanup(txState, se);
            return null;
        } catch (RollbackException | IllegalStateException re) {
            jtaEnlistmentFailureCleanup(txState, re);
            return null;
        }
    }
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException)

Example 42 with RollbackException

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

the class FacetsJCAConnectionManagerImpl method allocateConnection.

/*
   * allocates a ManagedConnection from the ConnectionPool or creates a new
   * ManagedConnection. @param javax.resource.spi.ManagedConnectionFactory
   * 
   * @param javax.resource.spi.ConnectionRequestInfo
   * 
   * @throws ResourceException
   */
public Object allocateConnection(ManagedConnectionFactory mcf, ConnectionRequestInfo reqInfo) throws ResourceException {
    if (!isActive) {
        throw new ResourceException(LocalizedStrings.FacetsJCAConnectionManagerImpl_FACETSJCACONNECTIONMANAGERIMPLALLOCATECONNECTIONNO_VALID_CONNECTION_AVAILABLE.toLocalizedString());
    }
    ManagedConnection conn = null;
    try {
        conn = (ManagedConnection) mannPoolCache.getPooledConnectionFromPool();
    } catch (PoolException ex) {
        ex.printStackTrace();
        throw new ResourceException(LocalizedStrings.FacetsJCAConnectionManagerImpl_FACETSJCACONNECTIONMANAGERIMPL_ALLOCATECONNECTION_IN_GETTING_CONNECTION_FROM_POOL_DUE_TO_0.toLocalizedString(ex.getMessage()), ex);
    }
    // Transaction Manager.
    try {
        synchronized (this) {
            if (transManager == null) {
                transManager = JNDIInvoker.getTransactionManager();
            }
        }
        Transaction txn = transManager.getTransaction();
        if (txn != null) {
            // Check if Data Source provides XATransaction
            // if(configs.getTransactionType = "XATransaction")
            XAResource xar = conn.getXAResource();
            txn.enlistResource(xar);
            java.util.List resList = (List) xalistThreadLocal.get();
            if (resList.size() == 0) {
                // facets specific implementation
                // register syschronisation only once
                txn.registerSynchronization(this);
            }
            resList.add(conn);
        // xalistThreadLocal.set(resList);
        // Asif :Add in the Map after successful registration of XAResource
        // xaResourcesMap.put(conn, xar);
        // else throw a resource exception
        }
    } catch (RollbackException ex) {
        String exception = LocalizedStrings.FacetsJCAConnectionManagerImpl_FACETSJCACONNECTIONMANAGERIMPL_AN_EXCEPTION_WAS_CAUGHT_WHILE_ALLOCATING_A_CONNECTION_DUE_TO_0.toLocalizedString(ex.getMessage());
        throw new ResourceException(exception, ex);
    } catch (SystemException ex) {
        throw new ResourceException(LocalizedStrings.FacetsJCAConnectionManagerImpl_FACETSJCACONNECTIONMANAGERIMPL_ALLOCATECONNECTION_SYSTEM_EXCEPTION_DUE_TO_0.toLocalizedString(ex.getMessage()), ex);
    }
    return conn.getConnection(subject, reqInfo);
}
Also used : XAResource(javax.transaction.xa.XAResource) List(java.util.List) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) ResourceException(javax.resource.ResourceException) ManagedConnection(javax.resource.spi.ManagedConnection) ArrayList(java.util.ArrayList) List(java.util.List) RollbackException(javax.transaction.RollbackException)

Example 43 with RollbackException

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

the class QueryAndJtaJUnitTest method testScenario1.

@Test
public void testScenario1() throws Exception {
    Context ctx = cache.getJNDIContext();
    UserTransaction ta = null;
    // Connection conn = null;
    try {
        qs.createIndex("iIndex", IndexType.FUNCTIONAL, "ID", "/portfolios");
        // PUT 4 objects in region, QUERY, CREATEINDEX
        for (int i = 0; i < 4; i++) {
            currRegion.put("key" + i, new Portfolio(i));
        }
        QueryObserverImpl observer2 = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer2);
        q = qs.newQuery("select distinct * from /portfolios where ID != 53");
        Object r = q.execute();
        if (!observer2.isIndexesUsed) {
            fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
        }
        if (((Collection) r).size() != 4) {
            fail("Query result not of expected size");
        }
        // print("Size of query result :"+ ((Collection)r).size());
        // print("Result of query =" + Utils.printResult(r));
        // print("Index IS: " + ((RangeIndex)i2).dump());
        // BEGIN TX PUT new 4 objects in region, QUERY,CREATEINDEX, ROLLBACK
        ta = (UserTransaction) ctx.lookup("java:/UserTransaction");
        ta.begin();
        for (int i = 9; i < 13; i++) {
            currRegion.put("key" + i, new Portfolio(i));
        }
        observer2 = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer2);
        q = qs.newQuery("select distinct * from /portfolios where ID != 53");
        r = q.execute();
        if (!observer2.isIndexesUsed) {
            fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
        }
        if (((Collection) r).size() != 4) {
            fail("Query result not of expected size");
        }
        // print("Size of query result :"+ ((Collection)r).size());
        // print("Result of query =" + Utils.printResult(r));
        Index i1 = qs.createIndex("tIndex", IndexType.FUNCTIONAL, "status", "/portfolios");
        // print("Index IS: " + ((RangeIndex)i1).dump());
        observer2 = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer2);
        q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
        r = q.execute();
        if (!observer2.isIndexesUsed) {
            fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
        }
        if (((Collection) r).size() != 2) {
            fail("Query result not of expected size");
        }
        observer2 = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer2);
        q = qs.newQuery("select distinct * from /portfolios where status = 'inactive'");
        r = q.execute();
        if (!observer2.isIndexesUsed) {
            fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
        }
        if (((Collection) r).size() != 2) {
            fail("Query result not of expected size");
        }
        ta.rollback();
        // PRINT INDEX AFTER ROLLBACK, REMOVEINDEX.
        observer2 = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer2);
        q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
        r = q.execute();
        if (!observer2.isIndexesUsed) {
            fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
        }
        if (((Collection) r).size() != 2) {
            fail("Query result not of expected size");
        }
        // print("AfterRollback \n"+currRegion.values());
        // print("Index IS: " + ((RangeIndex)i1).dump());
        qs.removeIndex(i1);
        // BEGIN TX PUT new 4 objects in region,CREATEINDEX, QUERY ,COMMIT
        ta.begin();
        for (int i = 9; i < 13; i++) {
            currRegion.put("key" + i, new Portfolio(i));
        }
        i1 = qs.createIndex("tIndex", IndexType.FUNCTIONAL, "status", "/portfolios");
        // print("Index IS: " + ((RangeIndex)i1).dump());
        observer2 = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer2);
        q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
        r = q.execute();
        if (!observer2.isIndexesUsed) {
            fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
        }
        if (((Collection) r).size() != 2) {
            fail("Query result not of expected size");
        }
        for (int i = 9; i < 13; i++) {
            currRegion.put("key" + i, new Portfolio(i));
        }
        observer2 = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer2);
        q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
        r = q.execute();
        if (!observer2.isIndexesUsed) {
            fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
        }
        if (((Collection) r).size() != 2) {
            fail("Query result not of expected size");
        }
        // print("Size of query result :"+ ((Collection)r).size());
        // print("Result of query =" + Utils.printResult(r));
        // print("Index on status IS: " + ((RangeIndex)i1).dump());
        // print("Index On ID IS: " + ((RangeIndex)i2).dump());
        ta.commit();
        // WAIT FOR 2 secs DISPLAYINDEX, QUERY
        Thread.sleep(2000);
        // print("Aftercommit \n"+currRegion.values());
        // print("Index IS: " + ((RangeIndex)i1).dump());
        observer2 = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer2);
        q = qs.newQuery("select distinct * from /portfolios where status = 'active'");
        r = q.execute();
        if (!observer2.isIndexesUsed) {
            fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
        }
        if (((Collection) r).size() != 4) {
            fail("Query result not of expected size");
        }
        observer2 = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer2);
        q = qs.newQuery("select distinct * from /portfolios where status = 'inactive'");
        r = q.execute();
        if (!observer2.isIndexesUsed) {
            fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
        }
        if (((Collection) r).size() != 4) {
            fail("Query result not of expected size");
        }
        observer2 = new QueryObserverImpl();
        QueryObserverHolder.setInstance(observer2);
        q = qs.newQuery("select distinct * from /portfolios where ID != 53");
        r = q.execute();
        if (!observer2.isIndexesUsed) {
            fail("NO INDEX WAS USED, IT WAS EXPECTED TO BE USED");
        }
        if (((Collection) r).size() != 8) {
            fail("Query result not of expected size");
        }
    // print("Size of query result :"+ ((Collection)r).size());
    // print("Result of query =" + Utils.printResult(r));
    // print("Index On ID IS: " + ((RangeIndex)i2).dump());
    } catch (RollbackException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
        ta.rollback();
    }
}
Also used : Context(javax.naming.Context) UserTransaction(javax.transaction.UserTransaction) Portfolio(org.apache.geode.cache.query.data.Portfolio) Index(org.apache.geode.cache.query.Index) RollbackException(javax.transaction.RollbackException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 44 with RollbackException

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

the class GlobalTransactionJUnitTest method testRegisterSynchronization.

@Test
public void testRegisterSynchronization() throws Exception {
    try {
        boolean exceptionoccurred = false;
        utx.begin();
        try {
            Transaction txn = tm.getTransaction();
            Synchronization sync = new SyncImpl();
            txn.registerSynchronization(sync);
        } catch (RollbackException e) {
            exceptionoccurred = true;
        }
        if (exceptionoccurred)
            fail("exception occurred while trying to register synchronization ");
        utx.rollback();
    } catch (Exception e) {
        try {
            utx.rollback();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        fail("exception in testRegisterSynchronization due to " + e);
        e.printStackTrace();
    }
}
Also used : Transaction(javax.transaction.Transaction) UserTransaction(javax.transaction.UserTransaction) Synchronization(javax.transaction.Synchronization) RollbackException(javax.transaction.RollbackException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 45 with RollbackException

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

the class GlobalTransactionJUnitTest method testRegisterSynchronizationAfterRollBack.

@Test
public void testRegisterSynchronizationAfterRollBack() throws Exception {
    try {
        boolean exceptionoccurred = false;
        utx.begin();
        utx.setRollbackOnly();
        Context ctx = cache.getJNDIContext();
        try {
            Transaction txn = tm.getTransaction();
            Synchronization sync = new SyncImpl();
            txn.registerSynchronization(sync);
        } catch (RollbackException e) {
            exceptionoccurred = true;
        }
        if (!exceptionoccurred)
            fail("RollbackException not occurred although the transaction was marked for rollback");
        utx.rollback();
    } catch (Exception e) {
        try {
            utx.rollback();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        fail("exception in testSetRollbackonly due to " + e);
        e.printStackTrace();
    }
}
Also used : Context(javax.naming.Context) Transaction(javax.transaction.Transaction) UserTransaction(javax.transaction.UserTransaction) Synchronization(javax.transaction.Synchronization) RollbackException(javax.transaction.RollbackException) SQLException(java.sql.SQLException) RollbackException(javax.transaction.RollbackException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

RollbackException (javax.transaction.RollbackException)57 SystemException (javax.transaction.SystemException)25 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)15 Transaction (javax.transaction.Transaction)14 UserTransaction (javax.transaction.UserTransaction)14 HeuristicMixedException (javax.transaction.HeuristicMixedException)12 Test (org.junit.Test)11 Connection (java.sql.Connection)8 XAException (javax.transaction.xa.XAException)8 PreparedStatement (java.sql.PreparedStatement)7 NotSupportedException (javax.transaction.NotSupportedException)7 IOException (java.io.IOException)6 EJBException (javax.ejb.EJBException)6 Account (org.apache.openejb.test.object.Account)6 RemoteException (java.rmi.RemoteException)5 CreateException (javax.ejb.CreateException)5 DataSource (javax.sql.DataSource)5 SQLException (java.sql.SQLException)4 Synchronization (javax.transaction.Synchronization)4 XAResource (javax.transaction.xa.XAResource)4