use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.
the class TransactionQueueTest method testQueueWithMap.
@Test
public void testQueueWithMap() {
int insCount = 4;
String queueName = "defQueue";
String mapName = "defMap";
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(insCount);
HazelcastInstance[] instances = factory.newInstances(getConfig());
instances[0].getMap(mapName).lock("lock1");
TransactionContext context = instances[1].newTransactionContext(new TransactionOptions().setTimeout(5, SECONDS));
context.beginTransaction();
try {
boolean offered = context.getQueue(queueName).offer(new VersionedObject<>("item1"));
assertTrue(offered);
context.getMap(mapName).put("lock1", "value1");
fail();
} catch (TransactionException ex) {
// expected
context.rollbackTransaction();
}
assertEquals(0, instances[0].getQueue(queueName).size());
assertNull(instances[0].getMap(mapName).get("lock1"));
}
use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.
the class TransactionQueueTest method testPeekMethod.
@Test
public void testPeekMethod() throws Exception {
int insCount = 4;
String name = "defQueue";
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(insCount);
HazelcastInstance[] instances = factory.newInstances(getConfig());
TransactionContext context = instances[0].newTransactionContext();
context.beginTransaction();
try {
TransactionalQueue<VersionedObject<String>> q = context.getQueue(name);
VersionedObject<String> response1 = q.peek(10, SECONDS);
assertNull(response1);
assertTrue(q.offer(new VersionedObject<>("ali")));
VersionedObject<String> response2 = q.peek();
assertEquals(new VersionedObject<>("ali"), response2);
context.commitTransaction();
} catch (TransactionException e) {
context.rollbackTransaction();
throw e;
}
assertEquals(1, getQueue(instances, name).size());
}
use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.
the class QueueContainer method txnCommitOffer.
/**
* Sets the data of a reserved item and commits the change so it can be
* visible outside a transaction.
* The commit means that the item is offered to the queue if
* {@code backup} is false or saved into a backup map if {@code backup} is {@code true}.
* This is because a node can hold backups for queues on other nodes.
* Cancels the queue eviction if one is scheduled.
*
* @param itemId the ID of the reserved item
* @param data the data to be associated with the reserved item
* @param backup if the item is to be offered to the underlying queue or stored as a backup
* @return {@code true} if the commit succeeded
* @throws TransactionException if there is no reserved item with the {@code itemId}
*/
public boolean txnCommitOffer(long itemId, Data data, boolean backup) {
QueueItem item = txMap.remove(itemId);
if (item == null && !backup) {
throw new TransactionException("No reserve: " + itemId);
} else if (item == null) {
item = new QueueItem(this, itemId, data);
}
item.setSerializedObject(data);
if (!backup) {
getItemQueue().offer(item);
cancelEvictionIfExists();
} else {
getBackupMap().put(itemId, item);
}
if (store.isEnabled() && !backup) {
try {
store.store(item.getItemId(), data);
} catch (Exception e) {
logger.warning("Exception during store", e);
}
}
return true;
}
use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.
the class ClusterStateManager method commitClusterState.
public void commitClusterState(ClusterStateChange stateChange, Address initiator, UUID txnId, boolean isTransient) {
Preconditions.checkNotNull(stateChange);
stateChange.validate();
clusterServiceLock.lock();
try {
final LockGuard stateLock = getStateLock();
if (!stateLock.allowsUnlock(txnId)) {
throw new TransactionException("Cluster state change [" + state + " -> " + stateChange + "] failed for " + initiator + ", current state: " + stateToString());
}
if (stateChange.isOfType(ClusterState.class)) {
ClusterState newState = (ClusterState) stateChange.getNewState();
doSetClusterState(newState, isTransient);
// if state is changed to allow joins, then remove all members which left while not active.
if (newState.isJoinAllowed()) {
node.getClusterService().getMembershipManager().removeAllMissingMembers();
}
} else if (stateChange.isOfType(Version.class)) {
// version is validated on cluster-state-lock, thus we can commit without checking compatibility
Version newVersion = (Version) stateChange.getNewState();
logger.info("Cluster version set to " + newVersion);
doSetClusterVersion(newVersion);
} else {
throw new IllegalArgumentException("Illegal ClusterStateChange of type " + stateChange.getType() + ".");
}
} finally {
clusterServiceLock.unlock();
}
}
use of com.hazelcast.transaction.TransactionException in project hazelcast by hazelcast.
the class ClientTransactionUtil method invoke.
/**
* Handles the invocation exception for transactions so that users will not see internal exceptions.
* <p>
* More specifically IOException, because in case of a IO problem in ClientInvocation that send to a connection
* sends IOException to user. This wraps that exception into a TransactionException.
*/
public static ClientMessage invoke(ClientMessage request, Object objectName, HazelcastClientInstanceImpl client, Connection connection) {
try {
final ClientInvocation clientInvocation = new ClientInvocation(client, request, objectName, connection);
final Future<ClientMessage> future = clientInvocation.invoke();
return future.get();
} catch (Exception e) {
throw rethrow(e, TRANSACTION_EXCEPTION_WRAPPER);
}
}
Aggregations