javatomcatconnection-poolingjdbc-pool

Naming error while using multiple connection pools in a Tomcat 8 application


I have an application that uses the built-in Tomcat connection pool, and for the most part it works. A problem arises when I'm trying to use another pool to get a different set of connection from the same database, but from a different username/password (It's an oracle database that uses 2 usernames to access different namespaces of tables and function).

The first pool is accepted, but for the second one, I'm getting this error

15:09:47.157 [http-nio-8081-exec-5] ERROR com.applicationname.providers.ConnectionManager - NamingException in MyDataSource
javax.naming.NameNotFoundException: Name [appdb_two] is not bound in this Context. Unable to find [appdb_two].
    at org.apache.naming.NamingContext.lookup(NamingContext.java:818) ~[catalina.jar:8.0.15]

Here is my configuration:

server.xml

<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/appdb_two"
              auth="Container"
              type="javax.sql.DataSource"
              driverClassName="oracle.jdbc.driver.OracleDriver"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"/>

    <Resource name="jdbc/appdb_one"
              auth="Container"
              type="javax.sql.DataSource"
              driverClassName="oracle.jdbc.driver.OracleDriver"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"/>

</GlobalNamingResources>

context.xml

<ResourceLink name="jdbc/appdb_two" 
              auth="Container"             
              username="DBONE"
              password="xxxx" 
              type="javax.sql.DataSource" 
              url="jdbc:oracle:thin:@xx.xxx.xxx.xxx:1521:XE"
              initialSize="20"
              maxActive="50"
              maxIdle="20"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"/>

<ResourceLink name="jdbc/appdb_one" 
              auth="Container"
              username="DBTWO"
              password="xxxx" 
              type="javax.sql.DataSource" 
              url="jdbc:oracle:thin:@xx.xxx.xxx.xxx:1521:XE"
              initialSize="20"
              maxActive="50"
              maxIdle="20"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"/>
...

Solution

  • I think you're looking up the second datasource via name "appdb_two", but should be using "jdbc/appdb_two" - it's hard to see from the stacktrace alone, code for lookup would be helpful.

    Also check that your web.xml has references to both data sources (<resource-ref> elements).