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