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
What happens is the following
Call 1:
Call 2:
The prefered result should be:
Call 1:
Call 2:
The @Transactional annotation is the metadata that specifies the semantics of the transactions on a method. We have two ways to rollback a transaction: declarative and programmatic. In the declarative approach, we annotate the methods with the @Transactional annotation.
@Transactional(MANDATORY) : fails if no transaction was started ; works within the existing transaction otherwise. @Transactional(SUPPORTS) : if a transaction was started, joins it ; otherwise works with no transaction.
The @Transactional annotation is metadata that specifies that an interface, class, or method must have transactional semantics; for example, "start a brand new read-only transaction when this method is invoked, suspending any existing transaction".
You certainly can place the @Transactional annotation on an interface (or an interface method), but this works only as you would expect it to if you are using interface-based proxies.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With