Transaction Management in JDBC


Prev Tutorial Next Tutorial

Transaction Management in JDBC

A transaction is a group of operation used to performed one task if all operations in the group are success then the task is finished and the transaction is successfully completed. If any one operation in the group is failed then the task is failed and the transaction is failed.

transaction management in jdbc

Suppose a movie ticket booking at online is a transaction. This task contains four operation.

  • Verify the seats
  • Reserve the seats
  • Payment
  • Issue tickets

If all the above four operations are done successfully then a transaction is finished successfully. In the middle, if any one operation is failed then all operation are canceled and finally a transaction is failed.

Properties of Transaction managements

Every transaction follows some transaction properties these are called ACID properties.

transaction properties

Atomicity: Atomicity of a transaction is nothing but in a transaction either all operations can be done or all operation can be undone, but some operations are done and some operation are undone should not occure.

Consistency: Consistency means, after a transaction completed with successful, the data in the datastore should be a reliable data this reliable data is also called as consistent data.

Isolation: Isolation means, if two transaction are going on same data then one transaction will not disturb another transaction.

Durability: Durability means, after a transaction is completed the data in the data store will be permanent until another transaction is going to be performed on that data.

Advantage of Transaction Management

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

Types of Transaction

  • Local Transaction
  • Distributed or global transaction

Local Transaction

A local transaction means, all operation in a transaction are executed against one database.
For example; If transfer money from first account to second account belongs to same bank then transaction is local transaction.

Global Transaction

A global transaction means, all operations in a transaction are executed against multiple database.
For Example; If transfer money from first account to second account belongs to different banks then the transaction is a global transaction.

Note: Jdbc technology perform only local transactions. For global transaction in java we need either EJB or spring framework.

Things required for transaction in Jdbc

To do transaction management in Jdbc, we need to follow the below steps.

  • Step 1: Disable auto commit mode of Jdbc
  • Step 2: Put all operation of a transaction in try block.
  • Step 3: If all operation are done successfully then commit in try block, otherwise rollback in catch block.

By default in Jdbc autocommit mode is enabled but we need to disable it. To disable call setAutoCommit() method of connection Interface.

Example

con.setAutoCommit(false);

To commit a transaction, call commit() and to rollback a transaction, call rollback() method of connection Interface respectively.

Example

con.commit();
con.rollback();

Note: In transaction management DDL operation are not allowed.

Note: The operation in a transaction management may be executed on same table or different table but database should be same.

Example

import java.sql.*;
class TrxaExample 
{
public static void main(String[] args)throws Exception 
{
Class.forName("oracle.jdbc.OracleDriver");
Connection con =DriverManager.getConnection("jdbc:oracle:thin:@rama-pc:1521:xe","system","system");
System.out.println("driver is loaded");
Statement stmt=con.createStatement();
con.setAutoCommit(false);
try
{
int i1=stmt.executeUpdate("insert into student values(110,'rama',685)");
int i2=stmt.executeUpdate("update customer set custadd='Hyderabad'where custid=111");
int i3=stmt.executeUpdate("delete from student where sid=101");
con.commit();
System.out.println("Transaction is success");
}//end of try
catch (Exception e)
{
try
{
con.rollback();
System.out.println("Trasaction is failed");
}
catch (Exception ex)
{
System.out.println(ex);
}
}//end of catch
stmt.close();
con.close();
System.out.println("connection is closed");
} //end of main
} //end of class

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements