h2/src/test/org/h2/test/store/TestTransactionStore.java
/**
     * Tests behavior when used for a sequence of SQL statements. Each statement
     * uses a savepoint. Within a statement, changes by the statement itself are
     * not seen; the change is only seen when the statement finished.
     * <p>
     * Update statements that change the key of multiple rows may use delete/add
     * pairs to do so (they don't need to first delete all entries and then
     * re-add them). Trying to add multiple values for the same key is not
     * allowed (an update statement that would result in a duplicate key).
     */
Tests behavior when used for a sequence of SQL statements. Each statement
uses a savepoint. Within a statement, changes by the statement itself are
not seen; the change is only seen when the statement finished.

Update statements that change the key of multiple rows may use delete/add
pairs to do so (they don't need to first delete all entries and then
re-add them). Trying to add multiple values for the same key is not
allowed (an update statement that would result in a duplicate key).
Tests behavior when used for a sequence of SQL statements . Each statement uses a savepoint . Within a statement changes by the statement itself are not seen ; the change is only seen when the statement finished . Update statements that change the key of multiple rows may use delete / add pairs to do so ( they don ' t need to first delete all entries and then re - add them ) . Trying to add multiple values for the same key is not allowed ( an update statement that would result in a duplicate key ) .
private void testMultiStatement() {
        MVStore s = MVStore.open(null);
        TransactionStore ts = new TransactionStore(s);
        Transaction tx;
        TransactionMap<String, String> m;
        long startUpdate;
        tx = ts.begin();
        startUpdate = tx.setSavepoint();
        m = tx.openMap("test");
        m.setSavepoint(startUpdate);
        startUpdate = tx.setSavepoint();
        m.setSavepoint(startUpdate);
        assertTrue(m.trySet("1", "Hello", true));
        assertTrue(m.trySet("2", "World", true));
        assertNull(m.get("1"));
        assertNull(m.get("2"));
        startUpdate = tx.setSavepoint();
        m.setSavepoint(startUpdate);
        assertEquals("Hello", m.get("1"));
        assertEquals("World", m.get("2"));
        assertEquals("Hello", m.get("1"));
        assertTrue(m.trySet("1", null, true));
        assertTrue(m.trySet("2", "Hello", true));
        assertEquals("World", m.get("2"));
        assertTrue(m.trySet("2", null, true));
        assertTrue(m.trySet("3", "World", true));
        assertEquals("Hello", m.get("1"));
        assertEquals("World", m.get("2"));
        assertNull(m.get("3"));
        startUpdate = tx.setSavepoint();
        m.setSavepoint(startUpdate);
        assertNull(m.get("1"));
        assertEquals("Hello", m.get("2"));
        assertEquals("World", m.get("3"));
        startUpdate = tx.setSavepoint();
        m.setSavepoint(startUpdate);
        assertTrue(m.trySet("2", null, true));
        assertTrue(m.trySet("1", "Hello", true));
        assertTrue(m.trySet("3", null, true));
        assertFalse(m.trySet("1", "World", true));
        tx.rollbackToSavepoint(startUpdate);
        startUpdate = tx.setSavepoint();
        m.setSavepoint(startUpdate);
        assertNull(m.get("1"));
        assertEquals("Hello", m.get("2"));
        assertEquals("World", m.get("3"));
        tx.commit();
        ts.close();
        s.close();
    }
private void testMultiStatement ( ) { MVStore s = MVStore . open ( null ) ; TransactionStore ts = new TransactionStore ( s ) ; Transaction tx ; TransactionMap < String , String > m ; long startUpdate ; tx = ts . begin ( ) ; startUpdate = tx . setSavepoint ( ) ; m = tx . openMap ( "test" ) ; m . setSavepoint ( startUpdate ) ; startUpdate = tx . setSavepoint ( ) ; m . setSavepoint ( startUpdate ) ; assertTrue ( m . trySet ( "1" , "Hello" , true ) ) ; assertTrue ( m . trySet ( "2" , "World" , true ) ) ; assertNull ( m . get ( "1" ) ) ; assertNull ( m . get ( "2" ) ) ; startUpdate = tx . setSavepoint ( ) ; m . setSavepoint ( startUpdate ) ; assertEquals ( "Hello" , m . get ( "1" ) ) ; assertEquals ( "World" , m . get ( "2" ) ) ; assertEquals ( "Hello" , m . get ( "1" ) ) ; assertTrue ( m . trySet ( "1" , null , true ) ) ; assertTrue ( m . trySet ( "2" , "Hello" , true ) ) ; assertEquals ( "World" , m . get ( "2" ) ) ; assertTrue ( m . trySet ( "2" , null , true ) ) ; assertTrue ( m . trySet ( "3" , "World" , true ) ) ; assertEquals ( "Hello" , m . get ( "1" ) ) ; assertEquals ( "World" , m . get ( "2" ) ) ; assertNull ( m . get ( "3" ) ) ; startUpdate = tx . setSavepoint ( ) ; m . setSavepoint ( startUpdate ) ; assertNull ( m . get ( "1" ) ) ; assertEquals ( "Hello" , m . get ( "2" ) ) ; assertEquals ( "World" , m . get ( "3" ) ) ; startUpdate = tx . setSavepoint ( ) ; m . setSavepoint ( startUpdate ) ; assertTrue ( m . trySet ( "2" , null , true ) ) ; assertTrue ( m . trySet ( "1" , "Hello" , true ) ) ; assertTrue ( m . trySet ( "3" , null , true ) ) ; assertFalse ( m . trySet ( "1" , "World" , true ) ) ; tx . rollbackToSavepoint ( startUpdate ) ; startUpdate = tx . setSavepoint ( ) ; m . setSavepoint ( startUpdate ) ; assertNull ( m . get ( "1" ) ) ; assertEquals ( "Hello" , m . get ( "2" ) ) ; assertEquals ( "World" , m . get ( "3" ) ) ; tx . commit ( ) ; ts . close ( ) ; s . close ( ) ; }
Tests behavior when used for a sequence of SQL statements.
Tests behavior when used for a sequence of SQL statements .
// start of statement // create table test // start of statement // insert into test(id, name) values(1, 'Hello'), (2, 'World') // not seen yet (within the same statement) // start of statement // now we see the newest version // update test set primaryKey = primaryKey + 1 // (this is usually a tricky case) // already updated by this statement, so it has no effect // but still returns true because it was changed by this transaction // not seen within this statement // start of statement // select * from test // start of statement // update test set id = 1 // should fail: duplicate key