Tutorialspoint

1 Answer
Arushi

JDBC provides support 5 transaction isolation levels through Connection interface.

  • TRANSACTION_NONE: It is represented by integer value 0 does not support transactions.

  • TRANSACTION_READ_COMMITTED: It is represented by integer value 2 supports transactions allowing Non-Repeatable Reads and, Phantom Reads.

  • TRANSACTION_READ_UNCOMMITTED: It is represented by integer value 1 supports transactions allowing Dirty Reads, Non-Repeatable Reads and, Phantom Reads.

  • TRANSACTION_REPEATABLE_READ: It is represented by integer value 4 supports transactions allowing only Phantom Reads.

  • TRANSACTION_SERIALIZABLE: It is represented by integer value 8 supports transactions with out allowing Dirty Reads, Non-Repeatable Reads and, Phantom Reads.

Following JDBC example displays all the transactions levels provided by the Connection interface of the JDBC API.

Example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TransactionIsolationLevelExample {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String url = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(url, "root", "password");
      System.out.println("Connection established: "+ con);
      System.out.println("TRANSACTION_NONE: "+Connection.TRANSACTION_NONE);
      System.out.println("TRANSACTION_READ_COMMITTED:"+Connection.TRANSACTION_READ_COMMITTED);
      System.out.println("TRANSACTION_READ_UNCOMMITTED: "+Connection.TRANSACTION_READ_UNCOMMITTED);
      System.out.println("TRANSACTION_REPEATABLE_READ: "+Connection.TRANSACTION_REPEATABLE_READ);
      System.out.println("TRANSACTION_SERIALIZABLE: "+Connection.TRANSACTION_SERIALIZABLE);
   }
}

Output

Connection established: com.mysql.jdbc.JDBC4Connection@6fdb1f78
TRANSACTION_NONE: 0
TRANSACTION_READ_COMMITTED:2
TRANSACTION_READ_UNCOMMITTED: 1
TRANSACTION_REPEATABLE_READ: 4
TRANSACTION_SERIALIZABLE: 8

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.