use of javax.transaction.xa.XAException in project graphdb by neo4j-attic.
the class XaLogicalLog method commitOnePhase.
// [TX_1P_COMMIT][identifier]
public synchronized void commitOnePhase(int identifier, long txId, int masterId) throws XAException {
LogEntry.Start startEntry = xidIdentMap.get(identifier);
assert startEntry != null;
assert txId != -1;
try {
LogIoUtils.writeCommit(false, writeBuffer, identifier, txId, masterId);
writeBuffer.force();
cacheTxStartPosition(txId, masterId, startEntry);
} catch (IOException e) {
throw new XAException("Logical log unable to mark 1P-commit [" + identifier + "] " + e);
}
}
use of javax.transaction.xa.XAException in project graphdb by neo4j-attic.
the class XaResourceManager method fail.
synchronized void fail(XAResource xaResource, Xid xid) throws XAException {
if (xidMap.get(xid) == null) {
throw new XAException("Unknown xid[" + xid + "]");
}
Xid xidEntry = xaResourceMap.remove(xaResource);
if (xidEntry == null) {
throw new XAException("Resource[" + xaResource + "] not enlisted");
}
XidStatus status = xidMap.get(xid);
status.getTransactionStatus().markAsRollback();
}
use of javax.transaction.xa.XAException in project neo4j-mobile-android by neo4j-contrib.
the class ReadOnlyTransactionImpl method enlistResource.
public synchronized boolean enlistResource(XAResource xaRes) throws RollbackException, IllegalStateException {
if (xaRes == null) {
throw new IllegalArgumentException("Null xa resource");
}
if (status == Status.STATUS_ACTIVE || status == Status.STATUS_PREPARING) {
try {
if (resourceList.size() == 0) {
//
byte[] branchId = txManager.getBranchId(xaRes);
Xid xid = new XidImpl(globalId, branchId);
resourceList.add(new ResourceElement(xid, xaRes));
xaRes.start(xid, XAResource.TMNOFLAGS);
return true;
}
Xid sameRmXid = null;
Iterator<ResourceElement> itr = resourceList.iterator();
while (itr.hasNext()) {
ResourceElement re = itr.next();
if (sameRmXid == null && re.getResource().isSameRM(xaRes)) {
sameRmXid = re.getXid();
}
if (xaRes == re.getResource()) {
if (re.getStatus() == RS_SUSPENDED) {
xaRes.start(re.getXid(), XAResource.TMRESUME);
} else {
// either enlisted or delisted
// is TMJOIN correct then?
xaRes.start(re.getXid(), XAResource.TMJOIN);
}
re.setStatus(RS_ENLISTED);
return true;
}
}
if (// should we join?
sameRmXid != null) {
resourceList.add(new ResourceElement(sameRmXid, xaRes));
xaRes.start(sameRmXid, XAResource.TMJOIN);
} else // new branch
{
// ResourceElement re = resourceList.getFirst();
byte[] branchId = txManager.getBranchId(xaRes);
Xid xid = new XidImpl(globalId, branchId);
resourceList.add(new ResourceElement(xid, xaRes));
xaRes.start(xid, XAResource.TMNOFLAGS);
}
return true;
} catch (XAException e) {
log.log(Level.SEVERE, "Unable to enlist resource[" + xaRes + "]", e);
status = Status.STATUS_MARKED_ROLLBACK;
return false;
}
} else if (status == Status.STATUS_ROLLING_BACK || status == Status.STATUS_ROLLEDBACK || status == Status.STATUS_MARKED_ROLLBACK) {
throw new RollbackException("Tx status is: " + txManager.getTxStatusAsString(status));
}
throw new IllegalStateException("Tx status is: " + txManager.getTxStatusAsString(status));
}
use of javax.transaction.xa.XAException in project neo4j-mobile-android by neo4j-contrib.
the class WriteTransaction method doRollback.
@Override
public void doRollback() throws XAException {
if (committed) {
throw new XAException("Cannot rollback partialy commited " + "transaction[" + getIdentifier() + "]. Recover and " + "commit");
}
try {
for (RelationshipTypeRecord record : relTypeRecords.values()) {
if (record.isCreated()) {
getRelationshipTypeStore().freeId(record.getId());
for (DynamicRecord dynamicRecord : record.getTypeRecords()) {
if (dynamicRecord.isCreated()) {
getRelationshipTypeStore().freeBlockId((int) dynamicRecord.getId());
}
}
}
removeRelationshipTypeFromCache(record.getId());
}
for (NodeRecord record : nodeRecords.values()) {
if (record.isCreated()) {
getNodeStore().freeId(record.getId());
}
removeNodeFromCache(record.getId());
}
for (RelationshipRecord record : relRecords.values()) {
if (record.isCreated()) {
getRelationshipStore().freeId(record.getId());
}
removeRelationshipFromCache(record.getId());
}
for (PropertyIndexRecord record : propIndexRecords.values()) {
if (record.isCreated()) {
getPropertyStore().getIndexStore().freeId(record.getId());
for (DynamicRecord dynamicRecord : record.getKeyRecords()) {
if (dynamicRecord.isCreated()) {
getPropertyStore().getIndexStore().freeBlockId((int) dynamicRecord.getId());
}
}
}
}
for (PropertyRecord record : propertyRecords.values()) {
if (record.getNodeId() != -1) {
removeNodeFromCache(record.getNodeId());
} else if (record.getRelId() != -1) {
removeRelationshipFromCache(record.getRelId());
}
if (record.isCreated()) {
getPropertyStore().freeId(record.getId());
for (PropertyBlock block : record.getPropertyBlocks()) {
for (DynamicRecord dynamicRecord : block.getValueRecords()) {
if (dynamicRecord.isCreated()) {
if (dynamicRecord.getType() == PropertyType.STRING.intValue()) {
getPropertyStore().freeStringBlockId(dynamicRecord.getId());
} else if (dynamicRecord.getType() == PropertyType.ARRAY.intValue()) {
getPropertyStore().freeArrayBlockId(dynamicRecord.getId());
} else {
throw new InvalidRecordException("Unknown type on " + dynamicRecord);
}
}
}
}
}
}
} finally {
nodeRecords.clear();
propertyRecords.clear();
relRecords.clear();
relTypeRecords.clear();
propIndexRecords.clear();
nodeCommands.clear();
propCommands.clear();
propIndexCommands.clear();
relCommands.clear();
relTypeCommands.clear();
}
}
use of javax.transaction.xa.XAException 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