View RSS Feed

My Java Tips

Transaction savepoints (JDBC 3.0)

Rate this Entry
by , 11-28-2011 at 07:32 PM (2702 Views)
JDBC 2 provides complete transaction rollback control over transaction. But if you want to rollback to a point, you cannot do so. JDBC 3.0 introduces savepoints which makes this possible.

JDBC 3.0 provides more control over transactions using savepoints. The Savepoint interface allows us to partition a transaction into logical breakpoints. This in turn provides control over how much of the transaction we wish to rolled back.

Lets see how to do this. Review the following example:

Java Code:
// Set a conservative transaction isolation level.
Statement stmt = conn.createStatement();
int rows = stmt.executeUpdate(    "INSERT INTO authors " +
                "(first_name, last_name) VALUES " +
                "('Lewis', 'Carroll')");
// Set a named savepoint.
Savepoint svpt = conn.setSavepoint("NewAuthor");
// ...

rows = stmt.executeUpdate(    "UPDATE authors set type = 'fiction' " +
                "WHERE last_name = 'Carroll'");

// ...
// ...
// The author has been added, but not updated.
Its really an interesting feature. Try it out.

Submit "Transaction savepoints (JDBC 3.0)" to Facebook Submit "Transaction savepoints (JDBC 3.0)" to Digg Submit "Transaction savepoints (JDBC 3.0)" to Submit "Transaction savepoints (JDBC 3.0)" to StumbleUpon Submit "Transaction savepoints (JDBC 3.0)" to Google

Tags: jdbc 2, savepoint Add / Edit Tags