jndi database connection with jpa and eclipselink

拈花ヽ惹草 提交于 2019-12-03 13:46:45
skanne

Just add the class JPAEclipseLinkSessionCustomizer to your project and configure the persistence.xml as shown below:

<persistence-unit name="yourUnit" transaction-type="RESOURCE_LOCAL">

    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

    <non-jta-data-source>java:comp/env/jdbc/yourDs</non-jta-data-source>        

    <properties>
        <property name="eclipselink.session.customizer" value="com.company.yourproject.yourpackage.JPAEclipseLinkSessionCustomizer"/>        
    </properties>

</persistence-unit>

You need this workaround only for Apache Tomcat.

Set the datasource URL to "jdbc/myDs"
<non-jta-data-source>jdbc/myDs</non-jta-data-source>

I hope you could solve the problem with the comments given ...

I fix it by adding a property "javax.persistence.nonJtaDataSource" with the same value as the element non-jta-data-source

I think you need to code a workaround for the javax.naming.NamingException: This context must be accessed throught a java: URL exception as detailed at http://wiki.eclipse.org/EclipseLink/Examples/JPA/Tomcat_Web_Tutorial#Session_Customizer

Also see this link which debugs through the problem

http://forums.java.net/jive/message.jspa?messageID=158622

Year 2015 actualisation: I not sure Eclipselink SessionCustomizer is required. In my test all work without (Tomcat 7.x, Java 7.x, Eclipselink 2.4.x).

Some older materials suggest specific problem with Tomcat 5/6 and special way to resolve it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!