2017-03-13 13:44 GMT+03:00 Enrico Olivelli <eolive...@gmail.com>:
> 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

Which context.xml file?
It must be META-INF/context.xml in the webapp or
conf/<enginename>/<hostname>/<appname> .xml in Tomcat.

A common mistake is to edit the global conf/context.xml file.

> <?xml version="1.0" encoding="UTF-8"?>
> <Context path="/dobermon">

The "path" attribute is not allowed here. (It is allowed only when
defining <Context> directly in conf/server.xml).

>     <ResourceLink
>         name="jdbc/DobermonDatabase"
>         global="jdbc/DobermonDatabase"
>         type="javax.sql.DataSource"
>         factory="org.apache.naming.factory.DataSourceLinkFactory"
>     />

I usually omit the "factory" attribute in a ResourceLink.  Setting
"name", "global" and "type" is enough.

Otherwise it looks good.

>
> </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">

Servlet 2.4 spec, really?  You want to stay compatible with Tomcat 5.5?

Note that you can use "validate" function in an XML editor to validate
this file against its schema (web-app_2_4.xsd).

>     <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 ?

Any other errors at startup time? In catalina log, in localhost log?

You may test whether the global Resource is declared correctly:
start Tomcat without your webapp,
connect as JMX client (with jconsole)
and look whether your db connection pool is listed as a bean in jconsole.

(http://tomcat.apache.org/presentations.html
See "Monitoring Apache Tomcat with JMX" presentation from ApacheCon 2016)

You may also try running with a debugger,
https://wiki.apache.org/tomcat/FAQ/Developing#Debugging

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to