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)
.autocommit(false)
.sql("START TRANSACTION")
.execute()
.sql("DELETE FROM employee WHERE name = ?")
.set("Jeff Lebowski")
.execute()
.sql("INSERT INTO employee VALUES (?)")
.set("Walter Sobchak")
.execute()
.commit();
}
}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.