Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optimal @EnableTransactionManagement Configuration

I'm pretty confused by this topic and see very little advice in the Spring 4.x documentation. First off let me state that I can configure my project any way I want. Static weaving, compile time weaving, none, etc.

I just want the optimal performance. But if the performance differences are microseconds then I just want simplest. I only care about a performance difference that would impact my customers.

Okay, so here are the relevant options:

  1. adviceMode=proxy, proxyTargetClass=true
  2. adviceMode=proxy, proxyTargetClass=false
  3. adviceMode=aspectJ, compile-time weaving
  4. adviceMode=aspectJ, load-time weaving

I'm okay with adding some time to my application initialization as long as it is fast while running.

So what are the trade offs between these four ways of configuring transaction management?

Are there feature trade-offs too or is this just a performance issue?

Note that I am using Spring 4.0.2 and Java 1.7. If there is Spring 4.0 documentation that basically answers this question then I apologize and just redirect me to the doc. So far I've seen documentation saying how to set them up but not how they compare. Thanks!

Also note that most of the information on the web about this is really out of date. So I'm looking for current comparisons.

like image 771
robert_difalco Avatar asked Mar 21 '14 20:03

robert_difalco


People also ask

What is the use of @EnableTransactionManagement annotation?

Annotation Type EnableTransactionManagement. Enables Spring's annotation-driven transaction management capability, similar to the support found in Spring's <tx:*> XML namespace. To be used on @Configuration classes to configure traditional, imperative transaction management or reactive transaction management.

What is the use of TX annotation-driven?

tx:annotation-driven element is used to tell Spring context that we are using annotation based transaction management configuration. transaction-manager attribute is used to provide the transaction manager bean name.

What is @transactional in spring boot?

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".

Is transaction enabled by default in spring boot?

However, if we're using a Spring Boot project and have a spring-data-* or spring-tx dependencies on the classpath, then transaction management will be enabled by default.


1 Answers

This configuration says how the transaction aspect will be applied. Briefly:

adviceMode=proxy, proxyTargetClass=true Cglib is used as proxy mechanism. If you use this, cglib must be on classpath, your proxied classes must have nonparametric constructor and they can't be final (cglib creates a child class as the proxy).

adviceMode=proxy, proxyTargetClass=false Jdk proxy mechanism is used. You only can proxy classes that implements a interface for methods that should be transactional. Jdk proxy can be type casted to the interfaces but can't be type casted as the original proxied class.

So, for adviceMode=proxy, the decision relies more on how are your code standards and what constraints result from used proxy mechanism.

adviceMode=aspectJ uses aspectJ library, which does byte code intrumentation instead of proxying.

adviceMode=aspectJ, compile-time weaving You should incorporate aspectJ instrumentation during a build process in your build scripts.

adviceMode=aspectJ, load-time weaving Instrumentation is performed on runtime. You have to put the aspectj agent as jvm parameter.

Using aspectJ is more powerful and probably more performant. It is also less invasive in terms of restrictions put on the classes you want to be transactional. However, proxy mode is simple Spring's out of the box solution.

More on proxies here http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch08s06.html. More on aspectJ with spring here http://docs.spring.io/spring/docs/3.0.0.M4/reference/html/ch07s08.html.

like image 84
Libor Subcik Avatar answered Sep 23 '22 11:09

Libor Subcik