Java Connection commit()

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

1. Connection commit() Method Overview

Definition:

The commit() method of the Connection interface is used to permanently save any changes made during the current transaction to the database. When a connection is in auto-commit mode (which is true by default), calling this method has no effect.

Syntax:

void commit() throws SQLException

Parameters:

The commit() method does not take any parameters.

Key Points:

- The commit() method is used to end a transaction by permanently saving any changes made since the last commit or rollback.

- If auto-commit mode is enabled (the default setting), the commit() method has no effect because every SQL statement is treated as a transaction and is automatically committed.

- If auto-commit mode is disabled, SQL statements are grouped in a transaction, and you must call the commit() method to save the changes.

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

- After calling commit(), the connection object will go back to auto-commit mode unless auto-commit has been disabled.

2. Connection commit() Method Example

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

public class CommitExample {

    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)) {
            // Disable auto-commit mode
            connection.setAutoCommit(false);

            try (PreparedStatement preparedStatement = connection.prepareStatement(insertSql)) {
                // Setting the values for the placeholders in the SQL query
                preparedStatement.setString(1, "John");
                preparedStatement.setString(2, "Doe");
                preparedStatement.executeUpdate();

                // Committing the transaction
                connection.commit();
                System.out.println("Transaction committed successfully.");

            } catch (SQLException e) {
                // If any SQL exception occurs, rollback the transaction
                connection.rollback();
                System.out.println("Transaction rolled back.");
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Output:

Transaction committed successfully.

Explanation:

In this example, we first establish a connection to the database and disable auto-commit mode by calling setAutoCommit(false)

We then create a PreparedStatement object and execute an SQL insert statement. After executing the statement, we call the commit() method to save the changes to the database permanently. If an SQLException occurs while executing the SQL statement, we catch the exception and roll back the transaction, which undoes the changes made in the current transaction.

Comments