commit()/rollback() Methods

The commit() and rollback() methods are used in JDBC to manage database transactions. A transaction is a group of database operations that should be treated as a single unit of work. The commit() method permanently saves all changes made during the current transaction, while rollback() cancels the changes made since the transaction began or since the last commit. These methods help maintain data consistency and reliability, especially when multiple operations must either all succeed or all fail. By default, JDBC connections usually use auto-commit mode, where each SQL statement is committed automatically. To manually control transactions, auto-commit can be disabled using setAutoCommit(false).

Transaction Methods

Method Signature and Short Description

public void setAutoCommit(boolean autoCommit)

Enables or disables automatic transaction committing.

public boolean getAutoCommit()

Returns whether auto-commit mode is currently enabled.

public void commit()

Permanently saves all changes made during the current transaction.

public void rollback()

Cancels all changes made during the current transaction.

public void rollback(Savepoint savepoint)

Rolls back the transaction to the specified savepoint.

public Savepoint setSavepoint()

Creates a savepoint at the current position in the transaction.

public Savepoint setSavepoint(String name)

Creates a named savepoint in the current transaction.

public void releaseSavepoint(Savepoint savepoint)

Removes a previously created savepoint.

 

Scroll to Top