Spring + EJB 3.1 CMT vs BMT transactions Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Spring + EJB 3.1 CMT vs BMT transactions without wasting too much if your time.

The question is published on by Tutorial Guruji team.

We have a simple stateless EJB timer that gets an instance of a spring service injected into it. The spring service has a method marked as transactional. When the EJB uses Transaction Management Type CONTAINER the spring service call results in an IllegalStateException: Operation not allowed. So we set the EJB Transaction Management Type to be BEAN and everything works as advertised. Is this normal? I was under the impression that the spring transaction management would join the CMT.

example EJB

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
@Interceptors(SpringBeanAutowiringInterceptor.class
public class TimerService {
    @Autowired
    IHelloService helloService;

    @Schedule(second="*/1", minute="*",hour="*", persistent=false)
    public void doWork(){
        helloService.hello();
    }
}

example spring service

@Service
public class HelloService implements IHelloService {
    @Transactional
    public void hello(){

    }
}

Answer

Probably the problem can be fixed, if you would define propagation level for Spring transaction as SUPPORTS (default is REQUIRED). I do not remember exact value for default settings in EJB, but it seems like EJB method is not starting transaction (propagation is DEFAULT), but ‘hello’ tries to do so and that is causing conflict.

We are here to answer your question about Spring + EJB 3.1 CMT vs BMT transactions - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji