sdoca
sdoca

Reputation: 8042

JPA/Glassfish 3 - NamingException injecting EJB into servlet

I am deploying to Glassfish 3.1.1.

I have a JPA project which creates a jar. It has standard data model classes annotated with @Entity and service classes annotated with @Stateless. The service classes all inherit from a GenericServiceBean class:

@Stateless
public class GenericServiceBean implements GenericService
{
    @PersistenceContext(unitName = "myUnitName")
    EntityManager em;

    public GenericServiceBean()
    {
        // empty
    }

    public <T> T create(T t)
    {
        em.persist(t);   
        return t;
    }

    public <T> T update(T t)
    {
        return em.merge(t);
    }

    public List<?> findWithNamedQuery(String namedQueryName,
            Map<String, Object> parameters)
    {
        return findWithNamedQuery(namedQueryName, parameters, 0);
    }

    public List<?> findWithNamedQuery(String namedQueryName,
            Map<String, Object> parameters, int resultLimit)
    {
        Query query = this.em.createNamedQuery(namedQueryName);

        if (resultLimit > 0)
        {
            query.setMaxResults(resultLimit);
        }

        for (Map.Entry<String, Object> entry : parameters.entrySet())
        {
            query.setParameter(entry.getKey(), entry.getValue());
        }

        return query.getResultList();
    }
}

public class BatchService extends GenericServiceBean
{
    public BatchService()
    {
        super();
    }

    public Batch create(Batch batch)
    {
        return this.create(batch);
    }

    public Batch find(Batch batch)
    {
        return this.update(batch);
    }

    public List<Batch> findByStatus(String status)
    {
        HashMap<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("status", status);

        return (List<Batch>) this.findWithNamedQuery("Batch.findByStatus",
                parameters);
    }
}

I have set up a JDBC connection pool in Glassfish and can successfully ping the database (MySQL). I set up a JDBC resource jdbc/myUnitName associated with the pool. Because I set the URL, user, password etc. properties on the pool, I did not add them to the resource nor did I include them in the persitence.xml. which looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
    <persistence-unit name="myUnitName">
        <jta-data-source>jdbc/myUnitName</jta-data-source>
        <properties>
            <property name="eclipselink.logging.level" value="FINE" />
        </properties>
    </persistence-unit>
</persistence>

I have a servlet into which I want the service bean injected:

public class ESwimConnectivityServlet extends HttpServlet
{
    @EJB
    private BatchService batchService; 

    @Override
    protected void service(HttpServletRequest request,
        HttpServletResponse response)
        throws ServletException, IOException
    {
        log.debug("Enter service()");
        this.processRequest(request, response);
        log.debug("Exit service()");

    String message = ("Connecting to database: ");

    try
    {
        List<Batch> batches = batchService.findByStatus(ProcessingStatusEnum.SUCCESS.toString());
        message += "SUCESS - batch query returned " + batches.size();
    }
    catch (Throwable t)
    {
        message += "FAIL - see log for details";
        log.error("Could not query database", t);
    }

    response.getWriter().write(message + "\n");

    }
}

When I try to deploy the servlet war, I get a this final Caused by in my stack trace:

Caused by: javax.naming.NamingException: 
Lookup failed for 'java:comp/env/com.mycompany.MyServlet/batchService' 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=com.mycompany.MyServlet/batchService,Remote 3.x interface =com.mycompany.service.BatchService,ejb-link=null,lookup=,mappedName=,jndi-name=com.mycompany.service.BatchService,refType=Session' .  Actual (possibly internal) Remote JNDI name used for lookup is 'com.mycompany.service.BatchService#com.mycompany.service.BatchService'
[Root exception is javax.naming.NamingException: Lookup failed for 'com.mycompany.service.BatchService#com.mycompany.service.BatchService' 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: com.mycompany.service.BatchService#com.mycompany.service.BatchService not found]]]

I don't know what I've done wrong in either my code or config in order to cause this issue. Any help debugging would be much appreciated!

Upvotes: 0

Views: 639

Answers (2)

m c
m c

Reputation: 1104

From another source:

Session bean cannot extends session bean

spec EJB Core 4.6.2

The session bean class may have superclasses and/or superinterfaces. A session bean class MUST NOT have a superclass that is itself a session bean class.

Upvotes: 0

Matt Handy
Matt Handy

Reputation: 30025

I think you have to annotate your BatchService class with the @Stateless annotation to make it injectable. I am not sure, but I think annotations cannot be inherited.

Upvotes: 1

Related Questions