> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/spring/spring-features/spring-persistence/jdbc/transaction-management.md).

# Transaction Management

## About

In JDBC, transaction management is handled manually by controlling the auto-commit behavior of a `Connection` object. JDBC transactions allow us to group multiple SQL operations into a single atomic unit of work. If one of the operations fails, we can roll back the entire transaction to maintain data consistency.

## **1. Basics of JDBC Transactions**

By default, JDBC connections operate in **auto-commit mode**. This means every SQL statement is immediately committed after execution. For proper transaction management, you must **disable auto-commit** and explicitly commit or roll back the transaction.

**Example (without Spring):**

```java
Connection conn = dataSource.getConnection();
try {
    conn.setAutoCommit(false); // start transaction

    // Execute multiple SQL statements
    PreparedStatement stmt1 = conn.prepareStatement("INSERT INTO employee ...");
    stmt1.executeUpdate();

    PreparedStatement stmt2 = conn.prepareStatement("UPDATE salary ...");
    stmt2.executeUpdate();

    conn.commit(); // commit if everything succeeds
} catch (SQLException e) {
    conn.rollback(); // rollback on any failure
    throw e;
} finally {
    conn.setAutoCommit(true); // restore default behavior
    conn.close();
}
```

## **2. Spring JDBC Transaction Management**

Spring provides a higher-level abstraction for managing transactions, often through:

* Declarative approach using `@Transactional`
* Programmatic approach using `TransactionTemplate`

Spring will manage the `Connection`, disable auto-commit, and commit or roll back the transaction based on the method execution and exceptions.

**Example (declarative):**

```java
@Transactional
public void performTransfer() {
    jdbcTemplate.update("UPDATE account SET balance = balance - 100 WHERE id = 1");
    jdbcTemplate.update("UPDATE account SET balance = balance + 100 WHERE id = 2");
}
```

In this case:

* Spring will automatically manage the transaction boundaries.
* If any exception is thrown inside the method, the entire transaction will be rolled back.

## Important Points

* **Auto-commit**
  * Enabled by default. Each SQL is committed automatically.
  * Must be set to false to begin a manual transaction.
* **Commit**
  * Used to make all changes made during the transaction permanent.
* **Rollback**
  * Used to revert all changes made during the transaction if an error occurs.
* **Savepoints**
  * We can define intermediate points in a transaction to roll back partially instead of the whole transaction.
  * Example:

    ```java
    Savepoint savepoint = conn.setSavepoint();
    // do something
    conn.rollback(savepoint); // rollback only to savepoint
    ```

## Common Mistakes to Avoid

* Not resetting `autoCommit` to `true` after transaction ends (can cause issues if connection is reused).
* Not closing the `Connection` in a `finally` block (can lead to leaks).
* Assuming database rollback will always happen automatically (some databases require explicit rollback).

## When to Use JDBC-level Transactions ?

* When working directly with JDBC or Spring JDBC (`JdbcTemplate`).
* In small projects without the need for ORM or JPA.
* When we need precise, low-level control over transaction boundaries.
* When using stored procedures, or performing complex batch operations.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://www.pranaypourkar.co.in/the-programmers-guide/spring/spring-features/spring-persistence/jdbc/transaction-management.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
