use of javax.transaction.xa.XAException in project neo4j-mobile-android by neo4j-contrib.
the class XaLogicalLog method applyTwoPhaseCommitEntry.
private void applyTwoPhaseCommitEntry(LogEntry.TwoPhaseCommit 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();
if (xid == null) {
throw new IOException("Xid null for identifier " + identifier);
}
try {
XaTransaction xaTx = xaRm.getXaTransaction(xid);
xaTx.setCommitTxId(txId);
xaRm.injectTwoPhaseCommit(xid);
registerRecoveredTransaction(txId);
} catch (XAException e) {
throw new IOException(e);
}
}
use of javax.transaction.xa.XAException in project neo4j-mobile-android by neo4j-contrib.
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 graphdb by neo4j-attic.
the class TestXaFramework method testCreateXaResource.
@Test
public void testCreateXaResource() throws Exception {
Map<Object, Object> config = new HashMap<Object, Object>();
config.put("store_dir", "target/var");
config.put(LogBufferFactory.class, CommonFactories.defaultLogBufferFactory(config));
xaDsMgr.registerDataSource("dummy_datasource", new DummyXaDataSource(config), UTF8.encode("DDDDDD"));
XaDataSource xaDs = xaDsMgr.getXaDataSource("dummy_datasource");
DummyXaConnection xaC = null;
try {
xaC = (DummyXaConnection) xaDs.getXaConnection();
try {
xaC.doStuff1();
fail("Non enlisted resource should throw exception");
} catch (XAException e) {
// good
}
Xid xid = new XidImpl(new byte[0], new byte[0]);
xaC.getXaResource().start(xid, XAResource.TMNOFLAGS);
try {
xaC.doStuff1();
xaC.doStuff2();
} catch (XAException e) {
fail("Enlisted resource should not throw exception");
}
xaC.getXaResource().end(xid, XAResource.TMSUCCESS);
xaC.getXaResource().prepare(xid);
xaC.getXaResource().commit(xid, false);
} finally {
xaDsMgr.unregisterDataSource("dummy_datasource");
if (xaC != null) {
xaC.destroy();
}
}
// cleanup dummy resource log
File dir = new File(".");
File[] files = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String fileName) {
return fileName.startsWith(resourceFile());
}
});
for (int i = 0; i < files.length; i++) {
files[i].delete();
}
}
use of javax.transaction.xa.XAException in project graphdb by neo4j-attic.
the class ReadOnlyTxManager method rollback.
public void rollback() throws IllegalStateException, SystemException {
Thread thread = Thread.currentThread();
ReadOnlyTransactionImpl tx = txThreadMap.get(thread);
if (tx == null) {
throw new IllegalStateException("Not in transaction");
}
if (tx.getStatus() == Status.STATUS_ACTIVE || tx.getStatus() == Status.STATUS_MARKED_ROLLBACK || tx.getStatus() == Status.STATUS_PREPARING) {
tx.doBeforeCompletion();
try {
tx.doRollback();
} catch (XAException e) {
e.printStackTrace();
log.severe("Unable to rollback marked or active transaction. " + "Some resources may be commited others not. " + "Neo4j kernel should be SHUTDOWN for " + "resource maintance and transaction recovery ---->");
throw new SystemException("Unable to rollback " + " ---> error code for rollback: " + e.errorCode);
}
tx.doAfterCompletion();
txThreadMap.remove(thread);
tx.setStatus(Status.STATUS_NO_TRANSACTION);
} else {
throw new IllegalStateException("Tx status is: " + getTxStatusAsString(tx.getStatus()));
}
}
use of javax.transaction.xa.XAException 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);
}
}
Aggregations