当前位置: 首页>>代码示例>>Java>>正文


Java TransactionContext.commitTransaction方法代码示例

本文整理汇总了Java中com.hazelcast.transaction.TransactionContext.commitTransaction方法的典型用法代码示例。如果您正苦于以下问题:Java TransactionContext.commitTransaction方法的具体用法?Java TransactionContext.commitTransaction怎么用?Java TransactionContext.commitTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.hazelcast.transaction.TransactionContext的用法示例。


在下文中一共展示了TransactionContext.commitTransaction方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: timestep

import com.hazelcast.transaction.TransactionContext; //导入方法依赖的package包/类
@TimeStep
public void timestep(ThreadState state) {
    int key = state.nextRandom(0, range / 2);

    TransactionOptions transactionOptions = new TransactionOptions()
            .setTransactionType(transactionType)
            .setDurability(durability);

    TransactionContext transactionContext = targetInstance.newTransactionContext(transactionOptions);

    transactionContext.beginTransaction();

    TransactionalMap<Object, Object> txMap = transactionContext.getMap("map");

    try {
        Object val = txMap.getForUpdate(key);

        if (val != null) {
            key = state.nextRandom(range / 2, range);
        }

        txMap.put(key, (long) key);

        transactionContext.commitTransaction();
    } catch (Exception e) {
        logger.fatal("----------------------tx exception -------------------------", e);

        if (failOnException) {
            throw rethrow(e);
        }

        transactionContext.rollbackTransaction();
    }
}
 
开发者ID:hazelcast,项目名称:hazelcast-simulator,代码行数:35,代码来源:MapTransactionContextTest.java

示例2: timeStep

import com.hazelcast.transaction.TransactionContext; //导入方法依赖的package包/类
@TimeStep
public void timeStep(ThreadState state) {
    firstLock.lock();
    try {
        TransactionContext ctx = targetInstance.newTransactionContext();
        try {
            ctx.beginTransaction();

            TransactionalQueue<Integer> queue = ctx.getQueue(name + 'q');
            queue.offer(1);

            secondLock.lock();
            secondLock.unlock();

            queue.take();

            ctx.commitTransaction();
            state.counter.committed++;
        } catch (Exception txnException) {
            try {
                ctx.rollbackTransaction();
                state.counter.rolled++;

                logger.fatal(name + ": Exception in txn " + state.counter, txnException);
            } catch (Exception rollException) {
                state.counter.failedRollbacks++;
                logger.fatal(name + ": Exception in roll " + state.counter, rollException);
            }
        }
    } catch (Exception e) {
        logger.fatal(name + ": outer Exception" + state.counter, e);
    } finally {
        firstLock.unlock();
    }
}
 
开发者ID:hazelcast,项目名称:hazelcast-simulator,代码行数:36,代码来源:TxnQueueWithLockTest.java

示例3: remove

import com.hazelcast.transaction.TransactionContext; //导入方法依赖的package包/类
/**
 * This method performs transactional operation on removing the {@code exchange}
 * from the operational storage and moving it into the persistent one if the {@link HazelcastAggregationRepository}
 * runs in recoverable mode and {@code optimistic} is false. It will act at <u>your own</u> risk otherwise.
 * @param camelContext   the current CamelContext
 * @param key            the correlation key
 * @param exchange       the exchange to remove
 */
@Override
public void remove(CamelContext camelContext, String key, Exchange exchange) {
    DefaultExchangeHolder holder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders);
    if (optimistic) {
        LOG.trace("Removing an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(), key);
        if (!cache.remove(key, holder)) {
            LOG.error("Optimistic locking failed for exchange with key {}: IMap#remove removed no Exchanges, while it's expected to remove one.",
                    key);
            throw new OptimisticLockingException();
        }
        LOG.trace("Removed an exchange with ID {} for key {} in an optimistic manner.", exchange.getExchangeId(), key);
        if (useRecovery) {
            LOG.trace("Putting an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.",
                    exchange.getExchangeId(), key);
            persistedCache.put(exchange.getExchangeId(), holder);
            LOG.trace("Put an exchange with ID {} for key {} into a recoverable storage in an optimistic manner.",
                    exchange.getExchangeId(), key);
        }
    } else {
        if (useRecovery) {
            LOG.trace("Removing an exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
            // The only considerable case for transaction usage is fault tolerance:
            // the transaction will be rolled back automatically (default timeout is 2 minutes)
            // if no commit occurs during the timeout. So we are still consistent whether local node crashes.
            TransactionOptions tOpts = new TransactionOptions();

            tOpts.setTransactionType(TransactionOptions.TransactionType.LOCAL);
            TransactionContext tCtx = hzInstance.newTransactionContext(tOpts);

            try {
                tCtx.beginTransaction();

                TransactionalMap<String, DefaultExchangeHolder> tCache = tCtx.getMap(cache.getName());
                TransactionalMap<String, DefaultExchangeHolder> tPersistentCache = tCtx.getMap(persistedCache.getName());

                DefaultExchangeHolder removedHolder = tCache.remove(key);
                LOG.trace("Putting an exchange with ID {} for key {} into a recoverable storage in a thread-safe manner.",
                        exchange.getExchangeId(), key);
                tPersistentCache.put(exchange.getExchangeId(), removedHolder);

                tCtx.commitTransaction();
                LOG.trace("Removed an exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key);
                LOG.trace("Put an exchange with ID {} for key {} into a recoverable storage in a thread-safe manner.",
                        exchange.getExchangeId(), key);
            } catch (Throwable throwable) {
                tCtx.rollbackTransaction();

                final String msg = String.format("Transaction with ID %s was rolled back for remove operation with a key %s and an Exchange ID %s.",
                        tCtx.getTxnId(), key, exchange.getExchangeId());
                LOG.warn(msg, throwable);
                throw new RuntimeException(msg, throwable);
            }
        } else {
            cache.remove(key);
        }
    }
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:66,代码来源:HazelcastAggregationRepository.java

示例4: run

import com.hazelcast.transaction.TransactionContext; //导入方法依赖的package包/类
@Override
@SuppressWarnings("PMD.PreserveStackTrace")
public void run() {
    TransactionOptions options = new TransactionOptions()
            .setTransactionType(transactionType)
            .setDurability(durability);

    while (!testContext.isStopped()) {
        TransactionContext context = null;

        final int key = random.nextInt(keyCount);
        final long increment = random.nextInt(100);
        try {
            context = targetInstance.newTransactionContext(options);
            context.beginTransaction();

            final TransactionalMap<Integer, Long> map = context.getMap(name);

            Long current = map.getForUpdate(key);
            Long update = current + increment;
            map.put(key, update);

            context.commitTransaction();

            // Do local increments if commit is successful, so there is no needed decrement operation
            localIncrements[key] += increment;
            count.committed++;
        } catch (Exception commitFailedException) {
            if (context != null) {
                try {
                    logger.warn(name + ": commit failed key=" + key + " inc=" + increment, commitFailedException);
                    if (rethrowAllException) {
                        throw rethrow(commitFailedException);
                    }

                    context.rollbackTransaction();
                    count.rolled++;
                } catch (Exception rollBackFailedException) {
                    logger.warn(name + ": rollback failed key=" + key + " inc=" + increment,
                            rollBackFailedException);
                    count.failedRollbacks++;

                    if (rethrowRollBackException) {
                        throw rethrow(rollBackFailedException);
                    }
                }
            }
        }
    }
    targetInstance.getList(name + "res").add(localIncrements);
    targetInstance.getList(name + "report").add(count);
}
 
开发者ID:hazelcast,项目名称:hazelcast-simulator,代码行数:53,代码来源:MapTransactionGetForUpdateTest.java

示例5: run

import com.hazelcast.transaction.TransactionContext; //导入方法依赖的package包/类
private static void run() {

        Config config = new Config("queueTest");

        QueueConfig queueConfig = config.getQueueConfig(QNAME);

        QueueStoreConfig queueStoreConfig = new QueueStoreConfig();
        queueStoreConfig.setEnabled(true);
        queueStoreConfig.setStoreImplementation(new MockQueueStore());
        queueStoreConfig.getProperties().setProperty("memory-limit", "0");

        queueConfig.setQueueStoreConfig(queueStoreConfig);

        HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance(config);

        long startTime = System.currentTimeMillis();

        int i = 0;
        while (i++ < 2000000) {
            if (i % 10000 == 0) {
                logger.info(Integer.toString(i) + "\t" + String.format("%8.3f", (double) (System.currentTimeMillis() -
                        startTime) / i));
            }

            TransactionOptions options = new TransactionOptions().setTransactionType(TransactionOptions.TransactionType.LOCAL);

            TransactionContext context = hzInstance.newTransactionContext(options);
            context.beginTransaction();

            TransactionalQueue<Integer> queue = context.getQueue(QNAME);
            queue.offer(i);

            context.commitTransaction();

        }
    }
 
开发者ID:romario13,项目名称:hz-queue,代码行数:37,代码来源:Test3.java


注:本文中的com.hazelcast.transaction.TransactionContext.commitTransaction方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。