
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get Default Transaction Isolation using DatabaseMetaData in Java
In a database system where more than one transaction is being executed simultaneously and in parallel, the property of isolation states that all the transactions will be carried out and executed as if it is the only transaction in the system. No transaction will affect the existence of any other transaction.
The getDefaultTransactionIsolation() method of the DatabaseMetaData interface returns the default isolation level of the underlying database in integer format.
To know the default transaction isolation of the underlying database −
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 get the isolation level of the database we have connected to, using the getDefaultTransactionIsolation() method of the DatabaseMetaData interface .
Example
Following JDBC program establishes connection with the database, retrieves and prints the default transaction isolation level of the underlying database.
import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.DriverManager; import java.sql.SQLException; public class DatabaseMetaData_getDefaultTransactionIsolation { public static void main(String args[]) throws SQLException { //Registering the Driver DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //Getting the connection String mysqlUrl = "jdbc:mysql://localhost/mydatabase"; Connection con = DriverManager.getConnection(mysqlUrl, "root", "password"); System.out.println("Connection established......"); //Retrieving the meta data object DatabaseMetaData metaData = con.getMetaData(); //retrieving the name of the database int product_name = metaData.getDefaultTransactionIsolation(); System.out.println("Transaction isolation level of the underlying database: "+product_name); } }
Output
Connection established...... Transaction isolation level of the underlying database: 2