Search in sources :

Example 46 with Xid

use of javax.transaction.xa.Xid in project hazelcast by hazelcast.

the class HazelcastXATest method startTX.

private void startTX(final HazelcastInstance instance, final CountDownLatch nodeShutdownLatch) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                HazelcastXAResource xaResource = instance.getXAResource();
                Xid xid = new SerializableXID(42, "globalTransactionId".getBytes(), "branchQualifier".getBytes());
                xaResource.start(xid, XAResource.TMNOFLAGS);
                TransactionContext context = xaResource.getTransactionContext();
                final TransactionalMap<Object, Object> map = context.getMap("map");
                map.put("key", "value");
                xaResource.prepare(xid);
                instance.shutdown();
                nodeShutdownLatch.countDown();
            } catch (XAException e) {
                e.printStackTrace();
            }
        }
    }).start();
}
Also used : Xid(javax.transaction.xa.Xid) SerializableXID(com.hazelcast.transaction.impl.xa.SerializableXID) XAException(javax.transaction.xa.XAException) TransactionContext(com.hazelcast.transaction.TransactionContext) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource)

Example 47 with Xid

use of javax.transaction.xa.Xid in project hazelcast by hazelcast.

the class HazelcastXATest method testRecovery.

@Test
public void testRecovery() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(4);
    HazelcastInstance instance1 = factory.newHazelcastInstance();
    HazelcastInstance instance2 = factory.newHazelcastInstance();
    HazelcastInstance instance3 = factory.newHazelcastInstance();
    HazelcastXAResource xaResource = instance1.getXAResource();
    Xid myXid = new SerializableXID(42, "globalTransactionId".getBytes(), "branchQualifier".getBytes());
    xaResource.start(myXid, 0);
    TransactionContext context1 = xaResource.getTransactionContext();
    TransactionalMap<Object, Object> map = context1.getMap("map");
    map.put("key", "value");
    xaResource.prepare(myXid);
    instance1.shutdown();
    instance1 = factory.newHazelcastInstance();
    xaResource = instance1.getXAResource();
    Xid[] recovered = xaResource.recover(0);
    for (Xid xid : recovered) {
        xaResource.commit(xid, false);
    }
    assertEquals("value", instance2.getMap("map").get("key"));
}
Also used : Xid(javax.transaction.xa.Xid) SerializableXID(com.hazelcast.transaction.impl.xa.SerializableXID) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionContext(com.hazelcast.transaction.TransactionContext) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 48 with Xid

use of javax.transaction.xa.Xid in project hazelcast by hazelcast.

the class HazelcastXATest method recoverTX.

private void recoverTX(HazelcastInstance instance) throws XAException {
    HazelcastXAResource xaResource = instance.getXAResource();
    final Xid[] recovered = xaResource.recover(XAResource.TMNOFLAGS);
    for (Xid xid : recovered) {
        xaResource.commit(xid, false);
    }
}
Also used : Xid(javax.transaction.xa.Xid) HazelcastXAResource(com.hazelcast.transaction.HazelcastXAResource)

Example 49 with Xid

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

the class XaLogicalLog method applyOnePhaseCommitEntry.

private void applyOnePhaseCommitEntry(LogEntry.OnePhaseCommit commit) throws IOException {
    int identifier = commit.getIdentifier();
    long txId = commit.getTxId();
    LogEntry.Start startEntry = xidIdentMap.get(identifier);
    if (startEntry == null) {
        throw new IOException("Unknown xid for identifier " + identifier);
    }
    Xid xid = startEntry.getXid();
    try {
        XaTransaction xaTx = xaRm.getXaTransaction(xid);
        xaTx.setCommitTxId(txId);
        xaRm.injectOnePhaseCommit(xid);
        msgLog.logMessage("Injected one phase commit, txId=" + commit.getTxId(), true);
    } catch (XAException e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
}
Also used : Xid(javax.transaction.xa.Xid) XAException(javax.transaction.xa.XAException) IOException(java.io.IOException)

Example 50 with Xid

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

the class XaLogicalLog method applyTransactionWithoutTxId.

public synchronized void applyTransactionWithoutTxId(ReadableByteChannel byteChannel, long nextTxId, int masterId) throws IOException {
    if (nextTxId != (xaTf.getLastCommittedTx() + 1)) {
        throw new IllegalStateException("Tried to apply tx " + nextTxId + " but expected transaction " + (xaTf.getCurrentVersion() + 1));
    }
    msgLog.logMessage("applyTxWithoutTxId log version: " + logVersion + ", committing tx=" + nextTxId + ") @ pos " + writeBuffer.getFileChannelPosition(), true);
    long logEntriesFound = 0;
    scanIsComplete = false;
    LogApplier logApplier = new LogApplier(byteChannel);
    int xidIdent = getNextIdentifier();
    long startEntryPosition = writeBuffer.getFileChannelPosition();
    while (logApplier.readAndWriteAndApplyEntry(xidIdent)) {
        logEntriesFound++;
    }
    byteChannel.close();
    LogEntry.Start startEntry = logApplier.startEntry;
    if (startEntry == null) {
        throw new IOException("Unable to find start entry");
    }
    startEntry.setStartPosition(startEntryPosition);
    //        System.out.println( "applyTxWithoutTxId#before 1PC @ pos: " + writeBuffer.getFileChannelPosition() );
    LogEntry.OnePhaseCommit commit = new LogEntry.OnePhaseCommit(xidIdent, nextTxId, masterId);
    LogIoUtils.writeLogEntry(commit, writeBuffer);
    Xid xid = startEntry.getXid();
    try {
        XaTransaction xaTx = xaRm.getXaTransaction(xid);
        xaTx.setCommitTxId(nextTxId);
        xaRm.commit(xid, true);
        LogEntry doneEntry = new LogEntry.Done(startEntry.getIdentifier());
        LogIoUtils.writeLogEntry(doneEntry, writeBuffer);
        xidIdentMap.remove(startEntry.getIdentifier());
        recoveredTxMap.remove(startEntry.getIdentifier());
        cacheTxStartPosition(nextTxId, masterId, startEntry);
    } catch (XAException e) {
        e.printStackTrace();
        throw new IOException(e.getMessage());
    }
    //        LogEntry.Done done = new LogEntry.Done( entry.getIdentifier() );
    //        LogIoUtils.writeLogEntry( done, writeBuffer );
    // xaTf.setLastCommittedTx( nextTxId ); // done in doCommit
    scanIsComplete = true;
    //        log.info( "Tx[" + nextTxId + "] " + " applied successfully." );
    msgLog.logMessage("Applied external tx and generated tx id=" + nextTxId, true);
//        System.out.println( "applyTxWithoutTxId#end @ pos: " + writeBuffer.getFileChannelPosition() );
}
Also used : XAException(javax.transaction.xa.XAException) IOException(java.io.IOException) Xid(javax.transaction.xa.Xid)

Aggregations

Xid (javax.transaction.xa.Xid)82 Test (org.junit.Test)35 XAException (javax.transaction.xa.XAException)20 IOException (java.io.IOException)16 XAResource (javax.transaction.xa.XAResource)14 UnitTest (nl.topicus.jdbc.test.category.UnitTest)11 XidImpl (org.neo4j.kernel.impl.transaction.XidImpl)11 LinkedList (java.util.LinkedList)10 InOrder (org.mockito.InOrder)6 HashMap (java.util.HashMap)5 RecoveredXid (nl.topicus.jdbc.xa.RecoveredXid)5 RelationshipType (org.neo4j.graphdb.RelationshipType)5 HazelcastXAResource (com.hazelcast.transaction.HazelcastXAResource)4 ArrayList (java.util.ArrayList)4 RollbackException (javax.transaction.RollbackException)4 SystemException (javax.transaction.SystemException)4 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)4 XaResource (org.neo4j.kernel.impl.transaction.xaframework.XaResource)4 TransactionContext (com.hazelcast.transaction.TransactionContext)3 SerializableXID (com.hazelcast.transaction.impl.xa.SerializableXID)3