Home Tutorial Java Jdbc JDBC Isolation Example

 
 

JDBC Isolation Example
Posted on: October 15, 2010 at 12:00 AM
Advertisement
In this tutorial you will learn the levels of isolation in JDBC and how to use them in JDBC

JDBC Isolation Level Example

JDBC isolation level represents that, how a database maintains its interiority against the problem such as dirty reads, non-repeatable reads and phantom reads  that occurs during concurrent transactions. You can get the current isolation level by using method getTransactionIsolation(), and also set the isolation by setTransactionIsolation(int isolationlevelconstant) methods respectively.

Types of Isolation level

1. TRANSACTION_READ_UNCOMMITTED-  It allows non-repeatable reads, dirty reads and phantom reads to occur

2. TRANSACTION_READ_COMMITTED- It ensures only those data can be read which is committed.

3. TRANSACTION_REPEATABLE_READ-  It is closer to serializable but phantom reads are also possible.

4. TRANSACTION_SERIALIZABLE- In this level of isolation dirty reads, non-repeatable reads, and phantom reads are prevented.

Integer Value  for Isolation level

TRANSACTION_NONE = 0
TRANSACTION_READ_COMMITTED = 2
TRANSACTION_READ_UNCOMMITTED = 1
TRANSACTION_REPEATABLE_READ = 4
TRANSACTION_SERIALIZABLE = 8

At first create table named student in MySql database and inset values into it as.

CREATE TABLE student (
RollNo int(9)  PRIMARY KEY NOT NULL,
Name tinytext NOT NULL,
Course varchar(25) NOT NULL,
Address text
 );

Insert Value Into student table

INSERT INTO student VALUES(1, 'Vinay', 'MCA', 'Delhi') ;

TransactionIsolationExample.java

package roseindia.net;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TransactionIsolationExample {
	public static void main(String[] args) throws SQLException {
		Connection con = null; // connection reference variable for getting
		// connection
		Statement stmt = null; // Statement reference variable for query
		// Execution
		ResultSet rs = null; // ResultSet reference variable for saving query
		// result
		String conUrl = "jdbc:mysql://localhost:3306/";
		String driverName = "com.mysql.jdbc.Driver";
		String databaseName = "student";
		String usrName = "root";
		String usrPass = "root";
		try {
			// Loading Driver
			Class.forName(driverName);
		} catch (ClassNotFoundException e) {
			System.out.println(e.toString());
		}
		try {
			// Getting Connection
			con = DriverManager.getConnection(conUrl + databaseName, usrName,
					usrPass);
			// Creating Statement for query execution
			stmt = con.createStatement();
			// creating Query String
			String query = "SELECT * FROM student";
			// excecuting query
			rs = stmt.executeQuery(query);
			while (rs.next()) {
				// Didplaying data of tables
				System.out.println("Roll No " + rs.getInt("RollNo") + ", Name "
						+ rs.getString("Name") + ", Course "
						+ rs.getString("Course") + ", Address "
						+ rs.getString("Address"));
			}
			DatabaseMetaData dbMetaData = con.getMetaData();
			if (dbMetaData
					.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE)) {
				System.out.println("Transaction Isolation level= "
						+ con.getTransactionIsolation());
				// Setting Transaction Isolation Level
				// You can set Its String Value or its int value
				con.setTransactionIsolation(2);
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		} finally {
			// Closing connection
			con.close();
			stmt.close();
			rs.close();
		}
	}
}
When you run this application it will display message as shown below:

Roll No 1, Name Vinay, Course MCA, Address Delhi
Transaction Isolation level= 4

Download this example code

Advertisement

Related Tags for JDBC Isolation Example:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 15, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com