r/SpringBoot • u/iamwisespirit • Jan 09 '26
Question @Transactional method
What happen when I run executorsrrvice inside @Transactional method what would you offer like this scenario
2
Upvotes
r/SpringBoot • u/iamwisespirit • Jan 09 '26
What happen when I run executorsrrvice inside @Transactional method what would you offer like this scenario
View all comments
Show parent comment
2
u/zattebij Jan 09 '26 edited Jan 09 '26
Apart from manual transaction management, you could also just call another
@Transactionalmethod from within the task in the worker thread.Of course, if using proxies (Spring default), that method would need to be in another component. If using (compile or load time) weaving, the nested transactional method can just be in the same component (the transactional around-aspect is weaved into the bytecode of your implementation class, rather than Spring generating a proxy subclass with the TX begin prolog and the TX commit/rollback epilog around the
superinvocation of your method implementation). Look into AspectJ which supports weaving.Also note that you cannot just use any entity from your main thread in these worker threads. Lazy loads won't work from these other threads (other sessions actually, but a Session/EntityManager is also thread-bound, like a transaction). Either load entities fresh in the worker threads (by ID), or use
EntityManager.mergeto get a copy of the entity for use in the worker thread.If you have such a pattern of:
- querying a lot of entities from DB;
- then distributing work on these entities across worker threads for parallel processing;
- and you wish each entity to be processed independently (in a separate transaction, so if one fails, it doesn't interfere with others),
... then consider using projections for the initial list rather than managed entities. Projections are unmanaged DTOs that you can safely pass around to worker threads of an
ExecutorService. You may even do most of the work inside the worker threads using this DTO, and only load the actual entity if there is some change to be saved to DB (or even then, not loading the entity but using a query to persist the change to DB). Note that projections, not being managed, don't support any lazy loading, so you'll have to query for the data you know in advance will be needed inside the worker threads.