hamidhtc / h2database

Automatically exported from code.google.com/p/h2database
0 stars 0 forks source link

[MVStore] java.sql.Connection.setAutoCommit(false) & Savepoint do not work #458

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
H2 Version: svn trunk

Test:
==========================================
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Savepoint;
import java.sql.Statement;

import junit.framework.Assert;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class TransactionTest {
    protected static Connection conn;
    protected static Statement stmt;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        conn = DriverManager.getConnection("jdbc:h2:tcp://localhost:9092/mydb", "sa", "");
        stmt = conn.createStatement();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        if (stmt != null)
            stmt.close();
        if (conn != null)
            conn.close();
    }

    @Test
    public void testRegularTable() throws Exception {
        run(null); //all tests pass
    }

    @Test
    public void testMVTable() throws Exception {
        run("org.h2.mvstore.db.MVTableEngine");
    }

    private void run(String engine) throws Exception {
        stmt.executeUpdate("DROP TABLE IF EXISTS TransactionTest");
        String sql = "CREATE TABLE IF NOT EXISTS TransactionTest(id int, name varchar(20))";
        if (engine != null)
            sql += " ENGINE \"" + engine + "\"";

        stmt.executeUpdate(sql);

        conn.setAutoCommit(false);
        stmt.executeUpdate("insert into TransactionTest(id, name) values(10, 'a')");
        stmt.executeUpdate("insert into TransactionTest(id, name) values(20, 'b')");

        count(2); //MVTable failed
        selectAll(); //MVTable failed

        conn.commit();

        count(2);
        selectAll();

        stmt.executeUpdate("insert into TransactionTest(id, name) values(30, 'c')");
        Savepoint sp = conn.setSavepoint();
        stmt.executeUpdate("insert into TransactionTest(id, name) values(40, 'd')");
        conn.rollback(sp);

        count(3); //MVTable failed
    }

    private void count(int expected) throws Exception {
        ResultSet rs = stmt.executeQuery("select count(*) from TransactionTest");
        rs.next();
        Assert.assertEquals(expected, rs.getInt(1));
        rs.close();
    }

    private void selectAll() throws Exception {
        ResultSet rs = stmt.executeQuery("select * from TransactionTest");
        int n = rs.getMetaData().getColumnCount();
        Assert.assertEquals(2, n);
        Assert.assertTrue(rs.next());
        do {
            for (int i = 1; i <= n; i++) {
                System.out.print(rs.getString(i) + " ");
            }
            System.out.println();
        } while (rs.next());
        rs.close();
    }
}

Original issue reported on code.google.com by zhh200...@gmail.com on 8 May 2013 at 10:29

GoogleCodeExporter commented 9 years ago
Thanks for the test case!

The main problem is that a rollback (partial or complete) doesn't update the 
'last modified' counter of the table. This needs to be implemented first. I'm 
currently working on it.

Regards,
Thomas

Original comment by thomas.t...@gmail.com on 14 May 2013 at 7:41

GoogleCodeExporter commented 9 years ago
Fixed in version 1.3.172.

Original comment by thomas.t...@gmail.com on 25 May 2013 at 1:51