Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring: Annotation-driven Transaction Manager

I'm setting up a new, JPA+Spring project. What is the difference (for me as a programmer) between:

<tx:annotation-driven transaction-manager="transactionManager" />

and

<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />

in my applicationContext.xml?

like image 601
John Manak Avatar asked Nov 11 '10 15:11

John Manak


People also ask

Which annotation is used for transaction management in Spring?

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. transaction-manager default value is transactionManager but I am still having it to avoid confusion.

Which of the following is used to enable annotation-driven transaction management?

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 @transaction annotation in Spring?

So when you annotate a method with @Transactional , Spring dynamically creates a proxy that implements the same interface(s) as the class you're annotating. And when clients make calls into your object, the calls are intercepted and the behaviors injected via the proxy mechanism.

Is it sufficient to annotate the classes with the @transactional annotation?

You can place the @Transactional annotation before an interface definition, a method on an interface, a class definition, or a public method on a class. However, the mere presence of the @Transactional annotation is not enough to activate the transactional behavior.


1 Answers

There is a huge difference between Proxies and byte code woven aspects. Proxies can only intercept if the invocation comes from “outer space”, but not if the invocation comes from the object itself (this.transactionalMethod())

This means if you have a Class with two methods, T and B. Method T has a transaction annotation, and method B invokes T by “this.T()”, then the proxy is never invoked (for T) so there is no transaction handling in this case!

If you use AspectJ the transaction handling code is woven in the byte code of T, and it will be executed no matter if the invocation comes from the object itself or from an other object.

like image 172
Ralph Avatar answered Oct 30 '22 02:10

Ralph