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>
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");
and then creating its Connection Object
What should be the correct syntax for using it for MongoDB?
I am intending to store the HOST,PORT, USERNAME and PASSWORD for the MongoDB server.
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.