Uncommitted data within the scope of a transaction cannot be read within the same thread(2.8.1)

classic Classic list List threaded Threaded
2 messages Options
38797715 38797715
Reply | Threaded
Open this post in threaded view
|

Uncommitted data within the scope of a transaction cannot be read within the same thread(2.8.1)

Hi guys,

If you execute the following code, you will find that cache.iterator()
does not return a result. If the transaction is not started, the
returned result is correct.
Is this a bug or a known technical limitation?

server side just start ignite.sh

public class CacheTransactionExample {

     public static void main(String[] args) throws IgniteException {
         Ignition.setClientMode(true);
         try (Ignite ignite = Ignition.start()) {
             System.out.println();

             CacheConfiguration<Integer, Account> cfg = new
CacheConfiguration<>("Account");

cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
             IgniteCache<Integer, Account> cache =
ignite.getOrCreateCache(cfg);

             try (Transaction tx =
ignite.transactions().txStart(OPTIMISTIC, READ_COMMITTED)) {
                 cache.put(1, new Account(1, 100));
                 cache.put(2, new Account(1, 200));

                 System.out.println();
                 System.out.println(">>> " + cache.get(1));
                 System.out.println(">>> " + cache.get(2));

                 Iterator it = cache.iterator();
                 while(it.hasNext()) {
                     Cache.Entry<Integer, Account> acc =
(Cache.Entry<Integer, Account>)it.next();
                     System.out.println("<<<" + acc.getValue());
                 }
                 tx.commit();
             }
             finally {
                 ignite.destroyCache("Account");
             }
         }
     }

     private static class Account implements Serializable {
         private int id;

         private double balance;

         Account(int id, double balance) {
             this.id = id;
             this.balance = balance;
         }

         void update(double amount) {
             balance += amount;
         }

         @Override public String toString() {
             return "Account [id=" + id + ", balance=$" + balance + ']';
         }
     }
}

ilya.kasnacheev ilya.kasnacheev
Reply | Threaded
Open this post in threaded view
|

Re: Uncommitted data within the scope of a transaction cannot be read within the same thread(2.8.1)

Hello!

I think that cache.iterator() is basically a scan query, and queries (such as SqlFieldsQuery or ContinuousQuery) do not honor transaction guarantees.

Regards,
--
Ilya Kasnacheev


чт, 20 авг. 2020 г. в 04:28, 38797715 <[hidden email]>:
Hi guys,

If you execute the following code, you will find that cache.iterator()
does not return a result. If the transaction is not started, the
returned result is correct.
Is this a bug or a known technical limitation?

server side just start ignite.sh

public class CacheTransactionExample {

     public static void main(String[] args) throws IgniteException {
         Ignition.setClientMode(true);
         try (Ignite ignite = Ignition.start()) {
             System.out.println();

             CacheConfiguration<Integer, Account> cfg = new
CacheConfiguration<>("Account");

cfg.setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL);
             IgniteCache<Integer, Account> cache =
ignite.getOrCreateCache(cfg);

             try (Transaction tx =
ignite.transactions().txStart(OPTIMISTIC, READ_COMMITTED)) {
                 cache.put(1, new Account(1, 100));
                 cache.put(2, new Account(1, 200));

                 System.out.println();
                 System.out.println(">>> " + cache.get(1));
                 System.out.println(">>> " + cache.get(2));

                 Iterator it = cache.iterator();
                 while(it.hasNext()) {
                     Cache.Entry<Integer, Account> acc =
(Cache.Entry<Integer, Account>)it.next();
                     System.out.println("<<<" + acc.getValue());
                 }
                 tx.commit();
             }
             finally {
                 ignite.destroyCache("Account");
             }
         }
     }

     private static class Account implements Serializable {
         private int id;

         private double balance;

         Account(int id, double balance) {
             this.id = id;
             this.balance = balance;
         }

         void update(double amount) {
             balance += amount;
         }

         @Override public String toString() {
             return "Account [id=" + id + ", balance=$" + balance + ']';
         }
     }
}