The combination of sleep mode @Transactional & ehcache @Cacheable always creates a database transaction

When doing loadtesting in our application, I noticed that if you use @Transactional and @Cacheable annotations, which hibernate always creates a database transaction. Is there an easy way to prevent this? An easier way to solve this problem in spring is this: class / interfaces

  • Servicelayer interface
  • A cached annotated class that is just a proxy / redirect to
  • Annotation Transaction Deployment Class

What's happening:

Call 1:

  • Transaction created.
  • class method called
  • the result is cached and returned

Call 2:

  • Transaction created.
  • Caching result is returned

The preferred result should be:

Call 1:

  • Transaction created.
  • class method called
  • the result is cached and returned

Call 2:

  • Caching result is returned
+7
source share
1 answer

You need to change the relative order of the @Transactional and @Cacheable .

It can be configured using the attribute order <tx:annotation-driven> and <cache:annotation-driven> . See 8.2.4.7 Setting order for value of order value.

+9
source

All Articles