Plain JDBC Transaction

import java.sql.Connection;
 
Connection connection = dataSource.getConnection();
 
try (connection) {
    connection.setAutoCommit(false); // (2)
	connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    executeSqlStatements(connection); // execute some SQL statements
    connection.commit();
} catch (SQLException e) {
    connection.rollback();
}
 
public void executeSqlStatements(Connection connection) {
	Statement stmt = con.createStatement()) {
    ResultSet rs = stmt.executeQuery(query);
    while (rs.next()) {
        String coffeeName = rs.getString("COF_NAME");
        int supplierID = rs.getInt("SUP_ID");
        System.out.println(coffeeName + ", " + supplierID);
    }
}
  • setAutoCommit(true) wraps every single SQL statement in its own transaction
  • setAutoCommit(false) is the opposite: You are the master of the transaction

Subpages

Spring Specific