use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class SpringTransactionManager method doRollback.
/**
* {@inheritDoc}
*/
@Override
protected void doRollback(DefaultTransactionStatus status) throws TransactionException {
IgniteTransactionObject txObj = (IgniteTransactionObject) status.getTransaction();
Transaction tx = txObj.getTransactionHolder().getTransaction();
if (status.isDebug() && log.isDebugEnabled())
log.debug("Rolling back Ignite transaction: " + tx);
try {
tx.rollback();
} catch (IgniteException e) {
throw new TransactionSystemException("Could not rollback Ignite transaction", e);
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class SpringTransactionManager method doBegin.
/**
* {@inheritDoc}
*/
@Override
protected void doBegin(Object transaction, TransactionDefinition definition) throws TransactionException {
if (definition.getIsolationLevel() == TransactionDefinition.ISOLATION_READ_UNCOMMITTED)
throw new InvalidIsolationLevelException("Ignite does not support READ_UNCOMMITTED isolation level.");
IgniteTransactionObject txObj = (IgniteTransactionObject) transaction;
Transaction tx = null;
try {
if (txObj.getTransactionHolder() == null || txObj.getTransactionHolder().isSynchronizedWithTransaction()) {
long timeout = ignite.configuration().getTransactionConfiguration().getDefaultTxTimeout();
if (definition.getTimeout() > 0)
timeout = TimeUnit.SECONDS.toMillis(definition.getTimeout());
Transaction newTx = ignite.transactions().txStart(transactionConcurrency, convertToIgniteIsolationLevel(definition.getIsolationLevel()), timeout, 0);
if (log.isDebugEnabled())
log.debug("Started Ignite transaction: " + newTx);
txObj.setTransactionHolder(new IgniteTransactionHolder(newTx), true);
}
txObj.getTransactionHolder().setSynchronizedWithTransaction(true);
txObj.getTransactionHolder().setTransactionActive(true);
tx = txObj.getTransactionHolder().getTransaction();
// Bind the session holder to the thread.
if (txObj.isNewTransactionHolder())
TransactionSynchronizationManager.bindResource(this.ignite, txObj.getTransactionHolder());
} catch (Exception ex) {
if (tx != null)
tx.close();
throw new CannotCreateTransactionException("Could not create Ignite transaction", ex);
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheHibernateStoreExample method executeTransaction.
/**
* Executes transaction with read/write-through to persistent store.
*
* @param cache Cache to execute transaction on.
*/
private static void executeTransaction(IgniteCache<Long, Person> cache) {
try (Transaction tx = Ignition.ignite().transactions().txStart()) {
Person val = cache.get(id);
System.out.println("Read value: " + val);
val = cache.getAndPut(id, new Person(id, "Isaac", "Newton"));
System.out.println("Overwrote old value: " + val);
val = cache.get(id);
System.out.println("Read value: " + val);
tx.commit();
}
System.out.println("Read value after commit: " + cache.get(id));
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class IgniteDiagnosticMessagesTest method testSeveralLongRunningTxs.
/**
* @throws Exception If failed.
*/
public void testSeveralLongRunningTxs() throws Exception {
int timeout = 3500;
System.setProperty(IGNITE_LONG_OPERATIONS_DUMP_TIMEOUT, String.valueOf(timeout));
try {
testSpi = true;
startGrid(0);
GridStringLogger strLog = this.strLog = new GridStringLogger();
strLog.logLength(1024 * 100);
startGrid(1);
awaitPartitionMapExchange();
CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
ccfg.setWriteSynchronizationMode(FULL_SYNC);
ccfg.setCacheMode(PARTITIONED);
ccfg.setAtomicityMode(TRANSACTIONAL);
final Ignite node0 = ignite(0);
final Ignite node1 = ignite(1);
node0.createCache(ccfg);
UUID id0 = node0.cluster().localNode().id();
TestRecordingCommunicationSpi.spi(node0).blockMessages(GridNearLockResponse.class, node1.name());
IgniteCache<Object, Object> cache = node0.cache(DEFAULT_CACHE_NAME);
int txCnt = 4;
final List<Integer> keys = primaryKeys(cache, txCnt, 0);
final AtomicInteger idx = new AtomicInteger();
IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
IgniteCache<Object, Object> cache = node1.cache(DEFAULT_CACHE_NAME);
try (Transaction tx = node1.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
Integer key = keys.get(idx.getAndIncrement() % keys.size());
cache.putIfAbsent(key, String.valueOf(key));
tx.commit();
}
return null;
}
}, txCnt * 2, "tx");
U.sleep(timeout * 2);
assertFalse(fut.isDone());
TestRecordingCommunicationSpi.spi(node0).stopBlock();
fut.get();
String log = strLog.toString();
assertTrue(log.contains("Cache entries [cacheId=" + CU.cacheId(DEFAULT_CACHE_NAME) + ", cacheName=" + DEFAULT_CACHE_NAME + "]:"));
assertTrue(countTxKeysInASingleBlock(log) == txCnt);
assertTrue(log.contains("General node info [id=" + id0));
} finally {
System.clearProperty(IGNITE_LONG_OPERATIONS_DUMP_TIMEOUT);
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class GridCacheDaemonNodeAbstractSelfTest method testExplicit.
/**
* @throws Exception If failed.
*/
public void testExplicit() throws Exception {
try {
startGridsMultiThreaded(3);
daemon = true;
startGrid(4);
IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
for (int i = 0; i < 30; i++) {
try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.put(i, i);
tx.commit();
}
}
Map<Integer, Integer> batch = new HashMap<>();
for (int i = 30; i < 60; i++) batch.put(i, i);
try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.putAll(batch);
tx.commit();
}
for (int i = 0; i < 60; i++) assertEquals(i, (int) cache.get(i));
} finally {
stopAllGrids();
}
}
Aggregations