This discussion is archived
3 Replies Latest reply: Oct 15, 2012 1:35 AM by r035198x RSS

Using @EJB in both parent and child fails

964392 Newbie
Currently Being Moderated
I have two classes A and B where B extends A

@Stateless
public class A{
@EJB
private PaperDaoImpl paperDao;
}

@Stateless
public class B extends A{

@EJB
private PencilDaoImp pencilDao;
}

When I remove the "@EJB private PaperDaoImpl paperDao" from class A, everything works fine; otherwise I get an error. But I need paperDao in class A. How can I fix this problem?

The error is basically:
javax.ejb.CreateException: Could not create stateless EJB

Actual error:


SEVERE: EJB5070: Exception creating stateless session bean : [ClassB]
WARNING: EJB5184:A system exception occurred during an invocation on EJB ClassB, method: public java.lang.String school.supplyrestapi.resource.ClassB.getIt(java.lang.String,java.lang.String)
WARNING: javax.ejb.EJBException: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
     at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:454)
     at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:2547)
     at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:1899)
     at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
     at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:89)
     at $Proxy269.getIt(Unknown Source)
     at school.supplyrestapi.resource.__EJB31_Generated__ClassB__Intf____Bean__.getIt(Unknown Source)
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
     at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
     at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
     at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
     at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
     at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
     at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
     at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
     at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)
     at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)
     at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
     at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
     at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
     at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
     at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
     at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
     at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
     at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
     at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
     at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
     at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
     at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
     at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
     at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
     at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
     at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
     at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
     at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
     at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
     at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
     at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
     at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
     at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
     at java.lang.Thread.run(Thread.java:680)
Caused by: javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB
     at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:726)
     at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:247)
     at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:449)
     ... 50 more
Caused by: javax.ejb.CreateException: Could not create stateless EJB
     at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:534)
     at com.sun.ejb.containers.StatelessSessionContainer.access$000(StatelessSessionContainer.java:95)
     at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:724)
     ... 52 more
Caused by: com.sun.enterprise.container.common.spi.util.InjectionException: Exception attempting to inject Remote ejb-ref name=school.supplyrestapi.ClassA/paperDao,Remote 3.x interface =school.supplydao.paperDao,ejb-link=null,lookup=,mappedName=,jndi-name=school.supplydao.paperDao,refType=Session into class school.supplyrestapi.ClassA: Lookup failed for 'java:comp/env/school.supplyrestapi.ClassA/paperDao' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming}
     at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl._inject(InjectionManagerImpl.java:703)
     at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.inject(InjectionManagerImpl.java:470)
     at com.sun.enterprise.container.common.impl.util.InjectionManagerImpl.injectInstance(InjectionManagerImpl.java:171)
     at com.sun.ejb.containers.BaseContainer.injectEjbInstance(BaseContainer.java:1694)
     at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:494)
     ... 54 more
Caused by: javax.naming.NamingException: Lookup failed for 'java:comp/env/school.supplyrestapi.ClassA/paperDao' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: Exception resolving Ejb for 'Remote ejb-ref name=school.supplyrestapi.ClassA/paperDao,Remote 3.x interface =school.supplydao.paperDao,ejb-link=null,lookup=,mappedName=,jndi-name=school.supplydao.paperDao,refType=Session' .  Actual (possibly internal) Remote JNDI name used for lookup is 'school.supplydao.paperDao#school.supplydao.paperDao' [Root exception is javax.naming.NamingException: Lookup failed for 'school.supplydao.paperDao#school.supplydao.paperDao' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: school.supplydao.paperDao#school.supplydao.paperDao not found]]]

Edited by: 961389 on Oct 12, 2012 12:00 PM
  • 1. Re: Using @EJB in both parent and child fails
    r035198x Pro
    Currently Being Moderated
    Is your PaperDaoImpl class correctly setup as an EJB?
    Also, avoid creating a DAO per entity as an EJB. Rather have one DAOFactory as the EJB. You can still have entity specific DAOs if you feel that you need them but only one EJB.
  • 2. Re: Using @EJB in both parent and child fails
    gimbal2 Guru
    Currently Being Moderated
    r035198x wrote:
    Is your PaperDaoImpl class correctly setup as an EJB?
    Probably not.
    Also, avoid creating a DAO per entity as an EJB. Rather have one DAOFactory as the EJB. You can still have entity specific DAOs if you feel that you need them but only one EJB.
    Sorry, but that doesn't make any sense at all and it is on top of that your opinion, not a fact. You make it sound like it is somehow a bad idea to create multiple EJBs. If that were true the tech would be useless.
  • 3. Re: Using @EJB in both parent and child fails
    r035198x Pro
    Currently Being Moderated
    gimbal2 wrote:
    You make it sound like it is somehow a bad idea to create multiple EJBs. If that were true the tech would be useless.
    Creating unnecessary EJBs is the bad idea and results in apps that don't scale and shows lack of understanding of the tech. There is simply no point in making each of those DAOs an EJB for the container to pool separately.
    EJB DAO per entity is a documented anti-pattern :http://www.amazon.co.uk/Core-J2EE-Patterns-Practices-Strategies/dp/0131422464/ref=sr_1_2?ie=UTF8&s=books&qid=1247669640&sr=1-2
    http://www.amazon.co.uk/Bitter-EJB-Bruce-Tate/dp/1930110952/ref=sr_1_1?ie=UTF8&s=books&qid=1247669429&sr=8-1

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points