- Java & MySQL Tutorial
- Java & MySQL - Home
- Java & MySQL - Overview
- Java & MySQL - Environment Setup
- Java & MySQL - Sample Code
- Java & MySQL - Connections
- Java & MySQL - Exceptions
- Java & MySQL Statements
- Java & MySQL - Statement
- Java & MySQL - PreparedStatement
- Java & MySQL - CallableStatement
- Java & MySQL Result
- Java & MySQL - Result Set
- Java & MySQL - Navigate Result Set
- Java & MySQL - View Result Set
- Java & MySQL - Update Result Set
- Java & MySQL Transactions
- Java & MySQL - Transactions
- Java & MySQL - Commit & Rollback
- Java & MySQL - SavePoint Transactions
- Java & MySQL Batch Processing
- Java & MySQL - Batch Processing
- Batch Processing - Statement
- Batch Processing - PreparedStatement
- Java & MySQL Streaming
- Java & MySQL - Streaming Data
- Java & MySQL Examples
- Java & MySQL - Create Database
- Java & MySQL - Select Database
- Java & MySQL - Drop Database
- Java & MySQL - Create Tables
- Java & MySQL - Drop Tables
- Java & MySQL - Insert Records
- Java & MySQL - Select Records
- Java & MySQL - Update Records
- Java & MySQL - Delete Records
- Java & MySQL - Where Clause
- Java & MySQL - Like Clause
- Java & MySQL - Sorting Data
- Java & MySQL Useful Resources
- Java & MySQL - Quick Guide
- Java & MySQL - Useful Resources
- Java & MySQL - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java & MySQL - Transactions
85 Lectures 6 hours
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 −
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 −
Otherwise, to roll back updates to the database made using the Connection named conn, use the following code −
The new JDBC 3.0 Savepoint interface gives you the additional transactional control.
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.
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.