'Does Spring Boot "@Transactional" annotation apply to EhCache-backed caches?
Spring Boot, JPA (Hibernate) and Java/JSR-107 Caching (EhCache) here.
I have a service method where I need to update both a table and a cache at the same time:
@Service
public class WorkService {
@Autowired
private PayloadRepository payloadRepository;
@Autowired
private Cache<String,Payload> payloadCache;
// @Transactional ???
public void doSomeWork(Payload payload) {
// ... do some stuff up here
payloadRepository.save(payload);
// ... do some more stuff
payloadCache.put(payload.getLabel(), payload);
// ... do some finishing stuff down here
}
}
I would like to make it so that both repository and cache lines above are treated transactionally; that is if there are any (uncaught? unexpected?) exceptions thrown from the doSomeWork(...) method, the payloadRepository.save(...) would rollback and the payloadCache.put(...) would also rollback. Meaning the new payload would not exist either in the DB table or the cache.
I have perused the Spring Boot @Transactional docs, the Javax transactionality & XA docs, and the EhCache docs and I can't seem to determine whether I can just add @Transactional to this method and get the desired behavior or not.
If I can, I'd appreciate a quick explanation of why the @Transactional anno would apply to the cache as well as the repository. If not, I guess I'm also hoping to get an explanation of why as well as what I would need to do to get this desired transactional behavior.
Again the desired state is: if anything uncaught/unexpected gets thrown from inside this method, if the repository save(...) method or the cache put(...) method have already been executed, I want them to rollback.
Thanks in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
