Java Connection rollback()

In this guide, you will learn about the Connection rollback() method in Java programming and how to use it with an example.

1. Connection rollback() Method Overview

Definition:

The rollback() method of the Connection interface undoes all changes made in the current transaction and releases any database locks currently held by this Connection object. This method is used when an error occurs, and you do not want to save the changes made during the transaction.

Syntax:

void rollback() throws SQLException

Parameters:

None.

Key Points:

- The rollback() method is used to manually revert transactions when the connection is in auto-commit mode set to false.

- When auto-commit mode is enabled (which is by default), invoking the rollback() method results in a SQLException.

- A call to rollback() closes the current ResultSet object(s) associated with this Connection object.

- The method throws a SQLException if a database access error occurs, this method is called while participating in a distributed transaction, the Connection object is in auto-commit mode, or this method is called on a closed connection.

2. Connection rollback() Method Example

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class RollbackExample {

    public static void main(String[] args) {

        String url = "jdbc:your_database_url";
        String user = "your_database_user";
        String password = "your_database_password";

        String insertSql = "INSERT INTO employees (first_name, last_name) VALUES (?, ?)";

        try (Connection connection = DriverManager.getConnection(url, user, password)) {

            // Disabling auto-commit mode
            connection.setAutoCommit(false);

            try (PreparedStatement preparedStatement = connection.prepareStatement(insertSql)) {
                preparedStatement.setString(1, "John");
                preparedStatement.setString(2, "Doe");
                preparedStatement.executeUpdate();

                // Intentionally causing an error to demonstrate rollback
                preparedStatement.setString(1, null);
                preparedStatement.setString(2, "Smith");
                preparedStatement.executeUpdate();

                // Committing the transaction
                connection.commit();
                System.out.println("Transaction committed successfully.");
            } catch (SQLException e) {
                // Rollback the transaction in case of an error
                connection.rollback();
                System.err.println("Transaction rolled back due to error: " + e.getMessage());
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Output:

Transaction rolled back due to error: [Error details]

Explanation:

In this example, a connection to the database is established, and auto-commit mode is disabled. An INSERT statement is prepared and executed successfully. 

After that, another INSERT statement with a null value, causing a SQLException, is executed to demonstrate the rollback. 

When the SQLException occurs, the catch block is executed, the transaction is rolled back using the rollback() method, and an error message is printed.

Comments