Quick revision
The short answer
Spring binds a transaction to the thread that started it, and CompletableFuture.runAsync() runs the lambda on another thread, so the save happens outside the caller's transaction. That thread also does not share the caller's persistence context, so the entity is detached there and touching its lazy associations fails with LazyInitializationException, especially once the caller's transaction has ended. The task may even run before the caller commits and not see its data. The fix is to run the async work after commit, pass ids instead of entities, and start a new transaction inside the task.
What to remember
In this answer, we saw that a Spring transaction and its persistence context belong to the thread that started them, so work handed to CompletableFuture.runAsync() runs in a different transaction with a different persistence context. The caller's entity is detached there, which leads to LazyInitializationException when the caller's session is gone, and the task can run before the caller commits. Running the work after commit, passing ids, and loading fresh data in a new transaction fixes all three problems.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Spring binds a transaction to the thread that started it, and CompletableFuture.runAsync() runs the lambda on another thread, so the save happens outside the caller's transaction. That thread also does not share the caller's persistence context, so the entity is detached there and touching its lazy associations fails with LazyInitializationException, especially once the caller's transaction has ended. The task may even run before the caller commits and not see its data. The fix is to run the async work after commit, pass ids instead of entities, and start a new transaction inside the task.
Why this question is asked
Moving slow work to another thread is a natural instinct, and CompletableFuture makes it a one-line change. The bug it causes is confusing because it is intermittent: sometimes the save works, sometimes it throws, and sometimes data is missing. Interviewers ask about it because the explanation depends on knowing where Spring keeps the current transaction. In this answer, we will recap how transactions are bound to threads, reproduce the failure, explain each symptom, and then fix it properly.
1A Quick Recap: Transactions Live in a Thread
When a @Transactional method starts, Spring's transaction manager opens a connection, and with JPA an EntityManager, a session that tracks the entities we loaded, called the persistence context. Spring stores both in TransactionSynchronizationManager, which keeps them in ThreadLocal variables, values that only the current thread can see. Every repository call made on that thread looks there and joins the same transaction.
CompletableFuture.runAsync(task) without an executor runs the task on ForkJoinPool.commonPool(), a shared pool of other threads. Those threads have their own, empty ThreadLocals. From their point of view, no transaction exists.
2Reproducing the Problem
Here is a shortened version of the code. The caller loads a customer, changes it, and hands the entity to another thread to save it.
The log line proves the first point: on the pool thread there is no transaction. repo.save() is itself transactional in Spring Data, so it starts a separate, new transaction on that thread with a new persistence context. Our save is no longer part of the caller's transaction at all; if the caller rolls back, the save may still commit, and if the save fails, the caller never knows.
3Why LazyInitializationException Appears
The entity c belongs to the caller's persistence context, not to the new one on the pool thread. To the new transaction, c is a detached entity, an object that is not tracked by its session. For a detached entity with an id, save() calls merge(), which copies its state onto a freshly loaded managed copy. To copy it, Hibernate reads the entity's associations, and if c has a lazy collection that was never loaded, such as c.getAddresses(), that collection belongs to the caller's session.
Whether that works depends on timing. If the caller's transaction is still open, the session might still be there, although Hibernate sessions are not thread-safe and using one from two threads is a bug in itself. If the caller's method has already returned, its transaction committed and its session closed, and initialising the collection fails with no Session. That race is why the failure is intermittent.
7 messages in order. Play them, or step through them one at a time.
If the pool thread is fast enough, the caller's session is still open and the save seems to work.
4The Async Work Does Not See Uncommitted Data
There is a third problem. If the caller inserts a new row and passes its id to the async task, the task may run before the caller commits. Its new transaction, at READ COMMITTED isolation, cannot see uncommitted rows, so it finds nothing, or fails a foreign key check. The task has to wait until the data it depends on is committed.
5The Fix: After Commit, Ids Not Entities, a New Transaction
The reliable pattern is to publish the work so it runs after the commit, pass only the id across threads, and load the data again in a transaction of its own on the worker thread.
Notice what each part does. AFTER_COMMIT means the listener only runs if the caller's transaction committed, so it always sees the data. @Async moves it to a thread pool managed by Spring, which we can size and monitor, rather than the shared common pool. Passing the id means no entity crosses threads, and the REQUIRES_NEW transaction gives the listener its own persistence context in which lazy loading works. If we do not want events, TransactionSynchronizationManager.registerSynchronization() with an afterCommit() callback gives the same hook.
6Follow-up questions
Can I pass the transaction to the other thread?
Not in a supported way. Spring's transaction state is bound to one thread, and JDBC connections and Hibernate sessions are not designed for use from two threads at once. If the work must be in the same transaction, do it on the same thread; otherwise give it its own transaction.
Would a custom executor or context propagation fix it?
A Spring-managed executor is better than the common pool for sizing and monitoring, and context propagation libraries can copy things like the security context or tracing ids. Neither copies the transaction or persistence context, so the fix is still ids plus a new transaction.
What if the async work must succeed for the business operation to be valid?
Then it should not be fire-and-forget. Either do it synchronously in the transaction, or write an outbox row in the same transaction and let a separate process perform the work reliably, with retries.
7Conclusion
In this answer, we saw that a Spring transaction and its persistence context belong to the thread that started them, so work handed to CompletableFuture.runAsync() runs in a different transaction with a different persistence context. The caller's entity is detached there, which leads to LazyInitializationException when the caller's session is gone, and the task can run before the caller commits. Running the work after commit, passing ids, and loading fresh data in a new transaction fixes all three problems.
Open with
Spring transactions are bound to the thread. The transaction manager stores the connection and the EntityManager in ThreadLocals through TransactionSynchronizationManager, and CompletableFuture.runAsync runs the lambda on the common ForkJoinPool, where none of that exists.
Then, if they want more
So repo.save runs in its own new transaction, which Spring Data starts because save is transactional, and it is not part of the outer transaction at all: it can commit when the caller rolls back.
The entity was loaded in the caller's persistence context, so on the other thread it is detached; save calls merge, merge reads its lazy associations, and those belong to the caller's session. If the caller has already committed and closed its session, that throws LazyInitializationException, which is why it is intermittent.
There is also a timing issue: the task may run before the caller commits and not see its data. My fix is to publish an event with just the id, handle it with @TransactionalEventListener in the AFTER_COMMIT phase, make it @Async on a Spring-managed executor, and give the listener its own REQUIRES_NEW transaction that loads the entity fresh. If the work is essential, I would use an outbox instead of fire-and-forget.
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
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
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 "Inside a @Transactional method, code calls CompletableFuture.runAsync(() ->...": https://scrivio-interview-library.netlify.app/databases/transactions/5e8f625f8e/