Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Java JDBC interview: auto-commit, rollback and database-specific evidence

Last updated: 29 Sept 20264 min read
tutorial
IntermediateBy AITrove Editorial

A JDBC transaction groups database changes under a connection’s transaction state; rollback concerns those changes, not every action performed by the Java process.

Java 8+. This program requires the H2 JDBC driver; the downloadable Maven project declares it.

What changes when auto-commit is disabled?

With auto-commit enabled, an ordinary statement participates in the connection’s auto-commit behavior. Disabling it gives the application an explicit commit or rollback boundary. Code should set and restore required state when a pooled connection is borrowed, rather than assume the last borrower left suitable settings.

Does rollback undo an email?

No. A database transaction cannot retract an external message already sent. Applications coordinating storage with external effects need a durable handoff policy, often an outbox with deduplication. A try/catch block alone does not give distributed atomicity.

What does an H2 test prove?

The probe inserts one row in a local H2 database, rolls back and verifies absence. It checks this transaction path with the declared driver. It does not certify isolation, lock timing, SQL dialect or connection-pool behavior in a different production database.

Test concurrent conflicting transactions against the actual target engine when isolation matters. Query parameters keep values separate from statement structure; identifiers such as a table name need a different allowlist or query-building boundary.

Working program

Java
import java.sql.*;
public class JdbcRollbackProbe {
    public static void main(String[] args) throws Exception {
        try(Connection connection=DriverManager.getConnection("jdbc:h2:mem:rollback_probe")){
            try(Statement schema=connection.createStatement()){schema.execute("CREATE TABLE receipts(id INT PRIMARY KEY)");}
            connection.setAutoCommit(false);
            try(PreparedStatement insert=connection.prepareStatement("INSERT INTO receipts(id) VALUES(?)")){
                insert.setInt(1,42);insert.executeUpdate();
            }
            connection.rollback();
            try(Statement query=connection.createStatement();ResultSet result=query.executeQuery("SELECT COUNT(*) FROM receipts")){
                result.next();System.out.println("rows="+result.getInt(1));
            }
        }
    }
}

Output

Output
rows=0

Costs and boundaries

The database query and transaction costs depend on the engine, indexes, storage and contention. This tiny in-memory probe is a correctness check with H2 2.3.232, not a production throughput measurement. Connections, statements and result sets are all closed by their owning scope.

Common Mistakes

  • Rollback does not undo external side effects.
  • Driver-specific tests do not certify another database engine.
  • Do not return a pooled connection with unexplained transaction state.

Read next

Transaction failures, Bound parameters.

java
jdbc-interview
Storage details