Search in sources :

Example 16 with Savepoint

use of java.sql.Savepoint in project h2database by h2database.

the class TestMVTableEngine method testAutoCommit.

private void testAutoCommit() throws SQLException {
    Connection conn;
    Statement stat;
    ResultSet rs;
    deleteDb(getTestName());
    conn = getConnection(getTestName() + ";MV_STORE=TRUE");
    for (int i = 0; i < 2; i++) {
        stat = conn.createStatement();
        stat.execute("create table test(id int primary key, name varchar)");
        stat.execute("create index on test(name)");
        conn.setAutoCommit(false);
        stat.execute("insert into test values(1, 'Hello')");
        stat.execute("insert into test values(2, 'World')");
        rs = stat.executeQuery("select count(*) from test");
        rs.next();
        assertEquals(2, rs.getInt(1));
        conn.rollback();
        rs = stat.executeQuery("select count(*) from test");
        rs.next();
        assertEquals(0, rs.getInt(1));
        stat.execute("insert into test values(1, 'Hello')");
        Savepoint sp = conn.setSavepoint();
        stat.execute("insert into test values(2, 'World')");
        conn.rollback(sp);
        rs = stat.executeQuery("select count(*) from test");
        rs.next();
        assertEquals(1, rs.getInt(1));
        stat.execute("drop table test");
    }
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) Savepoint(java.sql.Savepoint) Savepoint(java.sql.Savepoint)

Example 17 with Savepoint

use of java.sql.Savepoint in project h2database by h2database.

the class JdbcConnection method setSavepoint.

/**
 * Creates a new unnamed savepoint.
 *
 * @return the new savepoint
 */
@Override
public Savepoint setSavepoint() throws SQLException {
    try {
        int id = getNextId(TraceObject.SAVEPOINT);
        if (isDebugEnabled()) {
            debugCodeAssign("Savepoint", TraceObject.SAVEPOINT, id, "setSavepoint()");
        }
        checkClosed();
        CommandInterface set = prepareCommand("SAVEPOINT " + JdbcSavepoint.getName(null, savepointId), Integer.MAX_VALUE);
        set.executeUpdate(false);
        JdbcSavepoint savepoint = new JdbcSavepoint(this, savepointId, null, trace, id);
        savepointId++;
        return savepoint;
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : CommandInterface(org.h2.command.CommandInterface) Savepoint(java.sql.Savepoint) DbException(org.h2.message.DbException) SQLClientInfoException(java.sql.SQLClientInfoException) SQLException(java.sql.SQLException)

Example 18 with Savepoint

use of java.sql.Savepoint in project h2database by h2database.

the class TestMvcc3 method testInsertUpdateRollback.

private void testInsertUpdateRollback() throws SQLException {
    if (!config.mvcc) {
        return;
    }
    deleteDb("mvcc3");
    Connection c1 = getConnection("mvcc3");
    Statement s1 = c1.createStatement();
    Connection c2 = getConnection("mvcc3");
    Statement s2 = c2.createStatement();
    s1.execute("create table test(id int primary key, name varchar) " + "as select 0, 'Hello'");
    c1.setAutoCommit(false);
    s1.executeUpdate("update test set name = 'World'");
    printRows("after update", s1, s2);
    Savepoint sp1 = c1.setSavepoint();
    s1.executeUpdate("delete from test");
    printRows("after delete", s1, s2);
    c1.rollback(sp1);
    printRows("after rollback delete", s1, s2);
    c1.rollback();
    printRows("after rollback all", s1, s2);
    ResultSet rs = s2.executeQuery("select * from test");
    assertTrue(rs.next());
    assertFalse(rs.next());
    c1.close();
    c2.close();
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) Savepoint(java.sql.Savepoint)

Example 19 with Savepoint

use of java.sql.Savepoint in project h2database by h2database.

the class TestCancel method testQueryTimeoutInTransaction.

private void testQueryTimeoutInTransaction() throws SQLException {
    deleteDb("cancel");
    Connection conn = getConnection("cancel");
    Statement stat = conn.createStatement();
    stat.execute("CREATE TABLE TEST(ID INT)");
    conn.setAutoCommit(false);
    stat.execute("INSERT INTO TEST VALUES(1)");
    Savepoint sp = conn.setSavepoint();
    stat.execute("INSERT INTO TEST VALUES(2)");
    stat.setQueryTimeout(1);
    conn.rollback(sp);
    conn.commit();
    conn.close();
}
Also used : Statement(java.sql.Statement) PreparedStatement(java.sql.PreparedStatement) Connection(java.sql.Connection) Savepoint(java.sql.Savepoint)

Example 20 with Savepoint

use of java.sql.Savepoint in project h2database by h2database.

the class TestLob method testLobTransactions.

private void testLobTransactions(int spaceLen) throws SQLException {
    deleteDb("lob");
    Connection conn = reconnect(null);
    conn.createStatement().execute("CREATE TABLE TEST(ID IDENTITY, " + "DATA CLOB, DATA2 VARCHAR)");
    conn.setAutoCommit(false);
    Random random = new Random(0);
    int rows = 0;
    Savepoint sp = null;
    int len = getSize(100, 400);
    // config.traceTest = true;
    for (int i = 0; i < len; i++) {
        switch(random.nextInt(10)) {
            case 0:
                trace("insert " + i);
                conn.createStatement().execute("INSERT INTO TEST(DATA, DATA2) VALUES('" + i + "' || SPACE(" + spaceLen + "), '" + i + "')");
                rows++;
                break;
            case 1:
                if (rows > 0) {
                    int x = random.nextInt(rows);
                    trace("delete " + x);
                    conn.createStatement().execute("DELETE FROM TEST WHERE ID=" + x);
                }
                break;
            case 2:
                if (rows > 0) {
                    int x = random.nextInt(rows);
                    trace("update " + x);
                    conn.createStatement().execute("UPDATE TEST SET DATA='x' || DATA, " + "DATA2='x' || DATA2 WHERE ID=" + x);
                }
                break;
            case 3:
                if (rows > 0) {
                    trace("commit");
                    conn.commit();
                    sp = null;
                }
                break;
            case 4:
                if (rows > 0) {
                    trace("rollback");
                    conn.rollback();
                    sp = null;
                }
                break;
            case 5:
                trace("savepoint");
                sp = conn.setSavepoint();
                break;
            case 6:
                if (sp != null) {
                    trace("rollback to savepoint");
                    conn.rollback(sp);
                }
                break;
            case 7:
                if (rows > 0) {
                    trace("checkpoint");
                    conn.createStatement().execute("CHECKPOINT");
                    trace("shutdown immediately");
                    conn.createStatement().execute("SHUTDOWN IMMEDIATELY");
                    trace("shutdown done");
                    conn = reconnect(conn);
                    conn.setAutoCommit(false);
                    sp = null;
                }
                break;
            default:
        }
        ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM TEST");
        while (rs.next()) {
            int id = rs.getInt("ID");
            String d1 = rs.getString("DATA").trim();
            String d2 = rs.getString("DATA2");
            assertEquals("id:" + id, d2, d1);
        }
    }
    conn.close();
}
Also used : Random(java.util.Random) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection) ResultSet(java.sql.ResultSet) Savepoint(java.sql.Savepoint) Savepoint(java.sql.Savepoint)

Aggregations

Savepoint (java.sql.Savepoint)159 Statement (java.sql.Statement)61 Connection (java.sql.Connection)56 SQLException (java.sql.SQLException)55 PreparedStatement (java.sql.PreparedStatement)32 Test (org.junit.Test)31 ResultSet (java.sql.ResultSet)26 DatabaseMetaData (java.sql.DatabaseMetaData)13 UnitTest (nl.topicus.jdbc.test.category.UnitTest)13 TransactionStatus (org.springframework.transaction.TransactionStatus)12 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)12 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)12 ArrayList (java.util.ArrayList)11 Vector (java.util.Vector)11 Test (org.junit.jupiter.api.Test)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 SQLClientInfoException (java.sql.SQLClientInfoException)7 HashMap (java.util.HashMap)5 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)4 Random (java.util.Random)3