• 0.17

Maintaining SQL Transactions Through JDBC

Say, you want to make a few operations in one ACID transaction, where the first one is deletion and the second one is inserting:

public class Main {
  public static void main(String[] args) {
    new JdbcSession(source)
      .sql("DELETE FROM employee WHERE name = ?")
      .set("Jeff Lebowski")
      .sql("INSERT INTO employee VALUES (?)")
      .set("Walter Sobchak")

In this example, autocommit(false) turns off autocommit mechanism, which happens by default after every call to update() or insert().

The last statement should be a call to commit(), which translates to the native transaction commit SQL statement.