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 CacheJdbcBlobStore method load.
/** {@inheritDoc} */
@SuppressWarnings({ "RedundantTypeArguments" })
@Override
public V load(K key) {
init();
Transaction tx = transaction();
if (log.isDebugEnabled())
log.debug("Store load [key=" + key + ", tx=" + tx + ']');
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = connection(tx);
stmt = conn.prepareStatement(loadQry);
stmt.setObject(1, toBytes(key));
ResultSet rs = stmt.executeQuery();
if (rs.next())
return fromBytes(rs.getBytes(2));
} catch (IgniteCheckedException | SQLException e) {
throw new CacheLoaderException("Failed to load object: " + key, e);
} finally {
end(tx, conn, stmt);
}
return null;
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class CacheJdbcBlobStore method delete.
/** {@inheritDoc} */
@Override
public void delete(Object key) {
init();
Transaction tx = transaction();
if (log.isDebugEnabled())
log.debug("Store remove [key=" + key + ", tx=" + tx + ']');
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = connection(tx);
stmt = conn.prepareStatement(delQry);
stmt.setObject(1, toBytes(key));
stmt.executeUpdate();
} catch (IgniteCheckedException | SQLException e) {
throw new CacheWriterException("Failed to remove object: " + key, e);
} finally {
end(tx, conn, stmt);
}
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class GridCacheMarshallingNodeJoinSelfTest method testNodeJoin.
/**
* @throws Exception If failed.
*/
public void testNodeJoin() throws Exception {
final CountDownLatch allowJoin = new CountDownLatch(1);
final CountDownLatch joined = new CountDownLatch(2);
for (int i = 0; i < 2; i++) {
ignite(i).events().localListen(new PE() {
@Override
public boolean apply(Event evt) {
assert evt.type() == EventType.EVT_NODE_JOINED;
info(">>> Event: " + evt);
joined.countDown();
return true;
}
}, EventType.EVT_NODE_JOINED);
}
IgniteInternalFuture<?> oneMoreGrid = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
allowJoin.await();
startGrid("oneMoreGrid");
return null;
}
}, 1);
IgniteCache<Integer, TestObject> cache = ignite(0).cache(DEFAULT_CACHE_NAME);
try (Transaction tx = ignite(0).transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
cache.get(0);
allowJoin.countDown();
joined.await();
assertNotNull(cache.get(1));
tx.commit();
}
oneMoreGrid.get();
assertNotNull(cache.get(1));
}
use of org.apache.ignite.transactions.Transaction in project ignite by apache.
the class GridCacheMultiUpdateLockSelfTest method checkMultiUpdateLocks.
/**
* @param nearEnabled Near enabled flag.
* @throws Exception If failed.
*/
private void checkMultiUpdateLocks(boolean nearEnabled) throws Exception {
this.nearEnabled = nearEnabled;
startGrids(3);
try {
IgniteKernal g = (IgniteKernal) grid(0);
GridCacheContext<Object, Object> cctx = g.internalCache(DEFAULT_CACHE_NAME).context();
GridDhtCacheAdapter cache = nearEnabled ? cctx.near().dht() : cctx.colocated();
AffinityTopologyVersion topVer = cache.beginMultiUpdate();
IgniteInternalFuture<?> startFut;
try {
assertEquals(3, topVer.topologyVersion());
final AtomicBoolean started = new AtomicBoolean();
startFut = multithreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
info(">>>> Starting grid.");
Ignite g4 = startGrid(4);
started.set(true);
IgniteCache<Object, Object> c = g4.cache(DEFAULT_CACHE_NAME);
info(">>>> Checking tx in new grid.");
try (Transaction tx = g4.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
assertEquals(2, c.get("a"));
assertEquals(4, c.get("b"));
assertEquals(6, c.get("c"));
}
return null;
}
}, 1);
U.sleep(200);
info(">>>> Checking grid has not started yet.");
assertFalse(started.get());
// Check we can proceed with transactions.
IgniteCache<Object, Object> cache0 = g.cache(DEFAULT_CACHE_NAME);
info(">>>> Checking tx commit.");
Transaction tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
try {
cache0.put("a", 1);
cache0.put("b", 2);
cache0.put("c", 3);
tx.commit();
} finally {
tx.close();
}
info(">>>> Checking grid still is not started");
assertFalse(started.get());
tx = g.transactions().txStart(PESSIMISTIC, REPEATABLE_READ);
try {
cache0.put("a", 2);
cache0.put("b", 4);
cache0.put("c", 6);
tx.commit();
} finally {
tx.close();
}
} finally {
info(">>>> Releasing multi update.");
cache.endMultiUpdate();
}
info("Waiting for thread termination.");
startFut.get();
} finally {
stopAllGrids();
}
}
Aggregations