package doc;

import org.mapdb.*;


public class concurrency_consistency_lock {

    public static void main(String[] args) {
        //a
        DB db = DBMaker.memoryDB().make();

        // there are two counters which needs to be incremented at the same time.
        Atomic.Long a = db.atomicLong("a");
        Atomic.Long b = db.atomicLong("b");


        // update those two counters together
        db.consistencyLock().readLock().lock(); //note readLock
        try{
            a.incrementAndGet();
            // if snapshot or commit would happen here, two counters would be inconsistent
            b.incrementAndGet();
        }finally {
            db.consistencyLock().readLock().unlock();
        }

        //now backup two counters (simulates taking snapshot)
        db.consistencyLock().readLock().lock();  //not writeLock
        try{
            System.out.println(
                    a.get() + " = " + b.get()
            );
        }finally {
            db.consistencyLock().readLock().unlock();
        }
        //z
    }
}

@@ -522,61 +522,115 @@
  // 
-if snapshot or commit would happen here, two counters
+'a' is incremented, 'b' not yet. If commit or rollback would happen here%0A            // data stored on disk
  wou
@@ -634,16 +634,20 @@
 would be
+come
  inconsi
@@ -651,16 +651,17 @@
 nsistent
+.
 %0A       

Update comment