3

I have achieved using AspectJ to add @Transactional to non-Bean class and non-public methods. However, I still can't make self-invocation success.

This is my transcation manager config class

@Configuration
@EnableTransactionManagement
public class DBConfig {
    @Bean(name = "transactionManager")
    public PlatformTransactionManager transactionManager() {
        DataSourceTransactionManager txManager = new DataSourceTransactionManager(DATA_SOURCE);
        AnnotationTransactionAspect.aspectOf().setTransactionManager(txManager);
        return txManager;
    }
}

This is my loadtime weaver config

@Configuration
@EnableLoadTimeWeaving
public class AspectJConfig implements LoadTimeWeavingConfigurer {

    @Override
    public LoadTimeWeaver getLoadTimeWeaver() {
        return new InstrumentationLoadTimeWeaver();
    }
}

And this is my self-invocation codes

public class Test {
    @Transactional
    public void testA() {
        testB();
        //......
    }

    @Transactional(propagation = Propagation.NEVER)
    public void testB() {
        //......
    }
}

When I call testA, It's expected that it will throw exception because I have defined the propagation as NEVER. However, actually nothing happened.

So would anyone help me???

kissrain
  • 95
  • 1
  • 10
  • Did you try `((Test) AopContext.currentProxy()).testB()`? – Ali Dehghani Feb 25 '16 at 09:47
  • Possible duplicate of [Spring @Transactional Annotation : Self Invocation](http://stackoverflow.com/questions/23931698/spring-transactional-annotation-self-invocation) – WeMakeSoftware Feb 25 '16 at 09:49
  • `@EnableTransactionManagement(mode=AdviceMode.ASPECTJ)` else it will still use proxies. Also you don't need to set the transaction manager the configuration class will take care of that. – M. Deinum Feb 25 '16 at 10:37
  • @AliDehghani No, it does not work for me. The result shows the proxy is null – kissrain Feb 25 '16 at 10:54
  • @M.Deinum Thx, but still does not work for me.... – kissrain Feb 25 '16 at 11:14
  • And how are you testing? – M. Deinum Feb 25 '16 at 11:19
  • @M.Deinum Just do what you have told me... After add mode explicitly, call testA(), however, no exception is thrown – kissrain Feb 25 '16 at 13:00
  • Well obviously yu are calling that method but HOW are you calling that method. On a spring configured instance, are you creating a new instance yourself... How... Show the test code. – M. Deinum Feb 25 '16 at 13:08
  • @M.Deinum Very simple... Test class is a mybatis service class. And just call test.testA(); very simple! – kissrain Feb 26 '16 at 02:23
  • You are still not telling how you test it. As stated you are obviously calling the method, but how, on which object instance a new one, one injected. Show the test code. – M. Deinum Feb 28 '16 at 12:18

0 Answers0