Hi,
I'm trying to setup a DataSource configured in GlobalNamingResources in
server.xml.
https://tomcat.apache.org/tomcat-8.5-doc/config/context.html#Resource_Links
in server.xml I have:
<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/DobermonDatabase"
auth="Container"
type="javax.sql.DataSource"
driverClassName="net.sourceforge.jtds.jdbc.Driver"
url="jdbc:jtds:sqlserver://xxxxx"
username="xxxxx"
password="xxxx"
maxTotal="20"
validationQuery="select 1"
maxIdle="10"
maxWaitMillis="-1" />
....
In context.xml I have
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/dobermon">
<ResourceLink
name="jdbc/DobermonDatabase"
global="jdbc/DobermonDatabase"
type="javax.sql.DataSource"
factory="org.apache.naming.factory.DataSourceLinkFactory"
/>
</Context>
In web.xml I have
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>Dobermon</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/DobermonDatabase</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
My code is:
protected Connection getConnection() throws SQLException, NamingException {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource)
ic.lookup("java:comp/env/jdbc/DobermonDatabase");
return ds.getConnection();
}
I am using Tomcat 8.5.9, I get this error:
java.lang.NullPointerException
at
org.apache.naming.factory.DataSourceLinkFactory.getObjectInstance(DataSourceLinkFactory.java:64)
at
javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
at org.apache.naming.NamingContext.lookup(NamingContext.java:839)
at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
at org.apache.naming.NamingContext.lookup(NamingContext.java:827)
at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
at org.apache.naming.SelectorContext.lookup(SelectorContext.java:163)
at javax.naming.InitialContext.lookup(InitialContext.java:417)
at dobermon.QueryResource.getConnection(QueryResource.java:36)
If I declare the datasource inside context.xml no error is thrown and I get
access the database
I want to use GlobalNamingResources without hardcoding username/password in
context.xml, that is committed to my shared source repository.
Any ideas ?
Is there any trivial error ?
Thank you
-- Enrico