All for Joomla All for Webmasters

Transaction Management in JDBC

Transaction represents a single unit of work.

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 transaction.

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 transaction.

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

Simple example of transaction management in jdbc using Statement

Let’s see the simple example of transaction management using Statement.

  1. import java.sql.*;
  2. class FetchRecords{
  3. public static void main(String args[])throws Exception{
  4. Class.forName(“oracle.jdbc.driver.OracleDriver”);
  5. Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,“system”,“oracle”);
  6. con.setAutoCommit(false);
  7. Statement stmt=con.createStatement();
  8. stmt.executeUpdate(“insert into user420 values(190,’abhi’,40000)”);
  9. stmt.executeUpdate(“insert into user420 values(191,’umesh’,50000)”);
  10. con.commit();
  11. con.close();
  12. }}

If you see the table emp400, you will see that 2 records has been added.

Example of transaction management in jdbc using PreparedStatement

Let’s see the simple example of transaction management using PreparedStatement.

  1. import java.sql.*;
  2. import java.io.*;
  3. class TM{
  4. public static void main(String args[]){
  5. try{
  6. Class.forName(“oracle.jdbc.driver.OracleDriver”);
  7. Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:xe”,“system”,“oracle”);
  8. con.setAutoCommit(false);
  9. PreparedStatement ps=con.prepareStatement(“insert into user420 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 salary”);
  18. String s3=br.readLine();
  19. int salary=Integer.parseInt(s3);
  20. ps.setInt(1,id);
  21. ps.setString(2,name);
  22. ps.setInt(3,salary);
  23. ps.executeUpdate();
  24. System.out.println(“commit/rollback”);
  25. String answer=br.readLine();
  26. if(answer.equals(“commit”)){
  27. con.commit();
  28. }
  29. if(answer.equals(“rollback”)){
  30. con.rollback();
  31. }
  32. System.out.println(“Want to add more records y/n”);
  33. String ans=br.readLine();
  34. if(ans.equals(“n”)){
  35. break;
  36. }
  37. }
  38. con.commit();
  39. System.out.println(“record successfully saved”);
  40. con.close();//before closing connection commit() is called
  41. }catch(Exception e){System.out.println(e);}
  42. }}

It will ask to add more records until you press n. If you press n, transaction is committed.

PinIt
submit to reddit

Leave a Reply

Top