You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Enrico Olivelli <eo...@gmail.com> on 2017/03/13 10:44:57 UTC

Trivial DataSource ResourceLink error

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

Re: Trivial DataSource ResourceLink error

Posted by Enrico Olivelli <eo...@gmail.com>.
2017-03-13 12:44 GMT+01:00 Konstantin Kolinko <kn...@gmail.com>:

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


it is META-INF/context.xml in the webapp, I cannot remove the 'path'
attribute because without it NetBeans will not deploy the App (this is
surely an issue for NB)



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


by omitting the factory="org.apache.naming.factory.DataSourceLinkFactory"
all works as espected




I wonder if there is a more "portable" way of declaring the resource link
in web.xml without the context.xml file in the web app

But for me at this time the context.xml solution is OK


Thank you very much !


>
> 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-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: Trivial DataSource ResourceLink error

Posted by Konstantin Kolinko <kn...@gmail.com>.
2017-03-13 13:44 GMT+03:00 Enrico Olivelli <eo...@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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org