Sobes.tech
Middle

What transaction isolation levels are supported in JDBC?

sobes.tech AI

Answer from AI

The following transaction isolation levels are supported in JDBC:

  • TRANSACTION_NONE: Indicates that transactions are not supported.
  • TRANSACTION_READ_UNCOMMITTED: Allows "dirty" reads. A transaction can see data that has not yet been committed by other transactions.
  • TRANSACTION_READ_COMMITTED: Prevents "dirty" reads. A transaction only sees data that has been committed. However, non-repeatable reads and phantom reads are possible.
  • TRANSACTION_REPEATABLE_READ: Prevents "dirty" reads and non-repeatable reads. If a transaction reads the same row multiple times, it always sees the same value. Nonetheless, phantoms are possible.
  • TRANSACTION_SERIALIZABLE: The highest level of isolation. Prevents "dirty" reads, non-repeatable reads, and phantoms. All transactions execute as if they were executed sequentially. This can significantly impact performance.

The isolation level can be set using the setTransactionIsolation() method on the Connection object.

// Get a connection
Connection connection = DriverManager.getConnection(url, user, password);

// Set the isolation level
connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);

// ... perform transactional operations ...

// Commit or rollback the transaction
connection.commit();
// or
// connection.rollback();

// Close the connection
connection.close();
What transaction isolation levels are supported in… - sobes.tech