@Override public <T> T executeTransaction(TransactionOptions options, TransactionalTask<T> task) throws TransactionException { final TransactionContext context = newTransactionContext(options); context.beginTransaction(); try { final T value = task.execute(context); context.commitTransaction(); return value; } catch (Throwable e) { context.rollbackTransaction(); if (e instanceof TransactionException) { throw (TransactionException) e; } if (e.getCause() instanceof TransactionException) { throw (TransactionException) e.getCause(); } if (e instanceof RuntimeException) { throw (RuntimeException) e; } throw new TransactionException(e); } }
private void begin() { transactionContext = hazelcastInstance.newTransactionContext(); transactionContext.beginTransaction(); transactionalMap = transactionContext.getMap(name); }
@Override public <E> TransactionalQueue<E> getTransactionalQueue(String name) { TransactionContext txContext = getTransactionContext(); return txContext.getQueue(name); }
public static void main(String[] args) throws Exception { HazelcastInstance hz = Hazelcast.newHazelcastInstance(); TransactionOptions txOptions = new TransactionOptions().setTimeout(10, TimeUnit.SECONDS); TransactionContext txCxt = hz.newTransactionContext(txOptions); txCxt.beginTransaction(); TransactionalMap<String, String> map = txCxt.getMap("map"); try { map.put("1", "1"); map.put("2", "2"); txCxt.commitTransaction(); } catch (RuntimeException t) { txCxt.rollbackTransaction(); throw t; } System.out.println("Finished"); Hazelcast.shutdownAll(); } }
@Override public void rollback() { context.rollbackTransaction(); } }
private void commit() { transactionContext.commitTransaction(); transactionContext = null; transactionalMap = null; } }
@Override protected Object innerCall() throws Exception { final TransactionContext context = endpoint.getTransactionContext(parameters.txnId); final TransactionalMap map = context.getMap(parameters.name); Set keySet = map.keySet(); List<Data> list = new ArrayList<Data>(keySet.size()); for (Object o : keySet) { list.add(serializationService.toData(o)); } return list; }
@Override public void rollback() { if (isTransacted()) { contextLock.lock(); try { txnContext.rollbackTransaction(); // It appears that Hazelcast doesn't allow a transaction context // to be reused so we'll recreate the context after commit. txnContext = config.getHazelcastInstance().newTransactionContext(); txnContext.beginTransaction(); } finally { contextLock.unlock(); } } }
@Override public void commit() { if (isTransacted()) { contextLock.lock(); try { txnContext.commitTransaction(); // It appears that Hazelcast doesn't allow a transaction context // to be reused so we'll recreate the context after commit. txnContext = config.getHazelcastInstance().newTransactionContext(); txnContext.beginTransaction(); } finally { contextLock.unlock(); } } }
/** * @see TransactionContext#beginTransaction() */ public void beginTransaction() { transactionContext.beginTransaction(); transactionActive = true; }
@Override protected Object innerCall() throws Exception { TransactionOptions options = new TransactionOptions(); options.setDurability(parameters.durability); options.setTimeout(parameters.timeout, TimeUnit.MILLISECONDS); options.setTransactionType(TransactionOptions.TransactionType.getByValue(parameters.transactionType)); TransactionManagerServiceImpl transactionManager = (TransactionManagerServiceImpl) clientEngine.getTransactionManagerService(); TransactionContext context = transactionManager.newClientTransactionContext(options, endpoint.getUuid()); context.beginTransaction(); endpoint.setTransactionContext(context); return context.getTxnId(); }
@Override public <K, V> TransactionalMultiMap<K, V> getMultiMap(String name) { return transactionContext().getMultiMap(name); }
@Override public <E> TransactionalList<E> getList(String name) { return transactionContext().getList(name); }
@Override public <E> TransactionalSet<E> getTransactionalSet(String name) { TransactionContext txContext = getTransactionContext(); return txContext.getSet(name); }
@Override public void rollback() { if (this.transaction != null) { this.transaction.rollbackTransaction(); } }
private void commit() { transactionContext.commitTransaction(); transactionContext = null; transactionalMap = null; } }
@Override protected Object innerCall() throws Exception { final TransactionContext context = endpoint.getTransactionContext(parameters.txnId); final TransactionalMap map = context.getMap(parameters.name); return map.replace(parameters.key, parameters.oldValue, parameters.newValue); }
/** * @see TransactionContext#beginTransaction() */ public void beginTransaction() { transactionContext.beginTransaction(); transactionActive = true; }
@Override protected Object innerCall() throws Exception { TransactionOptions options = new TransactionOptions(); options.setDurability(parameters.durability); options.setTimeout(parameters.timeout, TimeUnit.MILLISECONDS); options.setTransactionType(TransactionOptions.TransactionType.getByValue(parameters.transactionType)); TransactionManagerServiceImpl transactionManager = (TransactionManagerServiceImpl) clientEngine.getTransactionManagerService(); TransactionContext context = transactionManager.newClientTransactionContext(options, endpoint.getUuid()); context.beginTransaction(); endpoint.setTransactionContext(context); return context.getTxnId(); }
@Override public <K, V> TransactionalMultiMap<K, V> getMultiMap(String name) { return transactionContext().getMultiMap(name); }