use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.
the class PeerTypeRegistration method updateRegion.
private void updateRegion(Object k, Object v) {
Region<Object, Object> r = getIdToType();
InternalCache cache = (InternalCache) r.getRegionService();
checkDistributedTypeRegistryState();
TXManagerImpl txManager = (TXManagerImpl) cache.getCacheTransactionManager();
TXStateProxy currentState = suspendTX();
boolean state = useUDPMessagingIfNecessary();
try {
// The loop might not be necessary because we're
// updating a replicated region inside a dlock,
// but just in case we'll make sure to retry the transaction.
int failureCount = 0;
while (true) {
txManager.begin();
try {
r.put(k, v);
txManager.commit();
return;
} catch (TransactionException e) {
// even put can now throw a TransactionException, rollback if required
if (txManager.exists()) {
txManager.rollback();
}
// Let's just make sure things don't get out of hand.
if (++failureCount > MAX_TRANSACTION_FAILURES) {
throw e;
}
}
}
} finally {
releaseUDPMessaging(state);
resumeTX(currentState);
}
}
use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.
the class PeerTypeRegistration method allocateTypeId.
private int allocateTypeId(PdxType newType) {
TXStateProxy currentState = suspendTX();
Region<Object, Object> r = getIdToType();
int id = newType.hashCode() & PLACE_HOLDER_FOR_TYPE_ID;
int newTypeId = id | this.dsId;
try {
int maxTry = maxTypeId;
while (r.get(newTypeId) != null) {
maxTry--;
if (maxTry == 0) {
throw new InternalGemFireError("Used up all of the PDX type ids for this distributed system. The maximum number of PDX types is " + maxTypeId);
}
// Find the next available type id.
id++;
if (id > this.maxTypeId) {
id = 1;
}
newTypeId = id | this.dsId;
}
return newTypeId;
} finally {
resumeTX(currentState);
}
}
use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.
the class PeerTypeRegistration method getEnumById.
public EnumInfo getEnumById(int id) {
verifyConfiguration();
EnumId enumId = new EnumId(id);
TXStateProxy currentState = suspendTX();
try {
return (EnumInfo) getIdToType().get(enumId);
} finally {
resumeTX(currentState);
}
}
use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.
the class PeerTypeRegistration method getExistingIdForEnum.
/** Should be called holding the dlock */
private EnumId getExistingIdForEnum(EnumInfo ei) {
TXStateProxy currentState = suspendTX();
int totalEnumIdInDS = 0;
try {
EnumId result = null;
for (Map.Entry<Object, Object> entry : getIdToType().entrySet()) {
Object v = entry.getValue();
Object k = entry.getKey();
if (k instanceof EnumId) {
EnumId id = (EnumId) k;
EnumInfo info = (EnumInfo) v;
enumToId.put(info, id);
int tmpDsId = PLACE_HOLDER_FOR_DS_ID & id.intValue();
if (tmpDsId == this.dsId) {
totalEnumIdInDS++;
}
if (ei.equals(info)) {
result = id;
}
} else {
typeToId.put((PdxType) v, (Integer) k);
}
}
if (totalEnumIdInDS == this.maxTypeId) {
throw new InternalGemFireError("Used up all of the PDX enum ids for this distributed system. The maximum number of PDX types is " + this.maxTypeId);
}
return result;
} finally {
resumeTX(currentState);
}
}
use of org.apache.geode.internal.cache.TXStateProxy in project geode by apache.
the class TXJUnitTest method testJTASynchronization.
@Test
public void testJTASynchronization() throws CacheException, javax.transaction.NotSupportedException, javax.transaction.RollbackException, javax.transaction.SystemException, javax.transaction.HeuristicMixedException, javax.transaction.HeuristicRollbackException {
javax.transaction.TransactionManager jtaTxMgr = this.cache.getJTATransactionManager();
TransactionListener tl = new TransactionListener() {
@Override
public void afterCommit(TransactionEvent event) {
++listenerAfterCommit;
te = event;
}
@Override
public void afterFailedCommit(TransactionEvent event) {
++listenerAfterFailedCommit;
te = event;
}
@Override
public void afterRollback(TransactionEvent event) {
++listenerAfterRollback;
te = event;
}
@Override
public void close() {
++listenerClose;
}
};
this.txMgr.addListener(tl);
Synchronization gfTXSync;
// Test successful JTA commit
jtaTxMgr.begin();
this.txMgr.begin();
{
TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
gfTXSync = gfTxMgrImpl.getTXState();
}
jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
assertEquals(0, this.listenerAfterCommit);
this.cache.getLogger().info("SWAP:doingCreate");
this.region.create("syncKey1", "syncVal1");
jtaTxMgr.commit();
assertEquals(1, this.listenerAfterCommit);
assertEquals("syncVal1", this.region.getEntry("syncKey1").getValue());
try {
this.txMgr.commit();
fail("JTA Cache Manager should have called commit!");
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable expected) {
}
// Test JTA rollback
jtaTxMgr.begin();
this.txMgr.begin();
{
TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
gfTXSync = gfTxMgrImpl.getTXState();
}
jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
assertEquals(0, this.listenerAfterRollback);
this.region.put("syncKey2", "syncVal2");
jtaTxMgr.rollback();
assertEquals(1, this.listenerAfterRollback);
assertTrue(!this.region.containsKey("syncKey2"));
// Test failed JTA commit with suspend
jtaTxMgr.begin();
this.txMgr.begin();
{
TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
gfTXSync = gfTxMgrImpl.getTXState();
jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
assertEquals(0, this.listenerAfterFailedCommit);
this.region.put("syncKey3", "syncVal3");
assertEquals("syncVal3", this.region.getEntry("syncKey3").getValue());
TXStateProxy gfTx = gfTxMgrImpl.internalSuspend();
javax.transaction.Transaction jtaTx = jtaTxMgr.suspend();
assertNull(jtaTxMgr.getTransaction());
this.region.put("syncKey3", "syncVal4");
assertEquals("syncVal4", this.region.getEntry("syncKey3").getValue());
gfTxMgrImpl.internalResume(gfTx);
try {
jtaTxMgr.resume(jtaTx);
} catch (Exception failure) {
fail("JTA resume failed");
}
assertNotNull(jtaTxMgr.getTransaction());
}
assertEquals("syncVal3", this.region.getEntry("syncKey3").getValue());
try {
jtaTxMgr.commit();
fail("Expected JTA manager conflict exception!");
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable expected) {
}
assertEquals(1, this.listenerAfterFailedCommit);
assertEquals("syncVal4", this.region.getEntry("syncKey3").getValue());
// Test failed JTA commit with a new thread
jtaTxMgr.begin();
this.txMgr.begin();
{
TXManagerImpl gfTxMgrImpl = (TXManagerImpl) this.txMgr;
gfTXSync = gfTxMgrImpl.getTXState();
jtaTxMgr.getTransaction().registerSynchronization(gfTXSync);
assertEquals(1, this.listenerAfterFailedCommit);
this.region.put("syncKey4", "syncVal3");
assertEquals("syncVal3", this.region.getEntry("syncKey4").getValue());
// Create a new thread and have it update the same key, causing
// a conflict
final int[] signal = { 0 };
Thread t = new Thread("non-TX conflict generator") {
@Override
public void run() {
try {
region.put("syncKey4", "syncVal4");
while (true) synchronized (signal) {
signal[0] = 1;
signal.notify();
signal.wait();
if (signal[0] == 0) {
break;
}
}
} catch (Exception error) {
fail("Non-tx thread failure due to: " + error);
}
}
};
t.start();
try {
while (true) synchronized (signal) {
if (signal[0] == 1) {
signal[0] = 0;
signal.notify();
break;
} else {
signal.wait();
}
}
} catch (InterruptedException dangit) {
fail("Tx thread waiting for non-tx thread failed due to : " + dangit);
}
assertEquals("syncVal3", this.region.getEntry("syncKey4").getValue());
}
try {
jtaTxMgr.commit();
fail("Expected JTA manager conflict exception!");
} catch (javax.transaction.HeuristicRollbackException expected) {
} catch (javax.transaction.RollbackException alsoExpected) {
} catch (VirtualMachineError e) {
SystemFailure.initiateFailure(e);
throw e;
} catch (Throwable yuk) {
fail("Did not expect this throwable from JTA commit: " + yuk);
}
assertEquals(2, this.listenerAfterFailedCommit);
assertEquals("syncVal4", this.region.getEntry("syncKey4").getValue());
this.txMgr.removeListener(tl);
}
Aggregations