views:

87

answers:

1

I have to use 3 different transaction managers in my webapp. So I wrote my own Annotation according to the Spring reference (Section 10.5.6.3 Custom shortcut annotations).

One annotation (for using one specific transactionmanager) looks like this:

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.transaction.annotation.Transactional;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Transactional("customer")
public @interface CustomerTX{


}

Everything is working fine when annotating my service layer with the customized @CustomerTX annotation. But I have to provide more options for my annotation, like readonly=true, rollbackFor= and so on. As you cannot "extend" an annotation (I really just need to extend the @Transactional annotation from Spring), whats the correct implementation for this?

A: 

You will have to create several custom annotations, I'm afraid, one for every use case, annotating each with the exact @Transactional annotation you need.

Or you will have to write your own aspect in AspectJ ( extend org.springframework.transaction.aspectj.AbstractTransactionAspect from spring-aspects.jar ) to create your own transaction logic.

seanizer
Do you really mean I have to write something like this (assuming I just want to have set the transaction to readonly):@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Transactional(value="customer",readOnly=true)public @interface CustomerTXReadonly{}This can't be the only working solution...
tim.kaufner
I'm afraid so, because of the limitations of annotations. As I said: either that or writing your own aspects (which I would prefer)
seanizer
Thanks for your help. I'll try writing my own aspects. It's a pity that annotations aren't extendable :(
tim.kaufner
Grab a copy of `AspectJ in Action`: http://www.manning.com/laddad/ It's a very good book about AspectJ with and without Spring
seanizer