1

I'm getting the following exception:

Exception data: javax.ejb.IllegalLoopbackException: Cannot call a method on a singleton session bean while constructing the bean instance : MyWar.war#BarProducer

My code is as follows.

I have a Stateless Session Bean that Injects both Foo and Bar.

@Stateless
public class MySessBean {

    @Inject
    private Foo foo;

    @Inject
    private Bar bar;

    public SomeData myMethod1(...){
        //does something with Foo
        foo.xyz();
    }

    public SomeData myMethod2(...){
        //does something with Bar
        bar.xyz();
    }   

}

I have a producer that creates the Singleton Foo:

@Singleton
public class FooProducer {

    @Produces
    public Foo getFoo() {
         return new Foo();
    }
}

I have another producer that creates the Singleton Bar. In order to create Bar I need to Inject Foo:

@Singleton
public class BarProducer {

    @Inject
    private Foo foo;

    @Produces
    public Bar getBar() {
            //uses Foo
        foo.xyz();
    }
}

I'm using WebSphere 8 (OpenWebBeans). I figured the container would know it needs to craete Foo singleton before it create Bar singleton??

DarVar
  • 16,882
  • 29
  • 97
  • 146

1 Answers1

1

you're not actually using CDI here. This is an EJB issue. The problem is that you're not specifying a @DependsOn for your EJBs. See here: http://docs.oracle.com/javaee/6/api/javax/ejb/DependsOn.html

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • 1
    Wow, another JEE6 concept I wasn't aware of. Could you add a sniplet showing how @DependsOn would be used in the given example? – Jan Galinski Oct 25 '13 at 08:38
  • On `BarProducer` add `@DependsOn("FooProducer")` at the class level. – John Ament Oct 25 '13 at 11:40
  • Thanks ... one correction: since the components are not @Named, I suppose you mean "fooProducer", right? – Jan Galinski Oct 25 '13 at 12:25
  • The value is your bean name, following EJB naming conventions. – John Ament Oct 25 '13 at 13:02
  • I guess javax.ejb.DependsOn can only be used with javax.ejb.Singleton? Is their a CDI DependsOn annotation for javax.inject.Singleton? – DarVar Oct 25 '13 at 13:30
  • Oh I just noticed javax.inject.Singleton is not CDI. So I guess the question should be: Is there a DependsOn CDI annotation for javax.enterprise.context.ApplicationScoped? – DarVar Oct 25 '13 at 13:40
  • No, there is no equivalent. But if that's the case (e.g. you didn't include imports in your example), the websphere error is very odd. The error seems to explicitly note that your `BarProducer` is a `javax.ejb.Singleton` `singleton session bean while constructing the bean instance : MyWar.war#BarProducer` – John Ament Oct 25 '13 at 21:51
  • Suggest you add the detailed examples and explanations from these comments up into the answer itself. – dbreaux Nov 08 '13 at 16:38