use of org.apache.geode.cache.TransactionWriter in project geode by apache.
the class DistTXState method precommit.
/*
* (non-Javadoc)
*
* @see org.apache.geode.internal.cache.TXStateInterface#commit()
*
* Take Locks Does conflict check on primary ([DISTTX] TODO on primary only) Invoke TxWriter
*/
@Override
public void precommit() throws CommitConflictException, UnsupportedOperationInTransactionException {
if (logger.isDebugEnabled()) {
logger.debug("DistTXState.precommit transaction {} is closed {} ", getTransactionId(), this.closed, new Throwable());
}
if (this.closed) {
return;
}
synchronized (this.completionGuard) {
this.completionStarted = true;
}
if (onBehalfOfRemoteStub && !proxy.isCommitOnBehalfOfRemoteStub()) {
throw new UnsupportedOperationInTransactionException(LocalizedStrings.TXState_CANNOT_COMMIT_REMOTED_TRANSACTION.toLocalizedString());
}
cleanupNonDirtyRegions();
/*
* Lock buckets so they can't be rebalanced then perform the conflict check to fix #43489
*/
try {
lockBucketRegions();
} catch (PrimaryBucketException pbe) {
// not sure what to do here yet
RuntimeException re = new TransactionDataRebalancedException(LocalizedStrings.PartitionedRegion_TRANSACTIONAL_DATA_MOVED_DUE_TO_REBALANCING.toLocalizedString());
re.initCause(pbe);
throw re;
}
if (this.locks == null) {
reserveAndCheck();
}
// For internal testing
if (this.internalAfterConflictCheck != null) {
this.internalAfterConflictCheck.run();
}
updateRegionVersions();
generateTailKeysForParallelDispatcherEvents();
/*
* If there is a TransactionWriter plugged in, we need to to give it an opportunity to abort the
* transaction.
*/
TransactionWriter writer = this.proxy.getTxMgr().getWriter();
if (!firedWriter && writer != null) {
try {
firedWriter = true;
writer.beforeCommit(getEvent());
} catch (TransactionWriterException twe) {
cleanup();
throw new CommitConflictException(twe);
} catch (VirtualMachineError err) {
// cleanup(); this allocates objects so I don't think we can do it -
// that leaves the TX open, but we are poison pilling so we should be
// ok??
SystemFailure.initiateFailure(err);
// now, so don't let this thread continue.
throw err;
} catch (Throwable t) {
// rollback the transaction!
cleanup();
// Whenever you catch Error or Throwable, you must also
// catch VirtualMachineError (see above). However, there is
// _still_ a possibility that you are dealing with a cascading
// error condition, so you also need to check to see if the JVM
// is still usable:
SystemFailure.checkFailure();
throw new CommitConflictException(t);
}
}
}
use of org.apache.geode.cache.TransactionWriter in project geode by apache.
the class TXWriterJUnitTest method testAfterCommitFailedOnTransactionWriterThrow.
/**
* make sure standard Cache(Listener,Writer) are not called during rollback due to transaction
* writer throw
*/
@Test
public void testAfterCommitFailedOnTransactionWriterThrow() throws Exception {
installCacheListenerAndWriter();
((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
throw new TransactionWriterException("Rollback now!");
}
public void close() {
}
});
installTransactionListener();
this.txMgr.begin();
this.region.create("key1", "value1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown CommitConflictException");
} catch (CommitConflictException expected) {
assertNotNull(expected.getCause());
assertTrue(expected.getCause() instanceof TransactionWriterException);
}
assertEquals(0, this.cbCount);
assertEquals(1, this.failedCommits);
assertEquals(0, this.afterCommits);
assertEquals(0, this.afterRollbacks);
}
use of org.apache.geode.cache.TransactionWriter in project geode by apache.
the class TXWriterOOMEJUnitTest method testAfterCommitFailedOnThrowOOM.
@Test
public void testAfterCommitFailedOnThrowOOM() throws Exception {
installCacheListenerAndWriter();
// install TransactionWriter
((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
throw new OutOfMemoryError("this is expected!");
}
public void close() {
}
});
installTransactionListener();
try {
SystemFailureTestHook.setExpectedFailureClass(OutOfMemoryError.class);
this.txMgr.begin();
this.region.create("key1", "value1");
this.cbCount = 0;
try {
this.txMgr.commit();
fail("Commit should have thrown OOME");
} catch (OutOfMemoryError expected) {
// this is what we expect
}
// no callbacks were invoked
assertEquals(0, this.cbCount);
assertEquals(0, this.failedCommits);
assertEquals(0, this.afterCommits);
assertEquals(0, this.afterRollbacks);
} finally {
SystemFailureTestHook.setExpectedFailureClass(null);
}
}
use of org.apache.geode.cache.TransactionWriter in project geode by apache.
the class CacheXml66DUnitTest method testTransactionWriter.
/**
* Tests that a region created with a named attributes set programmatically for delta propogation
* has the correct attributes.
*/
@Test
public void testTransactionWriter() throws Exception {
CacheCreation creation = new CacheCreation();
CacheTransactionManagerCreation ctmc = new CacheTransactionManagerCreation();
ctmc.setWriter(new TestTransactionWriter());
creation.addCacheTransactionManagerCreation(ctmc);
testXml(creation);
Cache c = getCache();
assertTrue(c instanceof GemFireCacheImpl);
c.loadCacheXml(generate(creation));
TransactionWriter tw = c.getCacheTransactionManager().getWriter();
assertTrue("tw should be TransactionWriter, but it is:" + tw, tw instanceof TestTransactionWriter);
}
use of org.apache.geode.cache.TransactionWriter in project geode by apache.
the class RemoteTransactionDUnitTest method testRemoteExceptionThrown.
@Test
public void testRemoteExceptionThrown() {
Host host = Host.getHost(0);
VM acc = host.getVM(0);
VM datastore = host.getVM(1);
initAccessorAndDataStore(acc, datastore, 0);
VM accessor = getVMForTransactions(acc, datastore);
datastore.invoke(new SerializableCallable() {
public Object call() throws Exception {
getGemfireCache().getTxManager().setWriter(new TransactionWriter() {
public void close() {
}
public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
throw new TransactionWriterException("AssertionError");
}
});
return null;
}
});
accessor.invoke(new SerializableCallable() {
public Object call() throws Exception {
getGemfireCache().getTxManager().begin();
Region r = getCache().getRegion(CUSTOMER);
r.put(new CustId(8), new Customer("name8", "address8"));
try {
getGemfireCache().getTxManager().commit();
fail("Expected exception not thrown");
} catch (Exception e) {
assertEquals("AssertionError", e.getCause().getMessage());
}
return null;
}
});
}
Aggregations