Page 1 of 6
Home Whiteboard Online Compilers Practice Articles AI Assistant
Chapters Categories
SQL HTML CSS Javascript Python Java C C++ PHP Scala C#
JDBC - Transactions
If your JDBC Connection is in auto-commit mode, which it is by default, then every SQL
statement is committed to the database upon its completion.
That may be fine for simple applications, but there are three reasons why you may want
to turn off the auto-commit and manage your own transactions −
To increase performance.
To maintain the integrity of business processes.
To use distributed transactions.
Transactions enable you to control if, and when, changes are applied to the database. It
treats a single SQL statement or a group of SQL statements as one logical unit, and if
any statement fails, the whole transaction fails.
To enable manual- transaction support instead of the auto-commit mode that the JDBC
driver uses by default, use the Connection object's setAutoCommit() method. If you
pass a boolean false to setAutoCommit( ), you turn off auto-commit. You can pass a
boolean true to turn it back on again.
For example, if you have a Connection object named conn, code the following to turn off
auto-commit −
[Link](false);
Commit & Rollback
Once you are done with your changes and you want to commit the changes then call
commit() method on connection object as follows −
Page 2 of 6
[Link]( );
Otherwise, to roll back updates to the database made using the Connection named conn,
use the following code −
[Link]( );
The following example illustrates the use of a commit and rollback object −
try{
//Assume a valid connection object conn
[Link](false);
Statement stmt = [Link]();
String SQL = "INSERT INTO Employees " +
"VALUES (106, 20, 'Rita', 'Tez')";
[Link](SQL);
//Submit a malformed SQL statement that breaks
String SQL = "INSERTED IN Employees " +
"VALUES (107, 22, 'Sita', 'Singh')";
[Link](SQL);
// If there is no error.
[Link]();
}catch(SQLException se){
// If there is any error.
[Link]();
}
In this case, none of the above INSERT statement would success and everything would
be rolled back.
For a better understanding, let us study the Commit & Rollback - Example Code.
Using Savepoints
The new JDBC 3.0 Savepoint interface gives you the additional transactional control.
Most modern DBMS, support savepoints within their environments such as Oracle's
PL/SQL.
When you set a savepoint you define a logical rollback point within a transaction. If an
error occurs past a savepoint, you can use the rollback method to undo either all the
changes or only the changes made after the savepoint.
Page 3 of 6
The Connection object has two new methods that help you manage savepoints −
setSavepoint(String savepointName) − Defines a new savepoint. It also
returns a Savepoint object.
releaseSavepoint(Savepoint savepointName) − Deletes a savepoint. Notice
that it requires a Savepoint object as a parameter. This object is usually a
savepoint generated by the setSavepoint() method.
There is one rollback (String savepointName) method, which rolls back work to the
specified savepoint.
The following example illustrates the use of a Savepoint object −
try{
//Assume a valid connection object conn
[Link](false);
Statement stmt = [Link]();
//set a Savepoint
Savepoint savepoint1 = [Link]("Savepoint1");
String SQL = "INSERT INTO Employees " +
"VALUES (106, 20, 'Rita', 'Tez')";
[Link](SQL);
//Submit a malformed SQL statement that breaks
String SQL = "INSERTED IN Employees " +
"VALUES (107, 22, 'Sita', 'Tez')";
[Link](SQL);
// If there is no error, commit the changes.
[Link]();
}catch(SQLException se){
// If there is any error.
[Link](savepoint1);
}
In this case, none of the above INSERT statement would success and everything would
be rolled back.
For a better understanding, let us study the Savepoints - Example Code.
Page 4 of 6
TOP TUTORIALS
Python Tutorial
Java Tutorial
C++ Tutorial
C Programming Tutorial
C# Tutorial
PHP Tutorial
R Tutorial
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
SQL Tutorial
TRENDING TECHNOLOGIES
Cloud Computing Tutorial
Amazon Web Services Tutorial
Microsoft Azure Tutorial
Git Tutorial
Ethical Hacking Tutorial
Docker Tutorial
Kubernetes Tutorial
DSA Tutorial
Spring Boot Tutorial
SDLC Tutorial
Unix Tutorial
CERTIFICATIONS
Business Analytics Certification
Java & Spring Boot Advanced Certification
Data Science Advanced Certification
Cloud Computing And DevOps
Advanced Certification In Business Analytics
Artificial Intelligence And Machine Learning
DevOps Certification
Page 5 of 6
Game Development Certification
Front-End Developer Certification
AWS Certification Training
Python Programming Certification
COMPILERS & EDITORS
Online Java Compiler
Online Python Compiler
Online Go Compiler
Online C Compiler
Online C++ Compiler
Online C# Compiler
Online PHP Compiler
Online MATLAB Compiler
Online Bash Terminal
Online SQL Compiler
Online Html Editor
ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE |
PRIVACY POLICY | REFUND POLICY | COOKIES POLICY | FAQ'S
Tutorials Point is a leading Ed Tech company striving to provide the best learning material on
technical and non-technical subjects.
Page 6 of 6
© Copyright 2025. All Rights Reserved.