Search in sources :

Example 36 with Savepoint

use of java.sql.Savepoint in project derby by apache.

the class SavepointJdbc30Test method xtestNestedSavepoints.

/**
 * Test 48
 */
public void xtestNestedSavepoints() throws SQLException {
    Connection con = getConnection();
    Savepoint savepoint1 = con.setSavepoint();
    Savepoint savepoint2 = con.setSavepoint();
    Statement s = createStatement();
    try {
        s.executeUpdate("SAVEPOINT s1 ON ROLLBACK RETAIN LOCKS ON ROLLBACK" + " RETAIN CURSORS");
        fail("FAIL 48 shouldn't be able set SQL savepoint nested inside " + "JDBC/SQL savepoints");
    } catch (SQLException se) {
        // Expected exception.
        assertSQLState("3B002", se);
    }
    // rollback JDBC savepoint but still can't have SQL savepoint because
    // there is still one JDBC savepoint
    con.releaseSavepoint(savepoint2);
    try {
        s.executeUpdate("SAVEPOINT s1 ON ROLLBACK RETAIN LOCKS ON ROLLBACK" + " RETAIN CURSORS");
        fail("FAIL 48 Should have gotten exception for nested SQL savepoint");
    } catch (SQLException se) {
        // Expected exception.
        assertSQLState("3B002", se);
    }
    // rollback last JDBC savepoint and
    con.releaseSavepoint(savepoint1);
    // now try SQL savepoint again
    s.executeUpdate("SAVEPOINT s1 ON ROLLBACK RETAIN LOCKS ON ROLLBACK " + "RETAIN CURSORS");
    con.rollback();
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) Savepoint(java.sql.Savepoint)

Example 37 with Savepoint

use of java.sql.Savepoint in project derby by apache.

the class SavepointJdbc30Test method testRollbackReleasesSavepointArray.

/**
 * Test 14 cause a transaction rollback and that should release the internal
 * savepoint array
 */
public void testRollbackReleasesSavepointArray() throws SQLException {
    Connection con = getConnection();
    Connection con2 = openDefaultConnection();
    con2.setAutoCommit(false);
    Statement s1, s2;
    s1 = createStatement();
    s1.executeUpdate("insert into t1 values(1,1)");
    s1.executeUpdate("insert into t1 values(2,0)");
    con.commit();
    s1.executeUpdate("update t1 set c11=c11+1 where c12 > 0");
    s2 = con2.createStatement();
    Savepoint savepoint1 = con2.setSavepoint("MyName");
    try {
        // following will get lock timeout which will rollback
        // transaction on c2
        s2.executeUpdate("update t1 set c11=c11+1 where c12 < 1");
        fail("FAIL 14 should have gotten lock time out");
    } catch (SQLException se) {
        // Expected exception.
        assertSQLState("40XL1", se);
    }
    try {
        // the transaction rollback above should have removed the
        // savepoint MyName
        con2.releaseSavepoint(savepoint1);
        fail("FAIL 14 A non-user initiated transaction rollback should " + "release the internal savepoint array");
    } catch (SQLException se) {
        // Expected exception.
        assertSQLState("3B001", se);
    }
    con.rollback();
    con2.rollback();
    s1.execute("delete from t1");
    con.commit();
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) Savepoint(java.sql.Savepoint)

Example 38 with Savepoint

use of java.sql.Savepoint in project derby by apache.

the class SavepointJdbc30Test method testNameCaseSensitivity.

/**
 * Test 10 test savepoint name case sensitivity
 */
public void testNameCaseSensitivity() throws SQLException {
    Connection con = getConnection();
    Savepoint savepoint1 = con.setSavepoint("MyName");
    String savepointName = savepoint1.getSavepointName();
    assertEquals(savepointName, "MyName");
    con.rollback();
}
Also used : Connection(java.sql.Connection) Savepoint(java.sql.Savepoint)

Example 39 with Savepoint

use of java.sql.Savepoint in project derby by apache.

the class NullableUniqueConstraintTest method testComparisonAcrossPages.

/**
 * Tries to forces internal routibe to travel across
 * pages to check for duplicates. It first inserts large
 * number of records assuming they occupy multiple pages
 * in index and then tries to insert duplicates of each
 * of them. Rrecords at the page boundry will require
 * duplucate checking routine to check more than one page
 * to look for locate. If that routine is not working properly
 * duplucate will be inserted in tree.
 * @throws java.sql.SQLException
 */
public void testComparisonAcrossPages() throws SQLException {
    Connection con = getConnection();
    Statement stmt = con.createStatement();
    // create unique constraint without not null
    stmt.executeUpdate("alter table constraintest add constraint " + "u_con unique (val1)");
    PreparedStatement ps = con.prepareStatement("insert into " + "constraintest (val1, val2) values (?, ?)");
    for (int i = 0; i < 500; i++) {
        ps.setString(1, "" + i);
        ps.setString(2, "" + i);
        ps.execute();
    }
    for (int i = 0; i < 500; i++) {
        ps.setString(1, "" + i);
        ps.setString(2, "" + i);
        try {
            ps.execute();
            fail("duplicate key inserted expected '23505'");
        } catch (SQLException e) {
            assertSQLState("inserting duplicate", "23505", e);
        }
    }
    // mark all records except for first, as deleted and try
    // inserting duplicate. This will force comparison
    // logic to scan all the records to find another rcord for
    // comparison.
    con.setAutoCommit(false);
    assertEquals(499, stmt.executeUpdate("delete from constraintest where val1 != '0'"));
    Savepoint deleted = con.setSavepoint("deleted");
    ps.setString(1, "0");
    ps.setString(2, "test");
    try {
        ps.execute();
        fail("managed to insert a duplicate");
    } catch (SQLException e) {
        assertSQLState("inserting duplicate", "23505", e);
    }
    // rollback to check point and try to insert a record
    // at the middle
    con.rollback(deleted);
    ps.setString(1, "250");
    ps.setString(2, "test");
    ps.execute();
    // rollback to check point and try
    // inserting at end
    con.rollback(deleted);
    ps.setString(1, "499");
    ps.setString(2, "test");
    ps.execute();
    ResultSet rs = stmt.executeQuery("select count (*) from constraintest");
    rs.next();
    assertEquals(2, rs.getInt(1));
    con.rollback();
    ps.close();
    stmt.close();
    ps.close();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Savepoint(java.sql.Savepoint) Savepoint(java.sql.Savepoint)

Example 40 with Savepoint

use of java.sql.Savepoint in project derby by apache.

the class WisconsinFiller method dropTable.

/**
 * Helper method which drops a table if it exists. Nothing happens if
 * the table doesn't exist.
 *
 * @param c the connection to use
 * @param table the table to drop
 * @throws SQLException if an unexpected database error occurs
 */
static void dropTable(Connection c, String table) throws SQLException {
    // Create a savepoint that we can roll back to if drop table fails.
    // This is not needed by Derby, but some databases (e.g., PostgreSQL)
    // don't allow more operations in a transaction if a statement fails,
    // and we want to be able to run these tests against other databases
    // than Derby.
    Savepoint sp = c.setSavepoint();
    Statement stmt = c.createStatement();
    try {
        stmt.executeUpdate("DROP TABLE " + table);
    } catch (SQLException e) {
        // OK to fail if table doesn't exist, roll back to savepoint
        c.rollback(sp);
    }
    stmt.close();
    c.releaseSavepoint(sp);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) 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