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