开发者

Spring @Transactional how to attach different method to same transaction

I have a problem with the transactional configuration subclassing.

I have a class A that has this method:

@Override
    @Transactional(propagation = Propagation.REQUIR开发者_JAVA技巧ES_NEW)
    public EventMessage<ModificaOperativitaRapporto> activate(EventMessage<ModificaOperativitaRapporto> eventMessage) {
// some dao operations
        return eventMessage;
    }

Then class B subclass class A and overrides the activate method

 InserimentoCanaleActivator extends ModificaOperativitaRapportoActivator ....

    @Override
        @Transactional(propagation = Propagation.REQUIRES_NEW)
        public EventMessage<ModificaOperativitaRapporto> activate(EventMessage<ModificaOperativitaRapporto> eventMessage) {
// others dao operations 
    return super.activate(eventMessage);

I need that when the super method is executed alone has his own transaction, but when is executed the method of class B all the operations need to participate to same transaction.

Any idea?


The transaction wraps the whole method call in the bean, but it's applied at the level of the bean and not at the level of the individual class implementations of the method. Basically, Spring handles transactions by using AOP. What that does is it puts a proxy object as the bean itself. The proxy manages the starting of the transaction, runs the method in your implementation class, and then finalizes the transaction (committing or rolling back) on the way back out again. It's an elegant way to do it, and far simpler than any manual approach (given Java's limitations).

The proxy machinery, once applied (by the presence of the @Transactional annotation plus some other config), looks at the type of transaction handling to do (the propagation parameter to the annotation) and picks the right action. In your case, if you want the superclass to have @Transactional(propagation=REQUIRES_NEW) and the subclass to have @Transactional(propagation=REQUIRED) then I'd expect that to work; the superclass's annotation will be ignored because the annotation machinery will see the subclass's annotation first and stop looking.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜