JDBC Transaction ACID property


 

JDBC Transaction ACID property

this tutorial of JDBC gives the overview of transaction ACID property

this tutorial of JDBC gives the overview of transaction ACID property

JDBC Transaction ACID Property

A Transaction is a unit of work performed on the database and treated in a reliable way independent of other transaction. In database transaction processing ACID property refers to the Atomicity, Consistency, Isolation, Durability respectively.

Atomicity- This property says that all the changes to the data is performed if they are single operation. For example suppose in a bank application if a fund transfer from one account to another account the atomicity property ensures that is a debit is made successfully in one account the corresponding credit would be made in other account.

Consistency- The consistency property of transaction says that the data remains in the consistence state when the transaction starts and ends. for example suppose in the same bank account, the fund transfer from one account to another account, the consistency property ensures that the total value(sum of both account ) value remains the same after the transaction ends.

Isolation- This property says that, the intermediate state of transaction are hidden/ invisible to another transaction process. Suppose in the the bank application, the isolation  property ensures that the fund transfer  from one account to another account, the transaction sees the fund transfer in one account or the other account.

Durability- The Durability says that when the transaction is completed successfully, the changing to the data is persist and is not un-done, even in the event of system failure.

An example of transaction with isolation is given below

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

Ads