On Wed, Oct 29, 2014 at 12:45 PM, <vince.w...@thomsonreuters.com> wrote:

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

Sometimes when you get a NameNotFoundException, it's because the pool
encountered an error when it was being created.  Hence it doesn't actually
exists.  A couple things you can do to troubleshoot.

1.) Set an initial size for your pool.  This will force it to make at least
one connection at startup which will generally cause it to fail fast, if
there is a problem.  You can then look in the logs to see the error.

2.) Connect with jconsole or jvisualvm and look at the mbeans for Tomcat.
When the pool is actually there, you should see it in the mbeans.


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

This looks suspect.  I'm pretty sure you want the JDBC driver name.


>         description="Global Address Database"
>         url="jdbc:oracle:thin:@10.15.120.29:1522:DGSPC"
>

You could add "initialSize" here, try "1".

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

You're using a different factory (specifically Oracle's implementation), so
the configuration attributes are going to be different.  Not sure how to
set initial size with this implementation.


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

This looks the most right.  I'd also try adding "initialSize" of 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"/>
>

These look OK.


> </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
>
>
Didn't test this, but it looks OK.

Dan



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

Reply via email to