Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure JPA to use JNDI?

I have a working persistence definition that works on java level tests. Now I want to incorporate that into a web application which defines the database connection as JNDI in the context.xml. What do I need to change to make it work with the JNDI instead of the persistence.xml or at least get the infor from there?

like image 888
javydreamercsw Avatar asked Dec 03 '09 23:12

javydreamercsw


1 Answers

Your persistence.xml beginning should be something like this (using EclipseLink as implementation), for a jdbc/MYNAME JNDI name:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
  <persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">    
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <non-jta-data-source>java:comp/env/jdbc/MYNAME</non-jta-data-source>
    <class>org.test.entity.MyEntity</class>
...
  </persistence-unit>
<persistence>

Of course you should set the configuration suitable for your environment. In the example I use a non-JTA DataSource: according to one of your comments, it seems your DataSource is not JTA-compliant. For Hibernate, the persistence provider should be different.

like image 114
bdulac Avatar answered Oct 20 '22 18:10

bdulac