use of javax.transaction.xa.Xid in project graphdb by neo4j-attic.
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 graphdb by neo4j-attic.
the class TransactionImpl method doCommit.
void doCommit() throws XAException, SystemException {
boolean onePhase = isOnePhase();
boolean readOnly = true;
if (!onePhase) {
// prepare
status = Status.STATUS_PREPARING;
LinkedList<Xid> preparedXids = new LinkedList<Xid>();
Iterator<ResourceElement> itr = resourceList.iterator();
while (itr.hasNext()) {
ResourceElement re = itr.next();
if (!preparedXids.contains(re.getXid())) {
preparedXids.add(re.getXid());
int vote = re.getResource().prepare(re.getXid());
if (vote == XAResource.XA_OK) {
readOnly = false;
} else if (vote == XAResource.XA_RDONLY) {
re.setStatus(RS_READONLY);
} else {
// rollback tx
status = Status.STATUS_MARKED_ROLLBACK;
return;
}
} else {
// set it to readonly, only need to commit once
re.setStatus(RS_READONLY);
}
}
status = Status.STATUS_PREPARED;
}
// commit
if (!onePhase && readOnly) {
status = Status.STATUS_COMMITTED;
return;
}
if (!onePhase) {
try {
txManager.getTxLog().markAsCommitting(getGlobalId());
} catch (IOException e) {
e.printStackTrace();
log.severe("Error writing transaction log");
txManager.setTmNotOk();
throw new SystemException("TM encountered a problem, " + " error writing transaction log," + e);
}
}
status = Status.STATUS_COMMITTING;
Iterator<ResourceElement> itr = resourceList.iterator();
while (itr.hasNext()) {
ResourceElement re = itr.next();
if (re.getStatus() != RS_READONLY) {
re.getResource().commit(re.getXid(), onePhase);
}
}
status = Status.STATUS_COMMITTED;
}
use of javax.transaction.xa.Xid in project graphdb by neo4j-attic.
the class TransactionImpl method enlistResource.
public synchronized boolean enlistResource(XAResource xaRes) throws RollbackException, IllegalStateException, SystemException {
if (xaRes == null) {
throw new IllegalArgumentException("Null xa resource");
}
if (status == Status.STATUS_ACTIVE || status == Status.STATUS_PREPARING) {
try {
if (resourceList.size() == 0) {
if (!globalStartRecordWritten) {
txManager.writeStartRecord(globalId);
globalStartRecordWritten = true;
}
//
byte[] branchId = txManager.getBranchId(xaRes);
Xid xid = new XidImpl(globalId, branchId);
resourceList.add(new ResourceElement(xid, xaRes));
xaRes.start(xid, XAResource.TMNOFLAGS);
try {
txManager.getTxLog().addBranch(globalId, branchId);
} catch (IOException e) {
e.printStackTrace();
log.severe("Error writing transaction log");
txManager.setTmNotOk();
throw new SystemException("TM encountered a problem, " + " error writing transaction log," + e);
}
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);
try {
txManager.getTxLog().addBranch(globalId, branchId);
} catch (IOException e) {
e.printStackTrace();
log.severe("Error writing transaction log");
txManager.setTmNotOk();
throw new SystemException("TM encountered a problem, " + " error writing transaction log," + e);
}
}
return true;
} catch (XAException e) {
e.printStackTrace();
log.severe("Unable to enlist resource[" + xaRes + "]");
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.Xid in project graphdb by neo4j-attic.
the class TestXa method testApplyLogicalLog.
@Test
public void testApplyLogicalLog() throws Exception {
ds.keepLogicalLogs(true);
Xid xid = new XidImpl(new byte[1], new byte[1]);
XAResource xaRes = xaCon.getXaResource();
xaRes.start(xid, XAResource.TMNOFLAGS);
long node1 = ds.nextId(Node.class);
xaCon.getNodeConsumer().createNode(node1);
long node2 = ds.nextId(Node.class);
xaCon.getNodeConsumer().createNode(node2);
long n1prop1 = ds.nextId(PropertyStore.class);
xaCon.getNodeConsumer().addProperty(node1, n1prop1, index("prop1"), "string1");
xaCon.getNodeConsumer().getProperties(node1, false);
int relType1 = (int) ds.nextId(RelationshipType.class);
xaCon.getRelationshipTypeConsumer().addRelationshipType(relType1, "relationshiptype1");
long rel1 = ds.nextId(Relationship.class);
xaCon.getRelationshipConsumer().createRelationship(rel1, node1, node2, relType1);
long r1prop1 = ds.nextId(PropertyStore.class);
xaCon.getRelationshipConsumer().addProperty(rel1, r1prop1, index("prop1"), "string1");
xaCon.getNodeConsumer().changeProperty(node1, n1prop1, "string2");
xaCon.getRelationshipConsumer().changeProperty(rel1, r1prop1, "string2");
xaCon.getNodeConsumer().removeProperty(node1, n1prop1);
xaCon.getRelationshipConsumer().removeProperty(rel1, r1prop1);
xaCon.getRelationshipConsumer().deleteRelationship(rel1);
xaCon.getNodeConsumer().deleteNode(node1);
xaCon.getNodeConsumer().deleteNode(node2);
xaRes.end(xid, XAResource.TMSUCCESS);
xaRes.commit(xid, true);
long currentVersion = ds.getCurrentLogVersion();
ds.keepLogicalLogs(true);
ds.rotateLogicalLog();
ds.rotateLogicalLog();
ds.rotateLogicalLog();
ds.setCurrentLogVersion(currentVersion);
ds.setLastCommittedTxId(0);
ds.makeBackupSlave();
ds.applyLog(ds.getLogicalLog(currentVersion));
ds.applyLog(ds.getLogicalLog(currentVersion + 1));
ds.applyLog(ds.getLogicalLog(currentVersion + 2));
ds.keepLogicalLogs(false);
}
use of javax.transaction.xa.Xid in project graphdb by neo4j-attic.
the class TestXa method testBrokenNodeCommand.
@Test
public void testBrokenNodeCommand() throws Exception {
Xid xid = new XidImpl(new byte[4], new byte[4]);
XAResource xaRes = xaCon.getXaResource();
xaRes.start(xid, XAResource.TMNOFLAGS);
long node1 = ds.nextId(Node.class);
xaCon.getNodeConsumer().createNode(node1);
xaRes.end(xid, XAResource.TMSUCCESS);
xaRes.prepare(xid);
xaCon.clearAllTransactions();
copyLogicalLog(path());
xaCon.clearAllTransactions();
ds.close();
deleteLogicalLogIfExist();
renameCopiedLogicalLog(path());
truncateLogicalLog(40);
ds = newNeoStore();
// ds = new NeoStoreXaDataSource( file( "neo" ), file( "nioneo_logical.log" ),
// lockManager, lockReleaser );
xaCon = (NeoStoreXaConnection) ds.getXaConnection();
xaRes = xaCon.getXaResource();
assertEquals(0, xaRes.recover(XAResource.TMNOFLAGS).length);
xaCon.clearAllTransactions();
}
Aggregations