r/SpringBoot • • Jan 09 '26

Question @Transactional method

What happen when I run executorsrrvice inside @Transactional method what would you offer like this scenario

2 Upvotes

31 comments sorted by

View all comments

Show parent comment

2

u/disposepriority Jan 09 '26

Ok no offence you really need to work on being able to describe problems.

Here are the scenarios I can imagine are happening:

  1. The first method will eventually return data based on the processing of the executed task and so has to wait for it

If this is the case then
A: Does the transaction need to begin within the original method? Can the executor service not call a secondary method marked as transactional?

B: If the original method does for some reason need to be part of the transaction, is there a reason a single transaction needs to be split between two threads?

C: If the original method has to wait for the result of the task and there is only one task per request, why not mark the entire original method as
`@Async
`@Transactional

2. The original method simply submits the task and fucks off, whatever is calling it does not expect an immediate response based on the processing

A: The original method should not be part of the transaction, it should submit its asynchronous task and return as soon as possible. The task should run within a transaction and that's that

These are just assumptions, provide some code if you want an answer based on specifics.