You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by bingili <bi...@illinois.edu> on 2017/09/20 20:04:01 UTC

exception in transaction, how to rollback all put?

In the code, tx.commit() is not reached. Is there a way to rollback the
cache.put("test1", "1"); cache.put("test2", "2");

        

            public IgniteTest() {
		CacheConfiguration cacheCfg = new CacheConfiguration();
		cacheCfg.setName("cacheName");
		cacheCfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);

		IgniteConfiguration cfg = new IgniteConfiguration();
		cfg.setCacheConfiguration(cacheCfg);
		
		TransactionConfiguration txCfg = new TransactionConfiguration();
		cfg.setTransactionConfiguration(txCfg);
		
		cfg.setClientMode(true);
		Ignite ignite = Ignition.start(cfg);
		transactions = ignite.transactions();
		cache = ignite.getOrCreateCache("txn test cache");
	}
	
	public void run() {
		Transaction tx = transactions.txStart(TransactionConcurrency.OPTIMISTIC,
                TransactionIsolation.SERIALIZABLE);
		try{
			  
		    cache.put("test1", "1");
		    cache.put("test2", "2");
		    
		    String[] bug = null;
		    bug[0] = "hello";
		    
		    cache.put("test3", "3");
		    System.out.println("tx.commit");
		    tx.commit();
		}catch (Exception ex) {
			ex.printStackTrace();
			System.out.println("tx.rollback");
			tx.rollback();
		}
	}



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

Re: exception in transaction, how to rollback all put?

Posted by Alexandr Kuramshin <ei...@gmail.com>.
Hi,

you are doing wrong.

cache = ignite.getOrCreateCache("txn test cache");

creates a new atomic cache, because you configure another cache as
transactional

cacheCfg.setName("cacheName");


2017-09-21 8:44 GMT+07:00 bingili <bi...@illinois.edu>:

> the test.print() before test.run() will printout three nulls, which is
> correct.
> However, after test.run(), the print in finally block will print out: 1 2
> null, which means cache.put("test1", "1"); cache.put("test2", "2"); are
> successfully written into cache, even though the exception happens. I
> thought the tx.rollback() should revert back the values of "test1" and
> "test2". It would expect to print out 3 nulls after tx.rollback().
>
>
> null
> null
> null
> --------
> java.lang.NullPointerException
>         at test.IgniteTest.run(IgniteTest.java:48)
>         at test.IgniteTest.main(IgniteTest.java:71)
> tx.rollback
> 1
> 2
> null
>
>         public void print() {
>                 System.out.println(cache.get("test1"));
>                 System.out.println(cache.get("test2"));
>                 System.out.println(cache.get("test3"));
>         }
>
>         public static void main(String[] args) {
>                 IgniteTest test = new IgniteTest();
>                 test.print();
>                 System.out.println("--------");
>                 try {
>                         test.run();
>                 } catch (Exception ex) {
>
>                 } finally {
>                         test.print();
>                 }
>         }
>
>
>
> --
> Sent from: http://apache-ignite-users.70518.x6.nabble.com/
>



-- 
Thanks,
Alexandr Kuramshin

Re: exception in transaction, how to rollback all put?

Posted by bingili <bi...@illinois.edu>.
the test.print() before test.run() will printout three nulls, which is
correct. 
However, after test.run(), the print in finally block will print out: 1 2
null, which means cache.put("test1", "1"); cache.put("test2", "2"); are
successfully written into cache, even though the exception happens. I
thought the tx.rollback() should revert back the values of "test1" and
"test2". It would expect to print out 3 nulls after tx.rollback().
		    

null
null
null
--------
java.lang.NullPointerException
	at test.IgniteTest.run(IgniteTest.java:48)
	at test.IgniteTest.main(IgniteTest.java:71)
tx.rollback
1
2
null

	public void print() {
		System.out.println(cache.get("test1"));
		System.out.println(cache.get("test2"));
		System.out.println(cache.get("test3"));
	}
	
	public static void main(String[] args) {
		IgniteTest test = new IgniteTest();
		test.print();
		System.out.println("--------");
		try {
			test.run();
		} catch (Exception ex) {
			
		} finally {
			test.print();
		}
	}



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/

Re: exception in transaction, how to rollback all put?

Posted by vkulichenko <va...@gmail.com>.
Your code looks correct, tx.rollback() will rollback the whole transaction.
Is there anything in particular that is not working?

-Val



--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/