简体   繁体   English

将MongoDB服务器属性放置在context.xml中

[英]Placing MongoDB server properties in context.xml

In an JAVA web project hosted in Tomcat with a backend ORACLE/MYSQL We could add a <Resource> like below (consider connection an Oracle Server) 在带有后端ORACLE/MYSQL Tomcat托管的JAVA Web项目中,我们可以添加如下所示的<Resource> (考虑连接Oracle Server)

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/myProject">

  <Resource auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" 
  factory="oracle.jdbc.pool.OracleDataSourceFactory" 
  maxActive="20" maxIdle="10" 
  maxWait="-1" name="jdbc/TestDB" password="dbPAss" type="oracle.jdbc.pool.OracleDataSource" 
  url="jdbc:oracle:thin:@DBHOST:PORT:SERVICENAME" 
  user="dbUser"/>

  <Loader delegate="true"/>
</Context>

in the context.xml of the project and only changing a few things if its connecting a MySQL and can used in a JAVA SERVLET by using context.xml的项目,只有改变一些东西,如果它连接MySQL ,并且可以在使用JAVA SERVLET使用

Context initContext = new InitialContext();
Context envContext  = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");

and then creating its Connection Object 然后创建其Connection对象

What should be the correct syntax for using it for MongoDB ? 将其用于MongoDB的正确语法应该是什么?

I am intending to store the HOST,PORT, USERNAME and PASSWORD for the MongoDB server. 我打算为MongoDB服务器存储主机,端口,用户名和密码。

Tomcat only supports JDBC DataSources when using <Resource> elements (well, it supports other things like SMTP sessions, etc. but for databases, they must be JDBC-based). Tomcat仅在使用<Resource>元素时支持JDBC数据源(嗯,它支持SMTP会话等其他功能,但是对于数据库,它们必须基于JDBC)。 There is currently no JDBC driver for MongoDB (because it's not a relational database, and the JDBC API makes no sense for it) (unless you want to try this thing: https://github.com/erh/mongo-jdbc ), so you'll have to manage your own resource pool for it. 当前没有用于MongoDB的JDBC驱动程序(因为它不是关系数据库,并且JDBC API对此没有任何意义)(除非您想尝试一下: https//github.com/erh/mongo-jdbc ),因此您必须为其管理自己的资源池。

actually there is a JDBC driver for MongoDB. 实际上,MongoDB有一个JDBC驱动程序。 one was just released by a company called UnityJDBC. 一个刚刚由一家名为UnityJDBC的公司发布。 you can download the program and driver for free at... 您可以从以下位置免费下载程序和驱动程序...

http://www.unityjdbc.com/mongojdbc/mongo_jdbc.php http://www.unityjdbc.com/mongojdbc/mongo_jdbc.php

Tomcat (or more specifically, JNDI) does in fact support arbitrary resources ( <Resource> elements in config.xml ), including connection information for MongoDB without requiring that MongoDB (or any other non-RDBMS) be wrangled to use JDBC. 实际上,Tomcat(或更确切地说是JNDI)确实支持任意资源( config.xml <Resource>元素),包括MongoDB的连接信息,而无需费心MongoDB(或任何其他非RDBMS)使用JDBC。 All that's required is an implementation of the javax.naming API to define the properties of your custom resource (see Adding Custom Resource Factories in the Tomcat JNDI HOW-TO reference). 所需javax.naming就是实现javax.naming API的定义您的自定义资源的属性(请参阅Tomcat JNDI HOW-TO参考中的“ 添加自定义资源工厂 ”)。

I recently found this GitHub project which aims to store MongoDB datasource connection information as a JNDI resource while using the official MongoDB Java client. 我最近发现了这个GitHub项目该项目旨在在使用正式的MongoDB Java客户端时将MongoDB数据源连接信息存储为JNDI资源。

If you're using Spring, this other answer provides information for MongoDB datasource configuration via a JNDI resource when using Spring (and that code could also be used as a guide to creating your own custom JNDI resource loader for MongoDB or any other configuration properties you with to store in the Tomcat context.xml ). 如果您使用的是Spring,则此其他答案将在使用Spring时通过JNDI资源提供有关MongoDB数据源配置的信息(并且该代码也可以用作为MongoDB或您创建的任何其他配置属性创建自己的自定义JNDI资源加载器的指南。用于存储在Tomcat context.xml )。

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

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