Scrivio Interview library
DebuggingIntermediate6 min read · updated 12 November 2026

A Spring service calls a stored procedure through @Procedure inside a @Transactional method. The procedure commits internally, and when the Java code later fails, half the data is committed. Explain and fix.

A COMMIT inside the procedure ends the transaction that Spring started on that connection, so everything written so far, by the procedure and by the Java code before the call, becomes permanent, and Spring's later rollback can only undo what came after. The fix is to remove transaction control from the procedure and let the caller own the boundary; if the procedure must commit, for example a long batch, we call it outside the business transaction and design compensating steps, and every procedure's contract documents its transaction behaviour.

Why this question is asked

This bug is surprising because the Java code looks perfect: one @Transactional method, a repository call, an exception, a rollback. Yet the data shows half a business operation committed. The cause lives in the database code, which many Java developers never read. Interviewers ask this to test whether we understand that a transaction belongs to a connection, not to an annotation, and how each database lets procedures interfere with it. In this answer, we will recap how Spring's transaction maps to the connection, reproduce the partial commit, look at how Oracle, MySQL and PostgreSQL differ, and go through the fixes.

1A Quick Recap of Who Owns the Transaction

When a @Transactional method starts, Spring takes a connection from the pool, turns off auto-commit, and binds that connection to the current thread. Every repository call in the method, including the @Procedure call, uses the same connection. At the end, Spring calls commit on the connection, or rollback if an exception escaped.

So the transaction is not something Spring holds in Java. It is the state of that database connection: everything executed since the last commit. Spring's rollback means rollback everything since the last commit on this connection. If something else has already committed on it, Spring has no way to know or to undo that.

2Reproducing the Partial Commit

Here is the shape of the failing code. The service writes an order, calls a procedure that books stock and commits, then fails while charging the payment.

Java
@Transactional
public void placeOrder(OrderRequest req) {
orders.save(new Order(req)); // insert order 501, flushed before the call
stock.reserve(req.productId(), req.qty()); // @Procedure reserve_stock: updates stock, then COMMIT
payments.charge(req); // throws PaymentDeclinedException
} // Spring calls rollback()
// Database afterwards (Oracle):
// order 501 exists, stock reduced, no payment -> half the operation committed
Step throughHow the procedure's COMMIT ends Spring's transaction
ServiceSpringDatabase
begin (auto-commit off)
insert order 501
call reserve_stock: update stock
COMMIT inside the procedure
charge fails: exception
rollback: nothing left to undo

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

Order and stock are already permanent when Spring rolls back.

Notice that even the order insert, which happened in Java before the procedure was called, survived. Hibernate flushed it before calling the procedure, so it was part of the connection's open transaction, and the procedure's COMMIT committed it along with the stock change. Spring's rollback then had nothing left to undo except work after the commit.

3How Each Database Lets This Happen

In Oracle, a COMMIT inside a PL/SQL procedure commits the session's whole transaction, exactly as in the example. Oracle also has autonomous transactions: a procedure marked PRAGMA AUTONOMOUS_TRANSACTION runs in a separate, independent transaction, so it commits only its own work, which then survives the caller's rollback. That is meant for audit logs, and misused it causes the same kind of half-committed result. MySQL behaves like the first case: COMMIT in a procedure commits the current transaction, and some statements, such as DDL, commit implicitly.

PostgreSQL is stricter, and the details matter. A procedure invoked with CALL may run COMMIT only when the CALL itself is not inside an explicit transaction block. Inside a Spring transaction, with auto-commit off, the driver has already started a transaction, so a COMMIT in the procedure fails with an error about invalid transaction termination instead of committing. In PostgreSQL, partial commits like ours come from calling the procedure outside the business transaction, for example on a separate connection, with REQUIRES_NEW, or with auto-commit on, or from tricks such as dblink, which runs work on another connection. So the question's premise, a procedure committing inside the caller's transaction, is accurate for Oracle and MySQL; on PostgreSQL we would see an error, unless the call ran outside the transaction.

Procedure commits by database
DatabaseCOMMIT in a procedure, in a transactionIndependent commit
OracleCommits the whole session transactionPRAGMA AUTONOMOUS_TRANSACTION
MySQLCommits the current transactionNot available inside one session
PostgreSQLError: invalid transaction terminationSeparate connection, e.g. dblink

4Fix 1: Let the Caller Own the Transaction

The clean fix is a rule: procedures that are called as part of a business operation do not contain COMMIT or ROLLBACK. They do their work and return, or raise an error, and the caller decides the boundary. Then the procedure's changes are part of Spring's transaction, and the rollback after the payment failure removes the order and the stock change together.

SQL
-- Before (Oracle)
procedure reserve_stock(p_product in number, p_qty in number) is
begin
update stock set available = available - p_qty where product_id = p_product;
commit; -- ends the caller's transaction
end;
-- After
procedure reserve_stock(p_product in number, p_qty in number) is
begin
update stock set available = available - p_qty where product_id = p_product;
-- no commit: the caller commits or rolls back
end;

After the change we add an integration test against the real engine: call placeOrder with a payment that fails, and assert that neither the order nor the stock change is in the database.

5Fix 2: If It Must Commit, Call It Outside the Business Transaction

Some procedures commit on purpose, such as a batch that processes a million rows and commits every 10,000 to keep undo space and locks small. Those cannot join a business transaction. We call them separately, outside any @Transactional method or with Propagation.NOT_SUPPORTED, and design for partial completion: the procedure is idempotent or restartable from a checkpoint, and if a later step fails, a compensating step, an explicit reverse action such as releasing the reserved stock, undoes the effect.

In the order example, if reserve_stock could not be changed, we would reserve stock first in its own transaction, then place the order and charge in a second, and release the stock if that second step fails.

6Document the Contract

Every procedure's contract should state its transaction semantics: does it commit, does it use autonomous transactions, which errors it raises and whether partial work can remain. Without this, every Java team has to discover the behaviour by incident. A short header comment in the procedure and a line in the repository method's Javadoc are enough.

7Follow-up questions

Would REQUIRES_NEW around the procedure call help?

It isolates the damage rather than fixing it. The procedure then commits in its own transaction, which is honest about what happens, but the outer operation can still fail afterwards, so we still need a compensating step. It is useful when we have chosen Fix 2 deliberately.

How do we find procedures that commit?

Search the procedure source in the catalog or the migration scripts for commit, rollback and autonomous transaction pragmas, and for DDL, which commits implicitly in Oracle and MySQL. A quick inventory of these is worth doing before any work that wraps procedure calls in Java transactions.

Does Hibernate flush before calling the procedure?

In Hibernate's default auto flush mode, pending changes are flushed before a native query or procedure call so the database sees them. That is why the Java insert in our example was part of the transaction the procedure committed. Without the flush, the insert would have been lost entirely rather than committed.

8Conclusion

In this answer, we saw that a transaction belongs to the connection, so a COMMIT inside a procedure ends the transaction Spring started and makes earlier work permanent before Spring's rollback. We compared Oracle and MySQL, where this happens directly, with PostgreSQL, where a COMMIT inside a caller's transaction raises an error and partial commits come from calling outside it. We fixed it by removing transaction control from procedures, calling deliberately committing procedures outside the business transaction with compensating steps, and documenting every procedure's transaction behaviour.

What to say in the interviewAbout 80 seconds aloud

Open with

The transaction is a property of the database connection, not of the annotation. Spring starts it by turning off auto-commit, and every repository call in the method, including the @Procedure call, runs on that connection.

Then, if they want more

If the procedure executes COMMIT, it commits everything on the connection so far, including the order Hibernate flushed before the call. When the Java code then throws, Spring rolls back, but only work after that commit can be undone, so we end up with half the operation committed.

In Oracle and MySQL a commit in a procedure does exactly this, and Oracle's autonomous transactions commit independently of the caller. PostgreSQL is stricter: a procedure called with CALL can only commit when it is not inside a transaction block, so inside a Spring transaction it would raise an error, and partial commits come from calling it outside the transaction.

The fix I prefer is to remove commits from procedures used in business operations and let the caller own the boundary, with a test that a failure afterwards rolls everything back. If a procedure must commit, like a long batch, I call it outside the business transaction, make it restartable, and add compensating steps. And I would document each procedure's transaction behaviour in its contract.

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.
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 "A Spring service calls a stored procedure through @Procedure inside a @Transactional...": https://scrivio-interview-library.netlify.app/databases/jpa-queries/35f3aa1b40/