title: jndi database connection with jpa and eclipselink
body: <p>I try to setup a database connection in java with JNDI in combination with eclipseLink/JPA on Tomcat 5.5. I already configured the JNDI resource in web.xml and context.xml.</p>
  
  <p>The db connection works with JNDI without using JPA and eclipseLink.
  After configuring the persistence.xml for eclipseLink, I got the following exception.</p>
  
  <p>I don't know how to configure the persistence.xml correctly to use JNDI datasource for the db connection.</p>
  
  <p>The exception</p>
  
  <pre><code>WicketMessage: Method onFormSubmitted of interface      
  org.apache.wicket.markup.html.form.IFormSubmitListener targeted at component     
  [MarkupContainer [Component id = loginForm]] threw an exception
  
  
  Root cause:
  
  javax.naming.NamingException: This context must be accessed throught a java: URL
  at org.apache.naming.SelectorContext.parseName(SelectorContext.java:686)
  at org.apache.naming.SelectorContext.lookup(SelectorContext.java:121)
  at javax.naming.InitialContext.lookup(InitialContext.java:396)
  at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:110)
  at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:94)
  at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:16    2)
  at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(Datab    aseSessionImpl.java:579)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:228)
  at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:380)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:157)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:214)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:202)
  at de.company.myDs.controller.AbstractCrudController.getEntityManager(Unknown Source)
  at de.company.myDs.controller.UserController.loadUserByName(Unknown Source)
  at de.company.myDs.controller.LoginController.validateUserLogin(Unknown Source)
  at de.company.myDs.pages.LoginPage$1.onSubmit(Unknown Source)
  at org.apache.wicket.markup.html.form.Form.delegateSubmit(Form.java:1534)
  at org.apache.wicket.markup.html.form.Form.process(Form.java:934)
  at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:896)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:182)
  at org.apache.wicket.request.target.component.listener.ListenerInterfaceRequestTarget.processEvents(ListenerInterfaceRequestTarget.java:73)
  at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
  at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)
  at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
  at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1428)
  at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
  at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:479)
  at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:312)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:172)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:873)
  at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
  at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
  at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
  at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
  at java.lang.Thread.run(Thread.java:637)
  
  Complete stack:
  
  org.apache.wicket.WicketRuntimeException: Method onFormSubmitted of interface org.apache.wicket.markup.html.form.IFormSubmitListener targeted at component [MarkupContainer [Component id = loginForm]] threw an exception
  at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:193)
  at org.apache.wicket.request.target.component.listener.ListenerInterfaceRequestTarget.processEvents(ListenerInterfaceRequestTarget.java:73)
  at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
  at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)
  at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
  at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1428)
  at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
  at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:479)
  
  java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:182)
  at org.apache.wicket.request.target.component.listener.ListenerInterfaceRequestTarget.processEvents(ListenerInterfaceRequestTarget.java:73)
  at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
  at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)
  at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
  at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1428)
  at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
  at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:479)
  
  javax.persistence.PersistenceException: Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.1.0.v20100614-r7608): org.eclipse.persistence.exceptions.ValidationException
  Exception Description: Cannot acquire data source [java:comp/env/jdbc/myDs].
  Internal Exception: javax.naming.NamingException: This context must be accessed throught a java: URL
  at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:408)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:157)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:214)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:202)
  at de.company.myDs.controller.AbstractCrudController.getEntityManager(Unknown Source)
  at de.company.myDs.controller.UserController.loadUserByName(Unknown Source)
  at de.company.myDs.controller.LoginController.validateUserLogin(Unknown Source)
  at de.company.myDs.pages.LoginPage$1.onSubmit(Unknown Source)
  at org.apache.wicket.markup.html.form.Form.delegateSubmit(Form.java:1534)
  at org.apache.wicket.markup.html.form.Form.process(Form.java:934)
  at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:896)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:182)
  at org.apache.wicket.request.target.component.listener.ListenerInterfaceRequestTarget.processEvents(ListenerInterfaceRequestTarget.java:73)
  at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
  at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)
  at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
  at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1428)
  at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
  at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:479)
  
  Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.1.0.v20100614-r7608): org.eclipse.persistence.exceptions.ValidationException
  Exception Description: Cannot acquire data source [java:comp/env/jdbc/myDs].
  Internal Exception: javax.naming.NamingException: This context must be accessed throught a java: URL
  at org.eclipse.persistence.exceptions.ValidationException.cannotAcquireDataSource(ValidationException.java:464)
  at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:116)
  at org.eclipse.persistence.sessions.JNDIConnector.connect(JNDIConnector.java:94)
  at org.eclipse.persistence.sessions.DatasourceLogin.connectToDatasource(DatasourceLogin.java:162)
  at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:579)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:228)
  at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:380)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.getServerSession(EntityManagerFactoryImpl.java:157)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManagerImpl(EntityManagerFactoryImpl.java:214)
  at org.eclipse.persistence.internal.jpa.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:202)
  at de.company.myDs.controller.AbstractCrudController.getEntityManager(Unknown Source)
  at de.company.myDs.controller.UserController.loadUserByName(Unknown Source)
  at de.company.myDs.controller.LoginController.validateUserLogin(Unknown Source)
  at de.company.myDs.pages.LoginPage$1.onSubmit(Unknown Source)
  at org.apache.wicket.markup.html.form.Form.delegateSubmit(Form.java:1534)
  at org.apache.wicket.markup.html.form.Form.process(Form.java:934)
  at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:896)
  at java.lang.reflect.Method.invoke(Method.java:597)
  at org.apache.wicket.RequestListenerInterface.invoke(RequestListenerInterface.java:182)
  at org.apache.wicket.request.target.component.listener.ListenerInterfaceRequestTarget.processEvents(ListenerInterfaceRequestTarget.java:73)
  at org.apache.wicket.request.AbstractRequestCycleProcessor.processEvents(AbstractRequestCycleProcessor.java:92)
  at org.apache.wicket.RequestCycle.processEventsAndRespond(RequestCycle.java:1250)
  at org.apache.wicket.RequestCycle.step(RequestCycle.java:1329)
  at org.apache.wicket.RequestCycle.steps(RequestCycle.java:1428)
  at org.apache.wicket.RequestCycle.request(RequestCycle.java:545)
  at org.apache.wicket.protocol.http.WicketFilter.doGet(WicketFilter.java:479)
  </code></pre>
  
  <p>web.xml </p>
  
  <pre><code>...
  &lt;resource-ref&gt;
    &lt;description&gt;DB Connection&lt;/description&gt;
    &lt;res-ref-name&gt;jdbc/myDs&lt;/res-ref-name&gt;
    &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
    &lt;res-auth&gt;Container&lt;/res-auth&gt;
  &lt;/resource-ref&gt;
  ...    
  </code></pre>
  
  <p>context.xml </p>
  
  <pre><code>...
  &lt;Resource
      name="jdbc/myDs"
      auth="Container"
      type="javax.sql.DataSource"
      maxActive="100"
      maxIdle="30"
      validationQuery="SELECT 1"
      testOnBorrow="true"
      testWhileIdle="true"
      timeBetweenEvictionRunsMillis="60000"
      minEvictableIdleTimeMillis="600000"
      username="root"
      password="passwd"
      maxWait="10000"
      driverClassName="org.postgresql.Driver"
      url="jdbc:postgresql://localhost:5432/myDs" /&gt;
  ...
  </code></pre>
  
  <p>persistence.xml</p>
  
  <pre><code>...    
  &lt;persistence-unit name="myDs" transaction-type="RESOURCE_LOCAL"&gt;
  
      &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
  
      &lt;non-jta-data-source&gt;java:comp/env/jdbc/myDs&lt;/non-jta-data-source&gt;
  
      &lt;exclude-unlisted-classes&gt;false&lt;/exclude-unlisted-classes&gt;
  
      &lt;class&gt;de.company.myDS.model.User&lt;/class&gt;
  
  &lt;/persistence-unit&gt;
  ...
  </code></pre>