Hello
I'm having difficulty getting a JDBC DataSource using Tomcat 8.
I want to define the JDBC details in server.xml so the database identified
depends on the server and not the application. It will be beneficial for me if
the applications only need to know the JDBC name and not password details.
Thus I'm using GlobalNamingResources in server.xml
and ResourceLink in context.xml
I'm obviously not getting it right because I get this exception:
javax.naming.NameNotFoundException: Name [jdbc/weblogin01b] is not bound in
this Context. Unable to find [jdbc].
at org.apache.naming.NamingContext.lookup(NamingContext.java:818)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
at
org.apache.naming.factory.ResourceLinkFactory.getObjectInstance(ResourceLinkFactory.java:92)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
at org.apache.naming.NamingContext.lookup(NamingContext.java:841)
at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
In CATALINA_BASE/conf/server.xml
I have included a variety of slightly different resources all aiming to get to
the same database schema hopefully one of them will be right:
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<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" />
<Resource name="jdbc/weblogin01"
username="weblogin01"
password="xxxxx"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.pool.OracleDataSource"
description="Global Address Database"
url="jdbc:oracle:thin:@10.15.120.29:1522:DGSPC"
maxActive="15"
maxIdle="3" />
<Resource name="jdbc/weblogin01b"
user="weblogin01"
password="xxxxx"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
url="jdbc:oracle:thin:@10.15.120.29:1522:DGSPC"
maxActive="20"
maxIdle="3"
maxWait="-1" />
<Resource name="jdbc/weblogin01c"
user="weblogin01"
password="xxxxx"
auth="Container"
type="javax.sql.DataSource"
driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@10.15.120.29:1522:DGSPC"
maxActive="20"
maxIdle="3"
maxWait="-1" />
</GlobalNamingResources>
In META-INF/context.xml
I have a ResourceLink to each resource
<Context path="/testDbAccess">
<ResourceLink name="jdbc/weblogin01"
global="jdbc/weblogin01"
type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/weblogin01b"
global="jdbc/weblogin01b"
type="javax.sql.DataSource"/>
<ResourceLink name="jdbc/weblogin01c"
global="jdbc/weblogin01c"
type="javax.sql.DataSource"/>
</Context>
In web.xml I made no changes related to JDBC on the understanding that the
ResourceLink elements will be sufficient.
In Java code I try to get a DataSource as follows:
String dbUser = "weblogin01b";
try {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Get data source
ds = (DataSource) envCtx.lookup("jdbc/" + dbUser);
if (ds == null) {
logger.log(Level.WARNING,"Null datasource for " + dbUser);
}
}
I get similar exceptions for each of the names:
jdbc/weblogin01
jdbc/weblogin01b
jdbc/weblogin01c
After years using GlassFish, I'm struggling with Tomcat, any help would be much
appreciated.
Thank you
________________________________
This e-mail is for the sole use of the intended recipient and contains
information that may be privileged and/or confidential. If you are not an
intended recipient, please notify the sender by return e-mail and delete this
e-mail and any attachments. Certain required legal entity disclosures can be
accessed on our website.<http://thomsonreuters.com/prof_disclosures/>