use of javax.transaction.xa.Xid in project neo4j-mobile-android by neo4j-contrib.
the class LogIoUtils method readTxStartEntry.
private static LogEntry.Start readTxStartEntry(ByteBuffer buf, ReadableByteChannel channel) throws IOException, ReadPastEndException {
byte globalIdLength = readNextByte(buf, channel);
byte branchIdLength = readNextByte(buf, channel);
byte[] globalId = new byte[globalIdLength];
readIntoBufferAndFlip(ByteBuffer.wrap(globalId), channel, globalIdLength);
byte[] branchId = new byte[branchIdLength];
readIntoBufferAndFlip(ByteBuffer.wrap(branchId), channel, branchIdLength);
int identifier = readNextInt(buf, channel);
int formatId = readNextInt(buf, channel);
// re-create the transaction
Xid xid = new XidImpl(globalId, branchId, formatId);
return new LogEntry.Start(xid, identifier, -1);
}
use of javax.transaction.xa.Xid in project neo4j-mobile-android by neo4j-contrib.
the class XaLogicalLog method applyDoneEntry.
private void applyDoneEntry(LogEntry.Done done) throws IOException {
// get the tx identifier
int identifier = done.getIdentifier();
LogEntry.Start entry = xidIdentMap.get(identifier);
if (entry == null) {
throw new IOException("Unknown xid for identifier " + identifier);
}
Xid xid = entry.getXid();
xaRm.pruneXid(xid);
xidIdentMap.remove(identifier);
recoveredTxMap.remove(identifier);
}
use of javax.transaction.xa.Xid in project neo4j-mobile-android by neo4j-contrib.
the class XaLogicalLog method applyStartEntry.
private void applyStartEntry(LogEntry.Start entry) throws IOException {
int identifier = entry.getIdentifier();
if (identifier >= nextIdentifier) {
nextIdentifier = (identifier + 1);
}
// re-create the transaction
Xid xid = entry.getXid();
xidIdentMap.put(identifier, entry);
XaTransaction xaTx = xaTf.create(identifier);
xaTx.setRecovered();
recoveredTxMap.put(identifier, xaTx);
xaRm.injectStart(xid, xaTx);
// force to make sure done record is there if 2PC tx and global log
// marks tx as committed
// fileChannel.force( false );
}
use of javax.transaction.xa.Xid in project neo4j-mobile-android by neo4j-contrib.
the class TransactionImpl method isOnePhase.
private boolean isOnePhase() {
if (resourceList.size() == 0) {
log.severe("Detected zero resources in resourceList");
return true;
}
// check for more than one unique xid
Iterator<ResourceElement> itr = resourceList.iterator();
Xid xid = itr.next().getXid();
while (itr.hasNext()) {
if (!xid.equals(itr.next().getXid())) {
return false;
}
}
return true;
}
use of javax.transaction.xa.Xid in project neo4j-mobile-android by neo4j-contrib.
the class TxManager method recover.
private void recover(Iterator<List<TxLog.Record>> danglingRecordList) {
msgLog.logMessage("TM non resolved transactions found in " + txLog.getName(), true);
try {
// contains NonCompletedTransaction that needs to be committed
List<NonCompletedTransaction> commitList = new ArrayList<NonCompletedTransaction>();
// contains Xids that should be rolledback
List<Xid> rollbackList = new LinkedList<Xid>();
// key = Resource(branchId) value = XAResource
Map<Resource, XAResource> resourceMap = new HashMap<Resource, XAResource>();
buildRecoveryInfo(commitList, rollbackList, resourceMap, danglingRecordList);
// invoke recover on all xa resources found
Iterator<Resource> resourceItr = resourceMap.keySet().iterator();
List<Xid> recoveredXidsList = new LinkedList<Xid>();
while (resourceItr.hasNext()) {
XAResource xaRes = resourceMap.get(resourceItr.next());
Xid[] xids = xaRes.recover(XAResource.TMNOFLAGS);
for (int i = 0; i < xids.length; i++) {
if (XidImpl.isThisTm(xids[i].getGlobalTransactionId())) {
// linear search
if (rollbackList.contains(xids[i])) {
log.fine("Found pre commit " + xids[i] + " rolling back ... ");
msgLog.logMessage("TM: Found pre commit " + xids[i] + " rolling back ... ", true);
rollbackList.remove(xids[i]);
xaRes.rollback(xids[i]);
} else {
recoveredXidsList.add(xids[i]);
}
} else {
log.warning("Unknown xid: " + xids[i]);
}
}
}
// sort the commit list after sequence number
Collections.sort(commitList, new Comparator<NonCompletedTransaction>() {
public int compare(NonCompletedTransaction r1, NonCompletedTransaction r2) {
return r1.getSequenceNumber() - r2.getSequenceNumber();
}
});
// go through and commit
Iterator<NonCompletedTransaction> commitItr = commitList.iterator();
while (commitItr.hasNext()) {
NonCompletedTransaction nct = commitItr.next();
int seq = nct.getSequenceNumber();
Xid[] xids = nct.getXids();
log.fine("Marked as commit tx-seq[" + seq + "] branch length: " + xids.length);
for (Xid xid : xids) {
if (!recoveredXidsList.contains(xid)) {
log.fine("Tx-seq[" + seq + "][" + xid + "] not found in recovered xid list, " + "assuming already committed");
continue;
}
recoveredXidsList.remove(xid);
Resource resource = new Resource(xid.getBranchQualifier());
if (!resourceMap.containsKey(resource)) {
final TransactionFailureException ex = new TransactionFailureException("Couldn't find XAResource for " + xid);
throw logAndReturn("TM: recovery error", ex);
}
log.fine("Commiting tx seq[" + seq + "][" + xid + "] ... ");
msgLog.logMessage("TM: Committing tx " + xid, true);
resourceMap.get(resource).commit(xid, false);
}
}
// rollback the rest
Iterator<Xid> rollbackItr = recoveredXidsList.iterator();
while (rollbackItr.hasNext()) {
Xid xid = rollbackItr.next();
Resource resource = new Resource(xid.getBranchQualifier());
if (!resourceMap.containsKey(resource)) {
final TransactionFailureException ex = new TransactionFailureException("Couldn't find XAResource for " + xid);
throw logAndReturn("TM: recovery error", ex);
}
log.fine("Rollback " + xid + " ... ");
msgLog.logMessage("TM: no match found for " + xid + " removing", true);
resourceMap.get(resource).rollback(xid);
}
if (rollbackList.size() > 0) {
log.fine("TxLog contained unresolved " + "xids that needed rollback. They couldn't be matched to " + "any of the XAResources recover list. " + "Assuming " + rollbackList.size() + " transactions already rolled back.");
msgLog.logMessage("TM: no match found for in total " + rollbackList.size() + " transaction that should have been rolled back", true);
}
// doesn't get lost.
for (XAResource participant : MapUtil.reverse(resourceMap).keySet()) {
xaResourceToDataSource(participant).rotateLogicalLog();
}
} catch (IOException e) {
throw logAndReturn("TM: recovery failed", new TransactionFailureException("Recovery failed.", e));
} catch (XAException e) {
throw logAndReturn("TM: recovery failed", new TransactionFailureException("Recovery failed.", e));
}
}
Aggregations