简体   繁体   English

数据核(JDO)的Tomcat JNDI资源

[英]Tomcat JNDI resource for Datanucleus (JDO)

I have developed an application on Tomcat 7.0 that uses Datanucleus / JDO to access to a database. 我已经在Tomcat 7.0上开发了一个使用Datanucleus / JDO访问数据库的应用程序。 I currently have the JDO connection properties stored in the "datanucleus.properties" located in the application itself. 我目前在应用程序本身的“ datanucleus.properties”中存储了JDO连接属性。 The connection is working fine, but I would like to store the connection information as JNDI, to have it on the server and no longer in the war itself (I always have to replace the file in the war when deploying it remotely). 连接工作正常,但是我想将连接信息存储为JNDI,以便将其存储在服务器上,而不再用于战争本身(远程部署它时,我总是必须在战争中替换该文件)。

I tried the following: 我尝试了以下方法:

  1. Create a in the web.xml of the application (jdbc/ConnectionDB) 在应用程序的web.xml(jdbc / ConnectionDB)中创建一个

  2. In "Server.xml", I tried to add the following the context of my application 在“ Server.xml”中,我尝试添加以下应用程序上下文

     <Resource name="jdbc/ConnectionDB" auth="Container" type="javax.jdo.PersistenceManagerFactory" /> <ResourceParams name="jdbc/ConnectionDB <parameter> <name>javax.jdo.PersistenceManagerFactoryClass</name> <value>org.datanucleus.api.jdo.JDOPersistenceManagerFactory</value> </parameter> <parameter> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> </parameter> <parameter> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://localhost/TomcatTest</value> </parameter> ... 
  3. I then try to create a new PMF with the following syntax: 然后,我尝试使用以下语法创建新的PMF:

    Context context = null; 上下文上下文= null; PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory("java:comp/env/jdbc/ConnectionDB",context); PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(“ java:comp / env / jdbc / ConnectionDB”,context);

When I run my application, I get a javax.jdo.JDOUserException: You have either specified for this PMF to use a "persistence-unit" of "datanucleus.properties" (yet this doesnt exist!) 运行我的应用程序时,我得到一个javax.jdo.JDOUserException:您已为此PMF指定使用“ datanucleus.properties”的“持久性单元”(但不存在!)

I don't really understand what is wrong in my setup. 我不太了解我的设置有什么问题。

Regards, 问候,

Marcel 马塞尔

I finally found the solution I was looking for, I post it here, it might help somebody else: 我终于找到了我想要的解决方案,我将其发布在这里,它可能会对其他人有所帮助:

  1. Create a resource in "Context.xml" file of the server 在服务器的“ Context.xml”文件中创建资源

      <Resource name="jdbc/SyncTestDB" auth="Container" type="javax.sql.DataSource" maxActive="100" maxIdle="30" maxWait="10000" username="root" password="mysql" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost/SyncTestDB"/> 
  2. Create a reference to that resource in the "web.xml" file of your application 在应用程序的“ web.xml”文件中创建对该资源的引用

      <resource-ref> <description>MySQL Database Connection</description> <res-ref-name>jdbc/SyncTestDB</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 
  3. And finally get a Persistence Manager Factory using the JNDI connection: 最后使用JNDI连接获得一个Persistence Manager Factory:

     PersistenceManagerFactory pmf; Properties properties = new Properties(); properties.setProperty("datanucleus.ConnectionFactoryName","java:comp/env/jdbc/SyncTestDB"); 

Read the javadoc for JDOHelper.getPersistenceManagerFactory(String) and it is obviously not for passing in some JNDI data source string. 阅读JDOHelper.getPersistenceManagerFactory(String)的javadoc,显然它不是用于传递某些JNDI数据源字符串。

Read the docs for Tomcat and you will also see that specifying a datasource you do not provide JDO connection details. 阅读Tomcat的文档,您还将看到指定数据源不提供JDO连接详细信息。

You can equally specify a persistence.xml with that JNDI string for the "javax.jdo.option.ConnectionFactoryName" property. 您可以同样为“ javax.jdo.option.ConnectionFactoryName”属性使用该JNDI字符串指定persistence.xml。 As per the JDO spec and DataNucleus/Tomcat docs then 根据JDO规范和DataNucleus / Tomcat文档,然后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM