Annotation is the best choice for marking that the method should be executed in a transaction. This is recommended for both Spring and EJB 3.
The XML approach requires much more configuration, is not suitable for refactoring, and you should see in the configuration if any method is executed in a transaction or not.
Since annotation-based transaction support is the preferred choice for most developers, and you don't like to use Spring @Transactional annotation, I recommend that you use custom annotation.
Then you have two options:
- Let your custom annotation extend Spring
@Transactional and use the <tx:annotation-driven /> element in your Spring configuration. This is easy and only one annotation needs to be updated to remove the Spring dependency. - Create an interceptor that executes the logic before and after the annotated method. When using Spring as a container, you must delegate transaction processing from the interceptor before and after providing recommendations for your preferred
PlatformTransactionManager implementation.
I wrote about how you can create an interceptor that adds logic before and after the method marked with the annotation here . And demonstrated what methods you should use in the PlatformTransactionManager here .
Hope this helps!
Espen
source share