Transaction savepoints (JDBC 3.0)

18 April 2008

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 introdices 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:

conn.setAutoCommit(false);
// Set a conservative transaction isolation level.
conn.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
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'");
 
// ...
conn.rollback(svpt);
// ...
// The author has been added, but not updated.
conn.commit();

Its really an interesting feature. Try it out.

del.icio.us:Transaction savepoints (JDBC 3.0)  digg:Transaction savepoints (JDBC 3.0)  spurl:Transaction savepoints (JDBC 3.0)  wists:Transaction savepoints (JDBC 3.0)  simpy:Transaction savepoints (JDBC 3.0)  newsvine:Transaction savepoints (JDBC 3.0)  blinklist:Transaction savepoints (JDBC 3.0)  furl:Transaction savepoints (JDBC 3.0)  reddit:Transaction savepoints (JDBC 3.0)  fark:Transaction savepoints (JDBC 3.0)  blogmarks:Transaction savepoints (JDBC 3.0)  Y!:Transaction savepoints (JDBC 3.0)  smarking:Transaction savepoints (JDBC 3.0)  magnolia:Transaction savepoints (JDBC 3.0)  segnalo:Transaction savepoints (JDBC 3.0)  gifttagging:Transaction savepoints (JDBC 3.0)

Top Of Page | Trackback

If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.

It will look like this: Transaction savepoints (JDBC 3.0)

Leave a Reply