When writing transactional methods with @Async it is not possible to throw @Transactional exceptions. Like ObjectOptimisticLockingFailureException , because they are thrown outside the method itself, for example, when a transaction is committed.
Example:
public class UpdateService { @Autowired private CrudRepository<MyEntity> dao;
I know that I can catch @Async exceptions as a whole as follows:
@Component public class MyHandler extends AsyncConfigurerSupport { @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return (ex, method, params) -> {
But I would prefer to handle this exception in a different way ONLY if it occurs in the UpdateService .
Question: how can I catch it inside UpdateService ?
The only chance: create an extra @Service that wraps the UpdateService and has a try-catch ? Or am I better?
source share