«Back to Home

Core Java

Topics

Transaction Management In JDBC

Transaction Management
 
In JDBC, transaction means a single unit of work. A transaction is a group of operations used to perform one task, if all the operations in the group are success, the task is finished and the transaction is successfully completed. If anyone operation in the group is fails, the task fails and the transaction is fails.

7
 
The ACID properties describes the transaction management well. ACID means Atomicity, Consistency, Isolation and Durability.
 
Atomicity denotes either all are successful or none.
 
Consistency makes sure to bring the database from one consistent state to the consistent state.
 
Isolation makes sure that the transaction is isolated from the other transaction.
 
Durability denotes once a transaction has been committed, it will remain thus, even in the event of errors, power loss etc.

8
 
Advantage of Transaction Management

Fast performance: Because the database is hit at the time of commit and it makes the performance fast.
 
Connection interface provides some methods to manage transaction in JDBC which are,

void setAutoCommit(boolean status)

It is true by default, which means each transaction is committed by default.
 
void commit()

This method is used to commit the transaction.
 
void rollback()

This method is used to cancel the transaction.
 
Let's see an example of the transaction management, using Statement.
 
Code
  1. import java.sql.*;  
  2. public class StudentDatabase {  
  3.     public static void main(String args[]) throws Exception {  
  4.         Class.forName("org.apache.derby.jdbc.ClientDriver");  
  5.         String url = "jdbc:derby://localhost:1527/Student";  
  6.         String username = "Student";  
  7.         String password = "student";  
  8.         Connection conn = DriverManager.getConnection(url, username, password);  
  9.         Statement st = conn.createStatement();  
  10.         st.executeUpdate("insert into STUDENT.STUDENTDB values(245,'Sam',24,'RETN college')");  
  11.         st.executeUpdate("insert into STUDENT.STUDENTDB values(191,'Jimmy',26,'MTST college')");  
  12.   
  13.         conn.commit();  
  14.         conn.close();  
  15.     }  
  16. }  
9

If we see the table Student, we will see that the two records have been added.
 
Let's see an example of the transaction management, using PreparedStatement.
 
Code
  1. import java.sql.*;  
  2. import java.io.*;  
  3. public class StudentDatabase {  
  4.     public static void main(String args[]) throws Exception {  
  5.         Class.forName("org.apache.derby.jdbc.ClientDriver");  
  6.         Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/Student""Student""student");  
  7.         conn.setAutoCommit(false);  
  8.   
  9.         PreparedStatement ps = conn.prepareStatement("insert into STUDENT.STUDENTDB values(?,?,?,?)");  
  10.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  11.         while (true) {  
  12.             System.out.println("Enter id");  
  13.             String s1 = br.readLine();  
  14.             int id = Integer.parseInt(s1);  
  15.             System.out.println("Enter name");  
  16.             String name = br.readLine();  
  17.             System.out.println("Enter age");  
  18.             String s3 = br.readLine();  
  19.             int age = Integer.parseInt(s3);  
  20.             System.out.println("Enter collage");  
  21.             String college = br.readLine();  
  22.             ps.setInt(1, id);  
  23.             ps.setString(2, name);  
  24.             ps.setInt(3, age);  
  25.             ps.setString(4, college);  
  26.             ps.executeUpdate();  
  27.             System.out.println("commit/rollback");  
  28.             String answer = br.readLine();  
  29.             if (answer.equals("commit")) {  
  30.                 conn.commit();  
  31.             }  
  32.             if (answer.equals("rollback")) {  
  33.                 conn.rollback();  
  34.             }  
  35.             System.out.println("Want to add more records yes/no");  
  36.             String ans = br.readLine();  
  37.             if (ans.equals("no")) {  
  38.                 break;  
  39.             }  
  40.         }  
  41.         conn.commit();  
  42.         System.out.println("Record successfully saved");  
  43.         conn.close();  
  44.     }  
  45. }  
10
11

Output

12

In the example mentioned above, it will ask to add more records until you press no. If we press no, the transaction is committed.
 
Summary

Thus, we learnt, JDBC transaction means a single unit of work and also learnt, how we can use it in Java through the examples.