Search in sources :

Example 96 with Transaction

use of javax.transaction.Transaction in project graphdb by neo4j-attic.

the class TestJtaCompliance method setUpFramework.

@Before
public void setUpFramework() {
    getTransaction().finish();
    TxModule txModule = getEmbeddedGraphDb().getConfig().getTxModule();
    tm = txModule.getTxManager();
    xaDsMgr = txModule.getXaDataSourceManager();
    java.util.Map<String, Object> map1 = new java.util.HashMap<String, Object>();
    map1.put("xa_resource", new FakeXAResource("XAResource1"));
    map1.put("store_dir", "target/var");
    java.util.Map<String, Object> map2 = new java.util.HashMap<String, Object>();
    map2.put("xa_resource", new FakeXAResource("XAResource2"));
    map2.put("store_dir", "target/var");
    try {
        xaDsMgr.registerDataSource("fakeRes1", new DummyXaDataSource(map1), UTF8.encode("0xDDDDDE"));
        xaDsMgr.registerDataSource("fakeRes2", new DummyXaDataSource(map2), UTF8.encode("0xDDDDDF"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        // make sure were not in transaction
        tm.commit();
    } catch (Exception e) {
    }
    Transaction tx = null;
    try {
        tx = tm.getTransaction();
    } catch (Exception e) {
        throw new RuntimeException("Unknown state of TM");
    }
    if (tx != null) {
        throw new RuntimeException("We're still in transaction");
    }
}
Also used : Transaction(javax.transaction.Transaction) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) Before(org.junit.Before)

Example 97 with Transaction

use of javax.transaction.Transaction in project graphdb by neo4j-attic.

the class TestTxSuspendResume method testMultipleTxSameThread.

@Test
public void testMultipleTxSameThread() throws Exception {
    String storePath = getStorePath("test-neo2");
    deleteFileOrDirectory(storePath);
    EmbeddedGraphDatabase neo2 = new EmbeddedGraphDatabase(storePath);
    TransactionManager tm = neo2.getConfig().getTxModule().getTxManager();
    tm.begin();
    Node refNode = neo2.getReferenceNode();
    Transaction tx1 = tm.suspend();
    tm.begin();
    refNode.setProperty("test2", "test");
    Transaction tx2 = tm.suspend();
    tm.resume(tx1);
    CommitThread thread = new CommitThread(tm, tx2);
    thread.start();
    // would wait for ever since tx2 has write lock but now we have other
    // thread thread that will commit tx2
    refNode.removeProperty("test2");
    assertTrue(thread.success());
    tm.commit();
    neo2.shutdown();
}
Also used : EmbeddedGraphDatabase(org.neo4j.kernel.EmbeddedGraphDatabase) Transaction(javax.transaction.Transaction) TransactionManager(javax.transaction.TransactionManager) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 98 with Transaction

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

the class RWLock method acquireWriteLock.

/**
     * Tries to acquire write lock for current transaction. If 
     * <CODE>this.writeCount</CODE> is greater than the currents tx's write 
     * count or the read count is greater than the currents tx's read count the 
     * transaction has to wait and the {@link RagManager#checkWaitOn} method is 
     * invoked for deadlock detection.
     * <p>
     * If the lock can be acquires the lock count is updated on <CODE>this</CODE>
     * and the transaction lock element (tle).
     * 
     * @throws DeadlockDetectedException
     *             if a deadlock is detected
     */
synchronized void acquireWriteLock() throws DeadlockDetectedException {
    Transaction tx = ragManager.getCurrentTransaction();
    if (tx == null) {
        tx = new PlaceboTransaction();
    }
    TxLockElement tle = txLockElementMap.get(tx);
    if (tle == null) {
        tle = new TxLockElement(tx);
    }
    try {
        tle.movedOn = false;
        while (writeCount > tle.writeCount || readCount > tle.readCount) {
            ragManager.checkWaitOn(this, tx);
            waitingThreadList.addFirst(new WaitElement(tle, LockType.WRITE, Thread.currentThread()));
            try {
                wait();
            } catch (InterruptedException e) {
                Thread.interrupted();
            }
            ragManager.stopWaitOn(this, tx);
        }
        if (tle.readCount == 0 && tle.writeCount == 0) {
            ragManager.lockAcquired(this, tx);
        }
        writeCount++;
        tle.writeCount++;
        tle.movedOn = true;
        // TODO optimize this put?
        txLockElementMap.put(tx, tle);
    } finally {
        // if deadlocked, remove marking so lock is removed when empty
        marked--;
    }
}
Also used : Transaction(javax.transaction.Transaction)

Example 99 with Transaction

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

the class RWLock method acquireReadLock.

/**
     * Tries to acquire read lock for current transaction. If 
     * <CODE>this.writeCount</CODE> is greater than the currents tx's write 
     * count the transaction has to wait and the {@link RagManager#checkWaitOn} 
     * method is invoked for deadlock detection.
     * <p>
     * If the lock can be acquires the lock count is updated on <CODE>this</CODE>
     * and the transaction lock element (tle).
     * 
     * @throws DeadlockDetectedException
     *             if a deadlock is detected
     */
synchronized void acquireReadLock() throws DeadlockDetectedException {
    Transaction tx = ragManager.getCurrentTransaction();
    if (tx == null) {
        tx = new PlaceboTransaction();
    }
    TxLockElement tle = txLockElementMap.get(tx);
    if (tle == null) {
        tle = new TxLockElement(tx);
    }
    try {
        tle.movedOn = false;
        while (writeCount > tle.writeCount) {
            ragManager.checkWaitOn(this, tx);
            waitingThreadList.addFirst(new WaitElement(tle, LockType.READ, Thread.currentThread()));
            try {
                wait();
            } catch (InterruptedException e) {
                Thread.interrupted();
            }
            ragManager.stopWaitOn(this, tx);
        }
        if (tle.readCount == 0 && tle.writeCount == 0) {
            ragManager.lockAcquired(this, tx);
        }
        readCount++;
        tle.readCount++;
        tle.movedOn = true;
        // TODO: this put could be optimized?
        txLockElementMap.put(tx, tle);
    } finally {
        // if deadlocked, remove marking so lock is removed when empty
        marked--;
    }
}
Also used : Transaction(javax.transaction.Transaction)

Example 100 with Transaction

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

the class RagManager method checkWaitOn.

// after invoke the transaction must wait on the resource
synchronized void checkWaitOn(Object resource, Transaction tx) throws DeadlockDetectedException {
    List<Transaction> lockingTxList = resourceMap.get(resource);
    if (lockingTxList == null) {
        throw new LockException("Illegal resource[" + resource + "], not found in map");
    }
    if (waitingTxMap.get(tx) != null) {
        throw new LockException(tx + " already waiting for resource");
    }
    Iterator<Transaction> itr = lockingTxList.iterator();
    List<Transaction> checkedTransactions = new LinkedList<Transaction>();
    Stack<Object> graphStack = new Stack<Object>();
    // has resource,transaction interleaved
    graphStack.push(resource);
    while (itr.hasNext()) {
        Transaction lockingTx = itr.next();
        // ... KISS to you too
        if (lockingTx.equals(tx)) {
            continue;
        }
        graphStack.push(tx);
        checkWaitOnRecursive(lockingTx, tx, checkedTransactions, graphStack);
        graphStack.pop();
    }
    // ok no deadlock, we can wait on resource
    waitingTxMap.put(tx, resource);
}
Also used : Transaction(javax.transaction.Transaction) LinkedList(java.util.LinkedList) Stack(java.util.Stack)

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