-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@TransactionalEventListener cannot work with TaskExecutor #25129
Comments
What |
I wanted to use |
It does matter. A transaction, in essence, is request (thread-) scoped. If you run in the |
Then I won't argue :D. |
Yes, it won't, for the reason I've exposed (the context, in the new thread, does not have a transaction at all). Considering that you should be able to impact the transaction at that phase, I am going to close this issue now. |
Wow, that was quick. What you say makes sense. What I was trying to say, though, is: |
The default behaviour is to invoke events on the same thread so if you don't do anything you'll get the expected result.
I am not sure what you mean by that but if you are configuring the event multicaster yourself and you want to be able to handle transactional events, then, yes, it must run on the same thread. |
Hum, I cannot confirm that. Method
Spring sets a task executor for this bean (of type |
I don't think that's accurate.
Please share a small project (github repo or zip we can download) that reproduces what you've described. |
I can't provide a sample project quickly, but this is the callstack when I end up in the setter that sets the task executor: setTaskExecutor:81, SimpleApplicationEventMulticaster (org.springframework.context.event) |
Thanks but that doesn't help. If no configuration on your end is required, you should be able to replicate that in a mini sample. |
Affects: Spring 4.2
I came to believe that a
@TransactionalEventListener
cannot work with aapplicationEventMulticaster
bean that uses ataskExecutor
that invokes the listener call in a new / different thread.Inside a new thread, which cannot be transaction synchronized in my understanding, the call to the listener will be discarded, unless the
@TransactionalEventListener(fallbackExecution = true)
is set. So, proper coupling to a transaction seems to be impossible.Should you desire a true asynchronous invocation on other event handlers, you will have to provide a task executor that delegates the invocation to different threads.
JavaDoc doesn't indicate that
@TransactionalEventListener
cannot work with a non-synchronous task executor in place. But, I might be doing something wrong after all.As far as I can tell, the code in spring-tx library behaves as it should, but can only work when no task executor is used at all in the event multicaster, or an executor like
org.springframework.core.task.SyncTaskExecutor
is used.The text was updated successfully, but these errors were encountered: