将 MongoDB 服务器属性放在 context.xml 中 [英] Placing MongoDB server properties in context.xml
问题描述
在 Tomcat
中托管的 JAVA
web 项目中,后端 ORACLE/MYSQL
我们可以添加一个
如下所示(考虑连接一个 Oracle
服务器)
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)
<?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>
在项目的 context.xml
中,如果它连接了 MySQL
,则只更改一些内容和可以在 JAVA SERVLET
中使用
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 initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/TestDB");
然后创建它的Connection
对象
将它用于 MongoDB
的正确语法应该是什么?
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.
推荐答案
Tomcat 在使用
元素时仅支持 JDBC DataSources(嗯,它支持其他东西,如 SMTP 会话等,但对于数据库,它们必须是基于 JDBC 的).目前没有MongoDB的JDBC驱动(因为它不是关系型数据库,JDBC API对它没有意义)(除非你想试试这个东西:https://github.com/erh/mongo-jdbc),因此您必须为其管理自己的资源池.
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). 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 服务器属性放在 context.xml 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!