0

Having seen numerous examples of how to use a JNDI DataSource from a Java Servlet, I've yet to find a sample of how to use JNDI in conjunction with JPA in a Servlet context.

The basic outline of my scenario is:

  1. a Java Servlet
  2. an external project containing JPA entities and a persistence.xml
  3. I'd like to use JPA in a ServLet

Lots of googeling have resulted in the following scheme.

In the web.xml I've registered a ServletContextListener together with a database definition:

<web-app>
    ...blah blah blah...
    <listener>
        <listener-class>my.company.MyContextListener</listener-class>
    </listener>
    ... blah blah blah
    <resource-ref>
        <res-ref-name>jdbc/MyDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    ...blah blah blah...
</web-app>

Where the details for the resource-ref jdbc/MyDB is properly defined in a context.xml.

The MyContextListener just instantiates a globally accessible EntityBroker:

public class MyContextListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent event) {
      Map<String, String> props = new HashMap<String, String>();  
      props.put(PersistenceUnitProperties.JTA_DATASOURCE, "java:/comp/env/jdbc/MyDB");  
      EntityManagerFactory factory = Persistence.createEntityManagerFactory("my-jpa", props);                
      event.getServletContext().setAttribute("RatingController", new EntityBroker(factory));
    }
}

Where an EntityBroker is an invention of mine. Basically it's just a wrapper for an EntityManagerFactory allowing clients to get an EntityManager:

public class EntityBroker {

    private final EntityManagerFactory factory;

    public EntityBroker(EntityManagerFactory factory) {
        this.factory = factory;
    }

    public EntityManager getManager() {
        return factory.createEntityManager();
    }
}

In this way I think I'm able to accommodate all future needs for client database access using JPA, but being a newbie to Servlet's I'd like to be more certain.

Is this scheme of mine totally bizarre or have I overcomplicated matters in any way?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Turin
  • 129
  • 2
  • 10
  • You can refer to JNDI datasources directly in your `persistence.xml` - [here](http://stackoverflow.com/questions/2911439/refering-tomcat-jndi-datasource-in-persistence-xml). As far as your `EntityBroker`, this is a little bizarre; the usual pattern for this is `session-per-request` or `open-session-in-view`. I.e. you bind an `EnitytManager` to a request and then access it in your servlets. – Boris the Spider Dec 31 '13 at 10:55
  • Why post a valid answer as a comment? – Gimby Dec 31 '13 at 11:28
  • Could you please elaborate on the concepts session-per-request and/or open-session-in-view? They are entirely new to me. I know how to refer to JNDI datasources in a persistence.xml file. – Turin Dec 31 '13 at 14:21

0 Answers0