Search in sources :

Example 21 with Transaction

use of javax.transaction.Transaction in project neo4j-mobile-android by neo4j-contrib.

the class TxManager method attemptWaitForTxCompletionAndBlockFutureTransactions.

@Override
public void attemptWaitForTxCompletionAndBlockFutureTransactions(long maxWaitTimeMillis) {
    msgLog.logMessage("TxManager is blocking new transactions and waiting for active to fail...");
    blocked = true;
    List<Transaction> failedTransactions = new ArrayList<Transaction>();
    synchronized (txThreadMap) {
        for (Transaction tx : txThreadMap.values()) {
            try {
                int status = tx.getStatus();
                if (status != Status.STATUS_COMMITTING && status != Status.STATUS_ROLLING_BACK) {
                    // Set it to rollback only if it's not committing or rolling back
                    tx.setRollbackOnly();
                }
            } catch (IllegalStateException e) {
                // OK
                failedTransactions.add(tx);
            } catch (SystemException e) {
                // OK
                failedTransactions.add(tx);
            }
        }
    }
    msgLog.logMessage("TxManager blocked transactions" + ((failedTransactions.isEmpty() ? "" : ", but failed for: " + failedTransactions.toString())));
    long endTime = System.currentTimeMillis() + maxWaitTimeMillis;
    while (txThreadMap.size() > 0 && System.currentTimeMillis() < endTime) Thread.yield();
}
Also used : Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) ArrayList(java.util.ArrayList)

Example 22 with Transaction

use of javax.transaction.Transaction in project neo4j-mobile-android by neo4j-contrib.

the class PersistenceManager method getResource.

private NeoStoreTransaction getResource(boolean registerEventHooks) {
    NeoStoreTransaction con = null;
    Transaction tx = this.getCurrentTransaction();
    if (tx == null) {
        throw new NotInTransactionException();
    }
    con = txConnectionMap.get(tx);
    if (con == null) {
        try {
            XaConnection xaConnection = persistenceSource.getXaDataSource().getXaConnection();
            XAResource xaResource = xaConnection.getXaResource();
            if (!tx.enlistResource(xaResource)) {
                throw new ResourceAcquisitionFailedException("Unable to enlist '" + xaResource + "' in " + "transaction");
            }
            con = persistenceSource.createTransaction(xaConnection);
            tx.registerSynchronization(new TxCommitHook(tx));
            if (registerEventHooks)
                registerTransactionEventHookIfNeeded();
            txConnectionMap.put(tx, con);
        } catch (javax.transaction.RollbackException re) {
            String msg = "The transaction is marked for rollback only.";
            throw new ResourceAcquisitionFailedException(msg, re);
        } catch (javax.transaction.SystemException se) {
            String msg = "TM encountered an unexpected error condition.";
            throw new ResourceAcquisitionFailedException(msg, se);
        }
    }
    return con;
}
Also used : XAResource(javax.transaction.xa.XAResource) Transaction(javax.transaction.Transaction) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException) SystemException(javax.transaction.SystemException) RollbackException(javax.transaction.RollbackException) XaConnection(org.neo4j.kernel.impl.transaction.xaframework.XaConnection)

Example 23 with Transaction

use of javax.transaction.Transaction in project neo4j-mobile-android by neo4j-contrib.

the class PersistenceManager method delistResourcesForTransaction.

void delistResourcesForTransaction() throws NotInTransactionException {
    Transaction tx = this.getCurrentTransaction();
    if (tx == null) {
        throw new NotInTransactionException();
    }
    NeoStoreTransaction con = txConnectionMap.get(tx);
    if (con != null) {
        try {
            tx.delistResource(con.getXAResource(), XAResource.TMSUCCESS);
        } catch (SystemException e) {
            throw new TransactionFailureException("Failed to delist resource '" + con + "' from current transaction.", e);
        }
    }
}
Also used : TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) Transaction(javax.transaction.Transaction) SystemException(javax.transaction.SystemException) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException)

Example 24 with Transaction

use of javax.transaction.Transaction in project neo4j-mobile-android by neo4j-contrib.

the class PropertyIndexManager method createPropertyIndex.

// concurent transactions may create duplicate keys, oh well
PropertyIndex createPropertyIndex(String key) {
    Transaction tx = getTransaction();
    if (tx == null) {
        throw new NotInTransactionException("Unable to create property index for " + key);
    }
    TxCommitHook hook = txCommitHooks.get(tx);
    if (hook == null) {
        hook = new TxCommitHook();
        txCommitHooks.put(tx, hook);
    }
    PropertyIndex index = hook.getIndex(key);
    if (index != null) {
        return index;
    }
    int id = (int) idGenerator.nextId(PropertyIndex.class);
    index = new PropertyIndex(key, id);
    hook.addIndex(index);
    persistenceManager.createPropertyIndex(key, id);
    return index;
}
Also used : Transaction(javax.transaction.Transaction) NotInTransactionException(org.neo4j.graphdb.NotInTransactionException)

Example 25 with Transaction

use of javax.transaction.Transaction in project spring-framework by spring-projects.

the class JtaTransactionManagerTests method jtaTransactionManagerWithPropagationRequiresNewAndExisting.

@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);
}
Also used : UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) Transaction(javax.transaction.Transaction) MockJtaTransaction(org.springframework.tests.transaction.MockJtaTransaction) UserTransaction(javax.transaction.UserTransaction) JtaTransactionManager(org.springframework.transaction.jta.JtaTransactionManager) TransactionManager(javax.transaction.TransactionManager) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) TransactionCallbackWithoutResult(org.springframework.transaction.support.TransactionCallbackWithoutResult) Test(org.junit.Test)

Aggregations

Transaction (javax.transaction.Transaction)160 SystemException (javax.transaction.SystemException)55 Test (org.junit.Test)42 RollbackException (javax.transaction.RollbackException)26 TransactionManager (javax.transaction.TransactionManager)24 UserTransaction (javax.transaction.UserTransaction)19 NotInTransactionException (org.neo4j.graphdb.NotInTransactionException)14 NotSupportedException (javax.transaction.NotSupportedException)13 Synchronization (javax.transaction.Synchronization)10 XAResource (javax.transaction.xa.XAResource)10 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)10 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)8 InvalidTransactionException (javax.transaction.InvalidTransactionException)7 TransactionContext (com.hazelcast.transaction.TransactionContext)6 RemoteException (java.rmi.RemoteException)6 ResourceException (javax.resource.ResourceException)6 ManagedConnection (javax.resource.spi.ManagedConnection)6 SQLException (java.sql.SQLException)5 HeuristicMixedException (javax.transaction.HeuristicMixedException)5 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)5