use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.
the class TestUpgradeStore method makeSureStoreCantBeUpgradedIfNotExplicitlyToldTo2.
@Test
public void makeSureStoreCantBeUpgradedIfNotExplicitlyToldTo2() throws Exception {
String path = path(12);
new EmbeddedGraphDatabase(path).shutdown();
setOlderNeoStoreVersion(path);
try {
new EmbeddedGraphDatabase(path, stringMap(ALLOW_STORE_UPGRADE, "false"));
fail("Shouldn't be able to upgrade if not told to");
} catch (TransactionFailureException e) {
if (!(e.getCause() instanceof IllegalStoreVersionException)) {
throw e;
}
}
}
use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.
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 (int i = 0; i < xids.length; i++) {
if (!recoveredXidsList.contains(xids[i])) {
log.fine("Tx-seq[" + seq + "][" + xids[i] + "] not found in recovered xid list, " + "assuming already committed");
continue;
}
recoveredXidsList.remove(xids[i]);
Resource resource = new Resource(xids[i].getBranchQualifier());
if (!resourceMap.containsKey(resource)) {
throw new TransactionFailureException("Couldn't find XAResource for " + xids[i]);
}
log.fine("Commiting tx seq[" + seq + "][" + xids[i] + "] ... ");
msgLog.logMessage("TM: Committing tx " + xids[i], true);
resourceMap.get(resource).commit(xids[i], 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)) {
throw new TransactionFailureException("Couldn't find XAResource for " + xid);
}
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);
}
} catch (XAException e) {
throw new TransactionFailureException("Recovery failed." + e);
}
}
use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.
the class TxManager method buildRecoveryInfo.
private void buildRecoveryInfo(List<NonCompletedTransaction> commitList, List<Xid> rollbackList, Map<Resource, XAResource> resourceMap, Iterator<List<TxLog.Record>> danglingRecordList) {
while (danglingRecordList.hasNext()) {
Iterator<TxLog.Record> dListItr = danglingRecordList.next().iterator();
TxLog.Record startRecord = dListItr.next();
if (startRecord.getType() != TxLog.TX_START) {
throw new TransactionFailureException("First record not a start record, type=" + startRecord.getType());
}
// get branches & commit status
HashSet<Resource> branchSet = new HashSet<Resource>();
int markedCommit = -1;
while (dListItr.hasNext()) {
TxLog.Record record = dListItr.next();
if (record.getType() == TxLog.BRANCH_ADD) {
if (markedCommit != -1) {
throw new TransactionFailureException("Already marked commit " + startRecord);
}
branchSet.add(new Resource(record.getBranchId()));
} else if (record.getType() == TxLog.MARK_COMMIT) {
if (markedCommit != -1) {
throw new TransactionFailureException("Already marked commit " + startRecord);
}
markedCommit = record.getSequenceNumber();
} else {
throw new TransactionFailureException("Illegal record type[" + record.getType() + "]");
}
}
Iterator<Resource> resourceItr = branchSet.iterator();
List<Xid> xids = new LinkedList<Xid>();
while (resourceItr.hasNext()) {
Resource resource = resourceItr.next();
if (!resourceMap.containsKey(resource)) {
resourceMap.put(resource, getXaResource(resource.getResourceId()));
}
xids.add(new XidImpl(startRecord.getGlobalId(), resource.getResourceId()));
}
if (// this xid needs to be committed
markedCommit != -1) {
commitList.add(new NonCompletedTransaction(markedCommit, xids));
} else {
rollbackList.addAll(xids);
}
}
}
use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.
the class TxManager method commit.
private void commit(Thread thread, TransactionImpl tx) throws SystemException, HeuristicMixedException, HeuristicRollbackException {
// mark as commit in log done TxImpl.doCommit()
Throwable commitFailureCause = null;
int xaErrorCode = -1;
if (tx.getResourceCount() == 0) {
tx.setStatus(Status.STATUS_COMMITTED);
} else {
try {
tx.doCommit();
} catch (XAException e) {
xaErrorCode = e.errorCode;
e.printStackTrace();
log.severe("Commit failed, status=" + getTxStatusAsString(tx.getStatus()) + ", errorCode=" + xaErrorCode);
if (tx.getStatus() == Status.STATUS_COMMITTED) {
// this should never be
setTmNotOk();
throw new TransactionFailureException("commit threw exception but status is committed?", e);
}
} catch (Throwable t) {
t.printStackTrace();
commitFailureCause = t;
}
}
if (tx.getStatus() != Status.STATUS_COMMITTED) {
try {
tx.doRollback();
} catch (XAException e) {
e.printStackTrace();
log.severe("Unable to rollback transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->");
setTmNotOk();
if (commitFailureCause != null) {
commitFailureCause.printStackTrace();
}
throw new HeuristicMixedException("Unable to rollback ---> error code in commit: " + xaErrorCode + " ---> error code for rollback: " + e.errorCode);
}
tx.doAfterCompletion();
txThreadMap.remove(thread);
try {
if (tx.isGlobalStartRecordWritten()) {
getTxLog().txDone(tx.getGlobalId());
}
} catch (IOException e) {
e.printStackTrace();
log.severe("Error writing transaction log");
setTmNotOk();
throw new SystemException("TM encountered a problem, " + " error writing transaction log," + e);
}
tx.setStatus(Status.STATUS_NO_TRANSACTION);
if (commitFailureCause == null) {
throw new HeuristicRollbackException("Failed to commit, transaction rolledback ---> " + "error code was: " + xaErrorCode);
} else {
throw new HeuristicRollbackException("Failed to commit, transaction rolledback ---> " + commitFailureCause);
}
}
tx.doAfterCompletion();
txThreadMap.remove(thread);
try {
if (tx.isGlobalStartRecordWritten()) {
getTxLog().txDone(tx.getGlobalId());
}
} catch (IOException e) {
e.printStackTrace();
log.severe("Error writing transaction log");
setTmNotOk();
throw new SystemException("TM encountered a problem, " + " error writing transaction log," + e);
}
tx.setStatus(Status.STATUS_NO_TRANSACTION);
}
use of org.neo4j.graphdb.TransactionFailureException in project graphdb by neo4j-attic.
the class PersistenceManager method delistResourcesForTransaction.
void delistResourcesForTransaction() throws NotInTransactionException {
Transaction tx = this.getCurrentTransaction();
if (tx == null) {
throw new NotInTransactionException();
}
ResourceConnection con = txConnectionMap.get(tx);
if (con != null) {
try {
tx.delistResource(con.getXAResource(), XAResource.TMSUCCESS);
} catch (SystemException e) {
throw new TransactionFailureException("Failed to delist resource '" + con + "' from current transaction.", e);
}
}
}
Aggregations