本文整理汇总了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();
}
}
示例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();
}
}
示例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);
}
}
}
示例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);
}
示例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();
}
}