# Setting and rolling back to a savepoint

The JDBC 3.0 API adds the method Connection.setSavepoint, which sets a savepoint within the current transaction. The Connection.rollback method has been overloaded to take a savepoint argument. See java.sql.Connection interface: supported JDBC 3.0 methods for more information.

The code example below inserts a row into a table, sets the savepoint `svpt1`, and then inserts a second row. When the transaction is later rolled back to `svpt1`, the second insertion is undone, but the first insertion remains intact. In other words, when the transaction is committed, only the row containing '1' will be added to TABLE1.

    conn.setAutoCommit(false); // Autocommit must be off to use savepoints.
    Statement stmt = conn.createStatement();
    int rows = stmt.executeUpdate("INSERT INTO TABLE1 (COL1) VALUES(1)");
    // set savepoint
    Savepoint svpt1 = conn.setSavepoint("S1");
    rows = stmt.executeUpdate("INSERT INTO TABLE1 (COL1) VALUES (2)");
    ...
    conn.rollback(svpt1);
    ...
    conn.commit();

Parent topic: java.sql.Savepoint interface
Related concepts
Releasing a savepoint
Rules for savepoints
Related reference
Restrictions on savepoints