use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.
the class TransactionImpl_TwoPhaseTest method rollback_whenRollingBackCommitFailedTransaction.
@Test
public void rollback_whenRollingBackCommitFailedTransaction() {
TransactionOptions options = new TransactionOptions().setTransactionType(TWO_PHASE).setDurability(0);
TransactionImpl tx = new TransactionImpl(txManagerService, nodeEngine, options, UUID.randomUUID());
tx.begin();
tx.add(new MockTransactionLogRecord().failCommit());
try {
tx.commit();
fail();
} catch (TransactionException expected) {
}
tx.rollback();
assertEquals(ROLLED_BACK, tx.getState());
}
use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.
the class TransactionImpl_TwoPhaseIntegrationTest method testPrepareFailed.
@Test
public void testPrepareFailed() {
TransactionOptions options = new TransactionOptions().setTransactionType(TWO_PHASE).setDurability(1);
TransactionImpl tx = new TransactionImpl(localTxService, localNodeEngine, options, txOwner);
tx.begin();
MockTransactionLogRecord record = new MockTransactionLogRecord().failPrepare();
tx.add(record);
try {
tx.prepare();
fail();
} catch (TransactionException expected) {
}
assertPreparing(tx);
assertNoBackupLogOnRemote(tx);
record.assertPrepareCalled().assertCommitNotCalled().assertRollbackNotCalled();
}
use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.
the class MapTransactionTest method testGetForUpdate_releasesBackupLock.
@Test
public void testGetForUpdate_releasesBackupLock() {
Config config = getConfig();
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
HazelcastInstance instance1 = factory.newHazelcastInstance(config);
HazelcastInstance instance2 = factory.newHazelcastInstance(config);
final String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
instance1.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext context) throws TransactionException {
TransactionalMap<Object, Object> map = context.getMap(randomString());
map.getForUpdate(keyOwnedByInstance2);
return null;
}
});
NodeEngine nodeEngine = getNodeEngineImpl(instance1);
Data keyData = nodeEngine.toData(keyOwnedByInstance2);
LockSupportService lockService = nodeEngine.getService(LockSupportService.SERVICE_NAME);
for (LockResource lockResource : lockService.getAllLocks()) {
if (keyData.equals(lockResource.getKey())) {
assertEquals(0, lockResource.getLockCount());
}
}
}
use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.
the class MapTransactionTest method testGetForUpdate_LoadsKeyFromMapLoader_whenKeyExistsInDb.
@Test
public void testGetForUpdate_LoadsKeyFromMapLoader_whenKeyExistsInDb() {
final String mapName = randomMapName();
final String valueFromDB = randomString();
final MapStoreAdapter mock = mock(MapStoreAdapter.class);
when(mock.load(anyObject())).thenReturn(valueFromDB);
Config config = new Config();
MapStoreConfig storeConfig = new MapStoreConfig();
storeConfig.setEnabled(true).setImplementation(mock);
config.getMapConfig(mapName).setMapStoreConfig(storeConfig);
HazelcastInstance instance = createHazelcastInstance(config);
instance.executeTransaction(new TransactionalTask<Object>() {
@Override
public Object execute(TransactionalTaskContext context) throws TransactionException {
TransactionalMap<Object, Object> map = context.getMap(mapName);
Object value = map.getForUpdate(1);
assertEquals(valueFromDB, value);
verify(mock, times(1)).load(anyObject());
return null;
}
});
}
use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.
the class MapTransactionTest method testReplace_whenSame.
@Test
public void testReplace_whenSame() throws TransactionException {
Config config = getConfig();
final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
final HazelcastInstance h1 = factory.newHazelcastInstance(config);
final HazelcastInstance h2 = factory.newHazelcastInstance(config);
final IMap map1 = h1.getMap("default");
final IMap map2 = h2.getMap("default");
map2.put("1", "1");
map2.put("2", "2");
boolean b = h1.executeTransaction(options, new TransactionalTask<Boolean>() {
public Boolean execute(TransactionalTaskContext context) throws TransactionException {
final TransactionalMap<Object, Object> txMap = context.getMap("default");
assertTrue(txMap.replace("1", "1", "11"));
assertFalse(txMap.replace("5", "5", "55"));
assertFalse(txMap.replace("2", "1", "22"));
assertEquals("1", map1.get("1"));
assertEquals("11", txMap.get("1"));
assertEquals("2", map1.get("2"));
assertEquals("2", txMap.get("2"));
return true;
}
});
assertTrue(b);
assertEquals("11", map1.get("1"));
assertEquals("11", map2.get("1"));
assertEquals("2", map1.get("2"));
assertEquals("2", map2.get("2"));
}
Aggregations