Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What is the auto-commit mode of a JDBC Connection, and how does Spring's transaction management interact with it?

A new JDBC connection is in auto-commit mode: every statement is committed on its own as soon as it runs. To group statements, we call setAutoCommit(false) and finish with commit() or rollback(). Spring's DataSourceTransactionManager and JpaTransactionManager do exactly that when a transaction begins and restore auto-commit when it ends, while code that runs outside a Spring transaction stays in auto-commit mode; HikariCP's autoCommit setting decides the pool default, and some setups turn it off and tell Hibernate so to save round trips.

Why this question is asked

Auto-commit is one of those defaults we rarely see, yet it decides whether a failure halfway through an operation leaves half-written data. It also explains several production surprises, such as a PostgreSQL fetch size that is ignored or extra round trips on every transaction. Interviewers ask it to check that we know what a transaction is at the JDBC level and what Spring actually does for us. In this answer, we will recap auto-commit, group statements by hand, see what Spring's transaction managers do with the connection, look at statements outside a transaction, and finish with the HikariCP and Hibernate settings.

1A Quick Recap of Auto-Commit

A transaction groups database changes so that they all succeed or all fail together. At the JDBC level, a transaction lives on a Connection. Every Connection has an auto-commit flag, and the JDBC specification says a new connection starts with it on. In auto-commit mode, the driver commits each statement immediately after it completes, so each statement is its own little transaction.

That is convenient for a single insert, but dangerous for an operation that needs several statements. If we move money by debiting one account and crediting another, and the second statement fails, the first one is already committed.

Java
try (Connection con = dataSource.getConnection()) {
System.out.println(con.getAutoCommit()); // true
try (Statement st = con.createStatement()) {
st.executeUpdate("update account set balance = balance - 100 where id = 1"); // committed now
st.executeUpdate("update account set balance = balance + 100 where id = 2"); // throws: account 2 is frozen (check constraint)
}
}
// account 1 has lost 100 although the credit never happened

Notice that the first update is permanent the moment it returns. Nothing ties the two statements together.

2Grouping Statements by Hand

To make the two updates one unit, we switch auto-commit off, run both statements, and then commit or roll back ourselves.

Java
try (Connection con = dataSource.getConnection()) {
con.setAutoCommit(false); // start grouping statements
try (PreparedStatement debit = con.prepareStatement("update account set balance = balance - ? where id = ?");
PreparedStatement credit = con.prepareStatement("update account set balance = balance + ? where id = ?")) {
debit.setBigDecimal(1, amount); debit.setLong(2, from); debit.executeUpdate(); // 1
credit.setBigDecimal(1, amount); credit.setLong(2, to); int n = credit.executeUpdate();
if (n != 1) throw new IllegalStateException("no target account");
con.commit(); // both changes become visible together
} catch (Exception e) {
con.rollback(); // neither change is kept
throw e;
} finally {
con.setAutoCommit(true); // a pooled connection goes back clean
}
}

The finally block matters for pooled connections. Closing a pooled connection does not close the socket; it hands the connection back to the pool for the next caller. HikariCP resets auto-commit to the pool's default when a connection is returned, but restoring state ourselves is good practice and matches what Spring does.

3What Spring's Transaction Managers Do

When a method annotated with @Transactional starts, Spring's transaction manager gets a connection and prepares it. DataSourceTransactionManager, used with JdbcTemplate and JdbcClient, checks whether the connection is in auto-commit mode and, if it is, calls setAutoCommit(false) and remembers that it changed it. It binds the connection to the current thread, so every JdbcTemplate call in that method uses the same connection. When the method returns, it commits; when it throws a runtime exception, it rolls back. Then it calls setAutoCommit(true) again, if it changed it, and releases the connection.

JpaTransactionManager does the same through Hibernate: beginning a JPA transaction makes Hibernate switch auto-commit off on its JDBC connection, and ending it restores the original value. Both managers leave the connection exactly as they found it.

Step throughA @Transactional method and its connection
Service proxyTx managerPoolConnection
begin transaction
getConnection()
connection (autoCommit = true)
setAutoCommit(false)
statements via JdbcTemplate, same connection
commit() or rollback()
setAutoCommit(true), restore the original
close(): back to the pool

8 messages in order. Play them, or step through them one at a time.

The transaction manager switches auto-commit off for the transaction and puts it back afterwards.

4Statements Outside a Spring Transaction

If no transaction is active, JdbcTemplate borrows a connection from the pool for each call, runs the statement in auto-commit mode, and gives the connection back. So two JdbcTemplate calls in a method without @Transactional are two separate commits, possibly on two different connections.

Java
public void transferWithoutTx(long from, long to, BigDecimal amount) {
jdbc.update("update account set balance = balance - ? where id = ?", amount, from); // commit 1
jdbc.update("update account set balance = balance + ? where id = ?", amount, to); // commit 2
}
// an exception between the two calls leaves the debit committed
@Transactional
public void transfer(long from, long to, BigDecimal amount) { /* same two calls */ }
// one connection, auto-commit off, one commit at the end

This is also why the usual @Transactional pitfalls, such as a call from inside the same class that skips the proxy, show up as auto-commit behaviour: the statements still run, but each one commits on its own.

5HikariCP's autoCommit and the Hibernate Hint

HikariCP has an autoCommit property, true by default, that sets the auto-commit state of every connection it creates and restores. In Spring Boot it is spring.datasource.hikari.auto-commit.

Some JPA applications set it to false. The reason is a small saving per transaction: if connections already arrive with auto-commit off, Hibernate does not need to call setAutoCommit(false) at the start and setAutoCommit(true) at the end, and with some drivers each of those calls costs work or a round trip. Hibernate must be told, otherwise it still checks and toggles the flag; the property is hibernate.connection.provider_disables_autocommit=true, set through spring.jpa.properties in Spring Boot, but check your Hibernate version for its exact name and behaviour.

There is a catch: with auto-commit off at the pool, any code that uses a connection outside a transaction and forgets to commit loses its writes, because HikariCP rolls back uncommitted work when the connection returns to the pool. So this setting suits applications where every write goes through Spring transactions.

6Follow-up questions

Does @Transactional(readOnly = true) change auto-commit?

No, it still turns auto-commit off for the duration, like any transaction. What readOnly adds is a hint: Spring may call setReadOnly(true) on the connection, and with Hibernate it switches the session to a mode that skips dirty checking and flushing. Some drivers and databases use the read-only flag to route to a replica or to optimise, which depends on the driver.

Why do PostgreSQL fetch sizes depend on auto-commit?

The PostgreSQL JDBC driver only streams results through a server-side cursor when auto-commit is off, because the cursor lives inside a transaction. In auto-commit mode it reads the whole result into memory regardless of the fetch size. That is why large reads should run inside a transaction.

What happens if code calls commit() on a connection inside a Spring transaction?

It commits the work done so far behind Spring's back, so a later rollback cannot undo it, and the transaction no longer behaves as one unit. Code inside a Spring-managed transaction should never call commit, rollback or setAutoCommit on the connection directly; it should let the transaction manager do it.

7Conclusion

In this answer, we saw that JDBC connections start in auto-commit mode, where each statement commits on its own, and that we group statements with setAutoCommit(false) followed by commit() or rollback(). Spring's DataSourceTransactionManager and JpaTransactionManager switch auto-commit off when a transaction begins and restore it afterwards, while statements outside a transaction run in auto-commit mode. HikariCP's autoCommit sets the pool default, and turning it off together with the Hibernate hint saves calls but requires every write to go through a transaction.

What to say in the interviewAbout 90 seconds aloud

Open with

Every new JDBC connection starts with auto-commit on, which means the driver commits each statement as soon as it finishes, so each statement is its own transaction. If I need several statements to succeed or fail together, like a debit and a credit, I call setAutoCommit(false), run them, then call commit, or rollback if something failed, and I restore auto-commit before the connection goes back to the pool.

Then, if they want more

Spring does this for me. When a @Transactional method starts, DataSourceTransactionManager takes a connection, switches auto-commit off if it was on, and binds the connection to the thread so every JdbcTemplate call uses it. At the end it commits or rolls back and turns auto-commit back on. JpaTransactionManager does the same through Hibernate.

So the important consequence is that any statement outside a Spring transaction runs in auto-commit mode: two JdbcTemplate calls in a method without @Transactional are two separate commits. On the pool side, HikariCP's autoCommit property sets the default for its connections.

Some teams set it to false and tell Hibernate with the provider_disables_autocommit property, so Hibernate does not toggle the flag on every transaction, though I would check the exact property for the Hibernate version. That only works safely when all writes go through transactions, because uncommitted work is rolled back when the connection returns to the pool.

It also matters for PostgreSQL, whose driver only uses a cursor for fetch size when auto-commit is off.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
java.sql, package documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the auto-commit mode of a JDBC Connection, and how does Spring's transaction...": https://scrivio-interview-library.netlify.app/databases/jdbc/d116e5d4d6/