Java DatabaseMetaData supportsTransactionIsolationLevel() method with example


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.

The supportsTransactionIsolationLevel() method of the DatabaseMetaData interface is used to determine whether the underlying database supports the specified transaction isolation level.

This method accepts an integer representing a transaction isolation level and, returns a boolean value which is −

  • True, when the underlying database support the specified transaction isolation level.
  • False, when the underlying database doesn't support the specified transaction isolation level.

To determine whether the underlying database supports required transaction isolation level −

  • Make sure your database is up and running.
  • Register the driver using the registerDriver() method of the DriverManager class. Pass an object of the driver class corresponding to the underlying database.
  • Get the connection object using the getConnection() method of the DriverManager class. Pass the URL the database and, user name, password of a user in the database, as String variables.
  • Get the DatabaseMetaData object with respect to the current connection using the getMetaData() method of the Connection interface.
  • Finally, invoke the supportsTransactionIsolationLevel() method of the ResultSetMetaData interface and save the value it returns in a boolean variable say, bool. If this value is true, underlying database supports the transaction isolation level else it doesn't.

Following JDBC program establishes connection with MySQL database, and determines and prints whether it supports the specified transaction isolation level.

Example

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DatabaseMetadata_supportsTransactionIsolationLevel {
   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......");
      //Retrieving the meta data object
      DatabaseMetaData metaData = con.getMetaData();
      //Determining whether the underlying database supports TransactionIsolationLevel
      boolean bool = metaData.supportsTransactionIsolationLevel(2);
      if(bool) {
         System.out.println("Underlying database supports the specified TransactionIsolationLevel");
      } else {
         System.out.println("Underlying database does not support the specified TransactionIsolationLevel");
      }
   }
}

Output

Connection established......
Underlying database supports TransactionIsolationLevel

Arushi
Arushi

e

Updated on: 30-Jul-2019

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements