Alternate Spring @Transactional configuration

Is there any alternative based on setting @Transactional annotation in Spring?

I want my java classes to be free of Spring as much as possible, that is, how can I disconnect from any structure.

+7
spring transactions
source share
2 answers

Yes, using aop:config and tx:advice . For example:

 <aop:config> <aop:pointcut id="serviceMethods" expression="execution(* com.package.service..*.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> 
+8
source share

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!

+3
source share

All Articles