referring Tomcat JNDI datasource in persistence.xml

霸气de小男生 提交于 2019-11-30 08:44:59

The <non-jta-data-source> in persistence.xml should be

java:comp/env/jdbc/myds

as per the response in http://forums.oracle.com/forums/thread.jspa?messageID=1899677

And also is your db driver in $CATALINA_HOME/lib

(Im using Apache OpenJPA library in Tomcat7 so may not match to Hibernate stuff)

I have never used a global jdbc for my OpenJPA webapps but gave it a try. It worked and this is my configuration. See folder where persistence.xml is saved, probably is openjpa problem but without it nothing works.

myapp/WEB-INF/classes/META-INF/persistence.xml
this is using openjpa provider so class list may not be needed in hibernate.

<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<persistence-unit name="main" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <non-jta-data-source>java:comp/env/jdbc/mydb</non-jta-data-source>

    <class>com.myapp.db.User</class>
    <class>com.myapp.db.Server</class>

    <properties>
        <property name="openjpa.DynamicEnhancementAgent" value="false" />
        <property name="openjpa.RuntimeUnenhancedClasses" value="unsupported" /> 
        <property name="openjpa.Log" value="commons" />
        <property name="openjpa.ConnectionFactoryProperties" value="PrintParameters=true" />
    </properties>
</persistence-unit>

</persistence>

tomcat/conf/server.xml
Add global jdbc resource.

  ..cut...
  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" readonly="true"  />

    <Resource name="jdbc/mydb" auth="Container" type="javax.sql.DataSource"
        maxActive="100" maxIdle="20" maxWait="10000"
        username="myuser" password="mypwd"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/mydb?useUnicode=true&amp;characterEncoding=utf8"
        validationQuery="SELECT 1" removeAbandoned="true" removeAbandonedTimeout="300"
    />
  </GlobalNamingResources>
  ..cut...

tomcat/conf/Catalina/localhost/myapp.xml
this my development box so I use docBase to link directly to projects folder. You should be find inserting this to war package (META-INF/context.xml) when deployed to production box.

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="/projects/myapp/web" reloadable="true" crossContext="true" >

  <Realm className="org.apache.catalina.realm.DataSourceRealm"
    dataSourceName="jdbc/mydb" localDataSource="false" digest="SHA"
    userTable="user"            userNameCol="username" userCredCol="password"
    userRoleTable="user_role_v" roleNameCol="role" 
  />

  <ResourceLink name="jdbc/mydb" global="jdbc/mydb" type="javax.sql.DataSource" />
</Context>

myapp/WEB-INF/web.xml
Add resource-ref to the end of file.

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0" >

  <description>Webapp</description>
  <display-name>Webapp</display-name>
  ..cut...

  <resource-ref>
    <description>mydb</description>
    <res-ref-name>jdbc/mydb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

</web-app>

Because Im using OpenJPA+Tomcat7 (not fullsized j2ee container) this may look overengineering but thats how it works. Results are good and developing db-aware webapps is very easy. No need to manual sql query hardcoding and oldskool DAO classes.

Did you make that resource available for the application by declaring it in your web.xml?

<resource-ref>
  <description>DB Connection</description>
  <res-ref-name>jdbc/myds</res-ref-name>
  <res-type>javax.sql.DataSource</res-type>
  <res-auth>Container</res-auth>
</resource-ref>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!