Re: Trivial DataSource ResourceLink error

2017-03-13 Thread Enrico Olivelli
2017-03-13 12:44 GMT+01:00 Konstantin Kolinko :

> 2017-03-13 13:44 GMT+03:00 Enrico Olivelli :
> > 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:
> >
> >  
> > 
> >  >   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" />
> >
> >>   auth="Container"
> >   type="javax.sql.DataSource"
> >   driverClassName="net.sourceforge.jtds.jdbc.Driver"
> >   url="jdbc:jtds:sqlserver://x"
> >   username="x"
> >   password=""
> >   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/// .xml in Tomcat.
>
> A common mistake is to edit the global conf/context.xml file.
>
> > 
> > 
>
> The "path" attribute is not allowed here. (It is allowed only when
> defining  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)



>
> >  > 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.
>
> >
> > 
> >
> > In web.xml I have
> > 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).
>
> > Dobermon
> >
> > 
> > DB Connection
> > jdbc/DobermonDatabase
> > javax.sql.DataSource
> > Container
> > 
> > 
> >
> > 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 

Re: Trivial DataSource ResourceLink error

2017-03-13 Thread Konstantin Kolinko
2017-03-13 13:44 GMT+03:00 Enrico Olivelli :
> 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:
>
>  
> 
>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" />
>
>  auth="Container"
>   type="javax.sql.DataSource"
>   driverClassName="net.sourceforge.jtds.jdbc.Driver"
>   url="jdbc:jtds:sqlserver://x"
>   username="x"
>   password=""
>   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/// .xml in Tomcat.

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

> 
> 

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

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

>
> 
>
> In web.xml I have
> 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).

> Dobermon
>
> 
> DB Connection
> jdbc/DobermonDatabase
> javax.sql.DataSource
> Container
> 
> 
>
> 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



Trivial DataSource ResourceLink error

2017-03-13 Thread Enrico Olivelli
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:

 



  
 


In context.xml I have








In web.xml I have
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">
Dobermon


DB Connection
jdbc/DobermonDatabase
javax.sql.DataSource
Container



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