Setting Transaction Isolation Levels

Transaction isolation levels in JDBC control how one transaction can see changes made by other transactions running at the same time. They help prevent problems such as dirty reads, non-repeatable reads, and phantom reads. The isolation level is set using the setTransactionIsolation() method of the Connection interface. JDBC provides four standard isolation levels: TRANSACTION_READ_UNCOMMITTED, TRANSACTION_READ_COMMITTED, TRANSACTION_REPEATABLE_READ, and TRANSACTION_SERIALIZABLE. A higher isolation level provides stronger data consistency but may reduce concurrency and performance.

JDBC Transaction Isolation Levels

Isolation Level Description
TRANSACTION_READ_UNCOMMITTED Allows a transaction to read uncommitted changes made by another transaction. It provides the lowest isolation.
TRANSACTION_READ_COMMITTED Allows a transaction to read only committed data. It prevents dirty reads.
TRANSACTION_REPEATABLE_READ Ensures that data already read by a transaction cannot be changed by another transaction until the first transaction completes.
TRANSACTION_SERIALIZABLE Provides the highest isolation and prevents dirty reads, non-repeatable reads, and phantom reads.

Program : Applying All Four Isolation Levels to Transactions

This program demonstrates how the four transaction isolation levels can be applied to database transactions. For each level, an employee record is retrieved and the transaction is committed after the operation is completed.

import java.sql.*;

public class FourIsolationLevelsExample {

    public static void main(String[] args) {

        String url = "jdbc:mysql://localhost:3306/jdbc_demo";
        String username = "root";
        String password = "password";

        String sql =
                "SELECT employee_id, employee_name, salary " +
                "FROM employee " +
                "WHERE employee_id = 101";

        try {
            Connection con =
                    DriverManager.getConnection(
                            url, username, password);

            con.setAutoCommit(false);

            // 1. READ_UNCOMMITTED
            performTransaction(
                    con,
                    Connection.TRANSACTION_READ_UNCOMMITTED,
                    sql);

            // 2. READ_COMMITTED
            performTransaction(
                    con,
                    Connection.TRANSACTION_READ_COMMITTED,
                    sql);

            // 3. REPEATABLE_READ
            performTransaction(
                    con,
                    Connection.TRANSACTION_REPEATABLE_READ,
                    sql);

            // 4. SERIALIZABLE
            performTransaction(
                    con,
                    Connection.TRANSACTION_SERIALIZABLE,
                    sql);

            con.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    static void performTransaction(
            Connection con,
            int isolationLevel,
            String sql) throws SQLException {

        con.setTransactionIsolation(isolationLevel);

        System.out.println(
                "\nTransaction Isolation Level: " +
                getIsolationName(isolationLevel));

        Statement stmt = con.createStatement();

        ResultSet rs = stmt.executeQuery(sql);

        if (rs.next()) {

            System.out.println(
                    "Employee ID: " +
                    rs.getInt("employee_id"));

            System.out.println(
                    "Employee Name: " +
                    rs.getString("employee_name"));

            System.out.println(
                    "Salary: " +
                    rs.getDouble("salary"));
        }

        con.commit();

        System.out.println(
                "Transaction committed.");

        rs.close();
        stmt.close();
    }

    static String getIsolationName(int level) {

        switch (level) {

            case Connection.TRANSACTION_READ_UNCOMMITTED:
                return "READ_UNCOMMITTED";

            case Connection.TRANSACTION_READ_COMMITTED:
                return "READ_COMMITTED";

            case Connection.TRANSACTION_REPEATABLE_READ:
                return "REPEATABLE_READ";

            case Connection.TRANSACTION_SERIALIZABLE:
                return "SERIALIZABLE";

            default:
                return "UNKNOWN";
        }
    }
}
Execution and Output:

PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" FourIsolationLevelsExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" FourIsolationLevelsExample

Transaction Isolation Level: READ_UNCOMMITTED
Employee ID: 101
Employee Name: Ravi
Salary: 78500.0
Transaction committed.

Transaction Isolation Level: READ_COMMITTED
Employee ID: 101
Employee Name: Ravi
Salary: 78500.0
Transaction committed.

Transaction Isolation Level: REPEATABLE_READ
Employee ID: 101
Employee Name: Ravi
Salary: 78500.0
Transaction committed.

Transaction Isolation Level: SERIALIZABLE
Employee ID: 101
Employee Name: Ravi
Salary: 78500.0
Transaction committed.Code language: CSS (css)
Scroll to Top