Transaction Management in JDBC Example | JDBC Transactions | JDBC

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorials, we are going to learn abut Transaction Management in JDBC. A transaction represents a group of operations, used to perform a task.
A transaction is a set of commands, it will take our database from one consistent state to another consistent state.
Here are the most important points about Transaction Management in JDBC.
A transaction means, it is a group of operations used to perform a task.
A transaction can reach either success state or failure state.
If all operations are completed successfully then the transaction becomes success.
If any one of the operation fail then all remaining operations will be cancelled and finally transaction will reach to fail state.
The basic transactions are of two types.
Local Transactions
Global / Distributed Transactions
If all the operations are executed on one/same database, then it is called as local transaction.
If the operations are executed on more than one database then it is called as global transactions.
Example : If we transfer the money from account1 to account2 of same bank, then it is called as local transaction. If we transfer the money from account1 to account2 of different banks, then it is called as global or distributed transaction.
We can get the Transaction support in JDBC from Connection interface. The Connection interface given 3 methods to perform Transaction Management in JDBC.
setAutoCommit()
commit()
rollback()
Before going to begin the operations, first we need to disable the auto commit mode. This can be done by calling setAutoCommit(false).
By default, all operations done from the java program are going to execute permanently in database. Once the permanent execution happened in database, we can’t revert back them (Transaction Management is not possible).
If all operations are executed successfully, then we commit a transaction manually by calling the commit() method.
If any one of the operation failed, then we cancel the transaction by calling rollback() method.
connection.setAutoCommit(false);

try{

----------
----------

connection.commit();

}catch(Exception e){

connection.rollback();

}
package com.onlinetutorialspoint.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class Jdbc_TransactionManagement_Example {

    public static void main(String[] args) throws Exception {
        Connection connection = null;
        Statement statement = null;

        try {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/onlinetutorialspoint", "root",
                "123456");
            connection.setAutoCommit(false);
            statement = connection.createStatement();
            statement
                .executeUpdate("insert person values ('5001','Hyderabad','Chandra Shekhar')");
            statement
                .executeUpdate("insert person values ('5002','Banglore','Ram')");
            connection.commit();
            System.out.println("Transaction is commited.");
            statement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
            connection.rollback();
        }
    }
}
Output:
Transaction is committed.
If we update with the statements,
statement.executeUpdate("insert person values ('5003','Hyderabad','Chandra Shekhar')");
statement.executeUpdate("insert person values ('5001','Banglore','Ram')");
We can get the Exception like below and the transaction will be roll backed.
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '5001' for key 'PRIMARY'
Transaction is rollbacked !
Happy Learning 🙂

JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file
JDBC Scrollable ResultSet Example
JDBC Updatable ResultSet Example

JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
CallableStatement in jdbc Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file
JDBC Scrollable ResultSet Example
JDBC Updatable ResultSet Example
 


Δ
 JDBC Driver Types
 Step by Step JDBC Program
 JDBC Select Program
 JDBC Insert Program
 JDBC Update Program
 JDBC Delete Program
 JDBC Connection – Properties File
 JDBC PreparedStatement Program
 JDBC – CallableStatement Example
 JDBC – Read an Image from DB
 JDBC – Insert an Image in DB
 JDBC – Updatable ResultSet
 JDBC – Scrollable ResultSet
 JDBC – ResultSetMetaData
 JDBC – DatabaseMetaData
 JDBC – Transaction Management
 JDBC – Batch Processing
 JDBC Interview Questions