
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
Java Connection getTransactionIsolation Method with Example
In this article, we learn the Connection getTransactionIsolation() method in Java, which allows you to determine a database connection's current transaction isolation level. Transaction isolation levels dictate how transactions interact, particularly regarding data locking during reads or writes.
What is the getTransactionIsolation() Method?
The getTransactionIsolation() method is part of the Connection Interface in Java. It returns an integer constant representing the current isolation level of the database connection.
JDBC provides support for 5 transaction isolation levels through the Connection interface ?
- TRANSACTION_NONE: It is represented by an integer value 0 and does not support transactions.
- TRANSACTION_READ_COMMITTED: It is represented by integer value 2 that 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 that supports transactions allowing only Phantom Reads.
- TRANSACTION_SERIALIZABLE: It is represented by integer value 8 supports transactions without allowing Dirty Reads, Non-Repeatable Reads and, Phantom Reads.
Using the MySQL JDBC Driver
In this approach, we manually register the MySQL JDBC Driver and use the getTransactionIsolation() method to retrieve the transaction isolation level of the connected database.
Following are the steps for creating a getTransactionIsolation() method using the MySQL JDBC driver ?
- Import necessary classes like java.sql.Connection, java.sql.DriverManager, and java.sql.SQLException.
- Register the MySQL JDBC driver.
- Establish a connection to the database using DriverManager.getConnection().
- Call getTransactionIsolation() on the connection object to retrieve the isolation level.
- Print the retrieved isolation level.
Example
Below is an example of creating a getTransactionIsolation() method using the MySQL JDBC driver ?
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class Connection_getTransactionIsolation { 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 transaction isolation level int transactionIsolationLevel = con.getTransactionIsolation(); System.out.println("Transaction isolation level of the underlying database is: " + transactionIsolationLevel); } }
Output
Connection established...... Transaction isolation level of the underlying database is: 4
Using try-with-resources
In this approach, we enhance the first method using the try-with-resources statement for automatic resource management. This ensures that the database connection is closed properly.
Following are the steps for creating a getTransactionIsolation() method using try-with-resources ?
- Import the same classes as in the first approach.
- Use the try-with-resources statement to manage the database connection.
- Within the try block, connect to the database and retrieve the transaction isolation level.
- Print the isolation level.
Example
Below is an example of creating a getTransactionIsolation() method usingtry-with-resources ?
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class ConnectionTransactionIsolationExample { public static void main(String[] args) { // Database URL, username, and password String url = "jdbc:mysql://localhost/mydatabase"; String username = "root"; String password = "password"; try (Connection con = DriverManager.getConnection(url, username, password)) { System.out.println("Connection established......"); // Retrieving the transaction isolation level int transactionIsolationLevel = con.getTransactionIsolation(); System.out.println("Transaction isolation level of the underlying database is: " + transactionIsolationLevel); } catch (SQLException e) { System.out.println("Error while connecting to the database: " + e.getMessage()); } } }
Output
Connection established......
Transaction isolation level of the underlying database is: 4
Comparison Table
Feature |
MySQL JDBC Driver | try-with-resources |
Resource Management |
Manual (con.close() needed) |
Automatic (try-with-resources) |
Code Readability |
Less readable |
Cleaner and modern syntax |
Error Handling |
Minimal |
Enhanced with try-catch |