Skip to main content

Persistence in Web Container

14 replies [Last post]
shelleyb
Offline
Joined: 2007-07-30

Is there a reference regarding best practices for using Java Persistence in a web container (Servlets only, without EJBs)?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Sahoo

Can you please check the version in web.xml? It must be >=2.5 for
resource injection to take place?

Thanks,
Sahoo

glassfish@javadesktop.org wrote:
> Yes, the ServletContextListener ("MyFacade") is defined in the web deployment descriptor and the init() method is being called as expected.
> [Message sent by forum member 'shelleyb' (shelleyb)]
>
> http://forums.java.net/jive/thread.jspa?messageID=239396
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

Shelley

The web deployment descriptor is version 2.5.

Sahoo wrote:
>
> Can you please check the version in web.xml? It must be >=2.5 for
> resource injection to take place?
>
> Thanks,
> Sahoo
>
--
View this message in context: http://www.nabble.com/Persistence-in-Web-Container-tf4571827.html#a13157448
Sent from the java.net - glassfish users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

Sahoo

What version of GlassFish are you using?

Sahoo

Shelley wrote:
> The web deployment descriptor is version 2.5.
>
>
> Sahoo wrote:
>
>> Can you please check the version in web.xml? It must be >=2.5 for
>> resource injection to take place?
>>
>> Thanks,
>> Sahoo
>>
>>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

mvatkina
Offline
Joined: 2005-04-04

Yes. Read this from the BluePrints team: https://blueprints.dev.java.net/bpcatalog/ee5/persistence/index.html

-marina

Message was edited by: mvatkina

shelleyb
Offline
Joined: 2007-07-30

The provided link references an article, "NYU Builds Digital Repository on Racks of Sun Fire X4500 Servers." Is there a more specific link where there might be information pertaining to the use of JPA within web containers?

mvatkina
Offline
Joined: 2005-04-04

Sorry. I don't know how this could happen. I fixed the reference.

Regards,
-marina

shelleyb
Offline
Joined: 2007-07-30

Thank you - the blueprints are very helpful.

I am still encountering a problem, however. The entity manager factory is not being injected via the @PersistenceUnit annotation.  Without injection, the code works fine (ie: calling Persistence.createEntityManagerFactory() directly). Am I missing some configuration or dependency?

public class MyFacade implements ServletContextListener {

    @PersistenceUnit(unitName = "myPu")
    private EntityManagerFactory emf;

    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        context.setAttribute("MyFacade", this);
    }

    public void contextDestroyed(ServletContextEvent sce) {
        if (emf.isOpen()) {
            emf.close();
        }
    }

    public T find(Class entityClass, Object id) {
        EntityManager em = emf.createEntityManager(); /* NullPointerException thrown here */
        T entity = em.find(entityClass, id);
        em.close();
        return entity;
    }

}

public class MyServlet extends HttpServlet {

    protected MyFacade mf;

    public void init(ServletConfig config) throws ServletException {
        ServletContext context = config.getServletContext();
        mf = (MyFacade) context.getAttribute("MyFacade");
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            . . .
            MyEntity myEntity = mf.find(MyEntity.class, id);
            . . .
    }

}

mvatkina
Offline
Joined: 2005-04-04

Are there any exceptions in the server.log?

thanks,
-marina

shelleyb
Offline
Joined: 2007-07-30

No, only the NullPointerException at runtime when a method is called on the entity manager factory. It just seems that the emf is not being injected. Besides adding the @PersistenceUnit annotation, is there anything else that needs to be done that I might be missing to achieve this dependency injection?

mvatkina
Offline
Joined: 2005-04-04

I found the following article that seems to be similar to your app: http://java.sun.com/mailers/techtips/enterprise/2006/TechTips_Nov06.html and it specifies CatalogFacade in the WEB-INF/web.xml:
com.sun.javaee.blueprints.autoid.CatalogFacade

Do you do the same?

thanks,
-marina

shelleyb
Offline
Joined: 2007-07-30

Yes, the ServletContextListener ("MyFacade") is defined in the web deployment descriptor and the init() method is being called as expected.

shelleyb
Offline
Joined: 2007-07-30

[i]Cross-Posting from Nabble [1][/i]

The web deployment descriptor is version 2.5.

[b]Sahoo wrote:[/b]
> Can you please check the version in web.xml? It must be >=2.5 for
> resource injection to take place?
>
> Thanks,
> Sahoo

[1] http://www.nabble.com/Persistence-in-Web-Container-tf4571827.html#a13157448

shelleyb
Offline
Joined: 2007-07-30

I am using persistence-api v1.0 with the servlet-api v2.5, and I have tried using TopLink (2.0.1-04) and Hibernate (3.2.5), and have run the app on Jetty (maven-jetty-plugin), JBoss (4.2), and Tomcat (6.0.14) servers.

[b]Sahoo wrote:[/b]
>What version of GlassFish are you using?
>
>Sahoo

http://www.nabble.com/Persistence-in-Web-Container-tf4571827.html#a13160111

Sahoo

Injection service is provided by the container. You are not using
GlassFish container, so it's time you asked in appropriate container
forums as to why injection was not working. It may just be that those
containers, unlike GlassFish, do *not* yet completely implement Servlet
2.5 specification.

Thanks,
Sahoo

glassfish@javadesktop.org wrote:
> I am using persistence-api v1.0 with the servlet-api v2.5, and I have tried using TopLink (2.0.1-04) and Hibernate (3.2.5), and have run the app on Jetty (maven-jetty-plugin), JBoss (4.2), and Tomcat (6.0.14) servers.
>
> [b]Sahoo wrote:[/b]
>
>> What version of GlassFish are you using?
>>
>> Sahoo
>>
>
> http://www.nabble.com/Persistence-in-Web-Container-tf4571827.html#a13160111
> [Message sent by forum member 'shelleyb' (shelleyb)]
>
> http://forums.java.net/jive/thread.jspa?messageID=239649
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net