Fine-grained transactions in Spring

| | bookmark | email | 1 comments
In the previous post I have presented how an annotation-driven development session in Eclipse can be very simple. Today I will present a solution for fine-grained transactions with Spring.
Currently Spring supports different transaction management approaches:
  • programmatic (through the direct usage of PlatformTransactionManager)
  • declarative (through the usage of TransactionProxyFactoryBean and TransactionInterceptor)
  • declarative transaction demarcation through source level metadata (based on commons-attributes and TransactionInterceptor)
  • transaction demarcation based on real JDK1.5 annotations (I am not sure yet how they are working in background)
While for most of cases one of the above solutions should work, I wanted to have a solution that will allow me to exactly match the transactional methods (not implying some naming conventions as required by 2), not to be limitted on having all transactional methods declared as public and most important not to be based on proxies (some of the above solutions are based on interceptors which are in fact proxies, either normal or cglib proxies).
So my current solution is based on javadoc-like annotations and AspectWerkz taking into account that AspectWerkz allows the definition of pointcuts based on annotations (Note: the solution will work also with JDK1.5 annotations).
I have declared as in the previous post the Transaction interface, its corresponding annotation.properties and now I define the following aspect:

The full implementation of TransactionManagerAspect is based on the logic of TransactionInterceptor and respectively TransactionAspectSupport.
To end up this short entry I will present the pros and cons (at least those i can see by myself):
Pros
  • non-public methods are allowed to be marked in transactions
  • more refined pointcut definition (no regex naming usage needed)

Cons:
  • metadata bound to source code and not external
  • post-compilation annotation processing (required only for the javadoc-like annotations)

All is left is to drive some small benchmark to see what is the time consumption on each of the above solutions.
If any of you is interested in the full code drop me a line and I will try to package the things for you.
By the way: this is not a Mardi Gras entry [blink/].