11

While doing loadtesting on our application I noticed that if you use @Transactional and @Cacheable annotions that hibernate always creates a database transaction. Is there an easy way to prevent this? A more eleberate way to solve this within spring is to have following class/interfaces

  • Servicelayer-interface
  • Cacheable annotated class which is just a proxy/forward to
  • Transactional annotation implentation class

What happens is the following

Call 1:

  1. Transaction gets created
  2. class method gets called
  3. result cached & returned

Call 2:

  1. Transaction gets created
  2. Cached result gets returned

The prefered result should be:

Call 1:

  1. Transaction gets created
  2. class method gets called
  3. result cached & returned

Call 2:

  1. Cached result gets returned
user1344117
  • 195
  • 2
  • 7
  • it isn't Hibernate that is creating the connection, it's Spring that does so prior to the method invocation that you have marked @Transactional – Hans Westerbeek May 21 '12 at 11:08

1 Answers1

13

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

It can be configured using order attribute of <tx:annotation-driven> and <cache:annotation-driven>. See 8.2.4.7 Advice ordering for the meaning of order values.

axtavt
  • 239,438
  • 41
  • 511
  • 482