0
@Service
public class SampleClass implements SampleInterface{

    @Transactional
    public void methodA(){
        SampleClass bean = ApplicationContext.getBean(SampleClass.class);
        bean.methodB();
    }

    @Transactional
    public void methodB(){
    //statements.
    }
}

is there any problem calling methodB() like that in the same class? I see lot of code like that. I don’t feel it is the right way. But I don’t know what are the consequences. Could anyone please let me know?

  • It makes no sense at all. Why not call `methodB` directly? `@Service` beans are supposed to be singletons, thus this will have the same effect. The only thing you've here is an unecessary degree of complexity and IoC principle violation. – Aritz Aug 30 '16 at 17:03
  • It's probably used to start a new transaction, where the method B would use REQUIRES_NEW. If that's the case, it would be much cleaner to put methodB in another bean, injected in SampleClass. – JB Nizet Aug 30 '16 at 17:54
  • @JBNizet Inorder to start a new transaction when methodB is invoked, we can specify @Transactional(propagation=Propagation.REQUIRES_NEW) – Bala Challa Aug 30 '16 at 18:28
  • it makes sense when you want to go through the proxy object of Sample class rather than directly calling the method . Transaction handling in spring is implemented using proxy objects, I guess may be because of that your code has such style of method calls – Zulfi Sep 29 '16 at 13:16

0 Answers0