Announcement Announcement Module
No announcement yet.
Spring Declarative Transactions in EJB Bean Managed Transaction Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Declarative Transactions in EJB Bean Managed Transaction

    I have a situation where it seems appropriate to use EJB (EJB 3). We have a few simple service interfaces with complex business logic accessed by multiple clients. I have looked into using the EJB Container Managed Transactions, and this may be the more appropriate solution, but I am not happy with the fact that in some instances I still need to programmatically set rollbacks etc. I have implemented a solution using Bean Managed Transactions and then applying Spring's declarative transactions using annotations, but I am not real happy with this approach either. Hopefully I am just missing something.

    My solution involves having two implementations of the service interface. One is simply a facade that is managed by the EJB container. The facade holds an instance of same service interface that is injected with a Spring managed instance that contains the business logic and transaction boundaries. Each method implemented by the facade layer simply delegates to the Spring managed instance.

    Here is a simplified example:

    public interface MyService {
        void doSomething();
    //EJB container managed instance
    public class MyServiceFacade implements MyService {
        private MyService myService;
        public void doSomething {
    //Spring managed instance
    public class MyServiceImpl implements MyService {
        public void doSomething {
            //business logic here.
    In this example an instance of the class MyServiceImpl is created and managed by Spring. This class is then autowired into the EJB container managed instance of MyServiceFacade, the facade layer simply delegates to the Spring managed instance.

    The biggest pain point here is the additional layer the facade approach introduces. It's pretty difficult to explain to developers who are not so familiar with EJB or Spring. Plus the code in the facade layer is so boiler plate and annoying to write that we have actually introduced a couple infinite loops.

    It seems like there could be some EJB interceptor applied post construct or post activate to declaritively apply transactions to the EJB managed bean directly.