We use Spring @Transactional
annotations on business layer methods. When we moved to Java 8 these methods have been converted to use Async features of Java 8 and use completable Futures and chain several async calls to the data layer. E.g.
@Transactional
public CompletableFuture<Entity> updateEntity(ID id) {
repository1.get(id)
.thenComposeAsync(item -> repository1.save(), executor);
}
The above code demonstrates that we want to chain multiple database layer async calls.
The @Transactional
annotation seems to only support blocking calls at this time and all the transaction context information is kept in ThreadLocal
. The above method creates 3 transactions. The outer transaction starts and completes as soon as the future is returned. repository1.get(id)
runs in another transaction and repository1.save()
runs in it's own transaction.
Is there a standard way to have multiple async calls run in a single transaction without having to rewrite the transaction interceptor. Also seems like we need to copy thread local variables in TransactionSynchronizationManager
from one thread to the other to get this to execute in a single transaction.
Appreciate feedback from Spring team if they have plans to enhance spring-tx to support this use case.