Exploring transactional mode

The IgniteTransactions interface defines the APIs to enable ACID-compliant semantics when working with caches. We can start a transaction by calling the tx*****(...) methods on the IgniteTransactions interface and work with one cache or across multiple caches.

Ignite transactions work on caches where the atomicity mode is CacheAtomicityMode.TRANSACTIONAL. By default, the atomicity mode is set to CacheAtomicityMode.ATOMIC.

The following example explores transactional behavior: 

  1. Create a class called TransactionHelloWorld:
      public class TransactionHelloWorld {
private static final String MY_ATOMIC_CACHE = "myAtomicCache";
private static final String MY_TRANSACTIONAL_CACHE =
"myTransactionalCache";

public static void main(String[] args) {
  1. Create a transactional cache config by setting the atomicity mode to CacheAtomicityMode.TRANSACTIONAL:
      CacheConfiguration<Long, String> myTransactionalCacheConfig = new       CacheConfiguration<Long, String>();
myTransactionalCacheConfig.setName(MY_TRANSACTIONAL_CACHE);
myTransactionalCacheConfig.setAtomicityMode
(CacheAtomicityMode.TRANSACTIONAL);
  1. Create an atomic cache config by setting the atomicity mode to CacheAtomicityMode.ATOMIC:
      CacheConfiguration<Long, String> myAtomicCacheConfig = 
new CacheConfiguration<Long, String>();
myAtomicCacheConfig.setName(MY_ATOMIC_CACHE);
myAtomicCacheConfig.setAtomicityMode(CacheAtomicityMode.ATOMIC);
  1. Set both the cache configurations:
       IgniteConfiguration cfg = new IgniteConfiguration();
cfg.setCacheConfiguration(myTransactionalCacheConfig,
myAtomicCacheConfig);
  1. Start Ignite:
       Ignite ignite = Ignition.start(cfg);
  1. Get the transactional cache:
       IgniteCache<Long, String> txCache = 
ignite.getOrCreateCache(MY_TRANSACTIONAL_CACHE);
  1. Get the atomic cache:
       IgniteCache<Long, String> atomicCache = 
ignite.getOrCreateCache(MY_ATOMIC_CACHE);
  1. Get an instance of IgniteTransactions and start a transaction with a default isolation, concurrency, timeout, and invalidation policy:
       IgniteTransactions transactions = ignite.transactions();
Transaction tx = transactions.txStart();
  1. Within the try block, first put two values in the transactional cache and then in the atomic cache, immediately throw a runtime exception. In the cache block, catch the runtime exception and roll back the on-going transaction. It will roll back the txCache operations wrapped inside the transaction:

try {
txCache.put(2l, "Value1");
txCache.put(3l, "Value2");

atomicCache.put(2l, "Value1");
atomicCache.put(3l, "Value2");
throw new RuntimeException("Failing the tx");
} catch (Exception e) {
System.out.println("rolling back transaction");
tx.rollback();
} finally {
tx.close();
}
  1. Now look up the keys in the transactional cache as well as in the atomic cache. The transactional cache should not return any values, as the tx was rolled back:
       System.out.println("key 2 in Tx Cache = " 
+ txCache.get(2l));
System.out.println("key 3 in Tx Cache = "
+ txCache.get(3l));
  1. The atomic cache should not have any effect on tx.rollback(). It should return these values:
       System.out.println("key 2 in Atomic Cache = " 
+ atomicCache.get(2l));
System.out.println("key 3 in Atomic Cache = "
+ atomicCache.get(3l));
}
}
  1. When we run the program, it prints the following output:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.50.156