Transaction Management in JDBC

Posted By on April 29, 2016


Download PDF
Statement, PreparedStatement & CallableStatement
Servlet API and Overview

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

import java.sql.*;
class FetchRecords{
public static void main(String args[])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 user420 values(190,’akash’,40000)”);
stmt.executeUpdate(“insert into user420 values(191,’rajat’,50000)”);

con.commit();
con.close();
}}

Statement, PreparedStatement & CallableStatement
Servlet API and Overview

Download PDF