Sunday, 4 January 2015

What is Transaction Management in JDBC

Transaction Management in JDBC:

Transaction represents a single unit of work.
Transaction follows the ACID property.The ACID properties describes the transaction management well.  ACID stands for Atomicity, Consistency,Isolation and Durability.

Atomicity: means either all successful or none.
Consistency: ensures bringing the database from one consistent state to another consistent state.
Isolation: ensures that transaction is isolated from other transactions.
Durability: means once a transaction has been committed, it will remain so,even in the event of errors, power loss etc.

Advantage of Transaction Mangaement:

Fast performance: It makes the performance fast because database is hit at the time of commit.

In JDBC, Connection interface provides methods to manage the transaction which are given below.

void setAutoCommit(boolean status): It is true bydefault means each transaction is committed bydefault.
void commit(): commits the transaction.
void rollback(): cancels the transaction

Example of transaction management in jdbc using Statement interface.

 import java.sql.*;

 class FetchRecords{
 public static void main(String []ars)throws Exception{
 Class.forName("oracle.jdbc.driver.OracleDriver");
 Connection  con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 con.setAutoCommit(false);
 Statement stmt=con.createStatement();
 stmt.executeUpdate("insert into student values(111,'Vishwa','KEC')");
 stmt.executeUpdate("insert into student values(222,'Alex','HKU')");
 stmt.executeUpdate("insert into student values(333,'Karan','PTU')");
 System.out.prinlnt("data inserted successfully");
 con.commit();
 con.close();
 }
 }

 Output: data inserted successfully

Note: By default setAutoCommit() is true.

 Example of transaction management in jdbc using PreparedStatement interface:

 import java.sql.*;
 import java.io.*;

 class TransactionMan {
 public static void main(String []args){
 try{
 Class.forName("oracle.jdbc.driver.OracleDriver");
 Connection  con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
 con.setAutoCommit(false);
 PreparedStatement ps=con.prepareStatement("insert into student values(?, ?, ?, ?)");
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

 while(true){
 System.out.println("enter id");
 String s1=br.readLine();
 int id=Integer.parseInt(s1);
 System.out.println("enter name");
 String name=br.readLine();
 System.out.println("enter salary");
 String s3=br.readLine();
 int salary=Integer.parseInt(s3);
 System.out.println("enter address");
 String s4=br.readLine();

 ps.setInt(1,id);
 ps.setString(2,name);
 ps.setInt(3,salary);
 ps.setInt(4,address);
 ps.executeUpdate();
 System.out.println("enter commit/rollback");
 String answer=br.readLine();
 if(answer.equals("commit")){
 con.commit();
 }
 if(answer.equals("rollback")){
 con.rollback();
 }
 System.out.println("Want to add more records press y/n");
 String ans=br.readLine();
 if(ans.equals("n")){
 break;
   }
 }  // end of if block
 con.commit();
 System.out.println("records successfully added");
 con.close();    //before closing connection commit() is called
 }catch(Exception ex) {
 System.out.println(ex); }
 }
}

It will ask to add more records until you press n.If you press n,transaction will be commited.

Output: records successfully added


No comments:

Post a Comment