Error looking up JNDI DataSource in tomcat 8.5

58 views Asked by At

I am using tomcat 8.5, where war files are copied in webapps folder and are exploded. We have a app context files defined in conf/Catalina/localhost/. I have a context defined in server.xml like below server.xml

<Host appBase="webapps" deployOnStartup="false" autoDeploy="false" name="localhost" unpackWARs="true">
    <Context displayName="Application One" path="/app1" reloadable="true"/>
</Host>

app1.xml

<Context displayName="Application One" path="/app1" swallowOutput="true">

    <Resource name="jdbc/APP1OS" auth="Container" type="javax.sql.DataSource" 
            driverClassName="com.mysql.cj.jdbc.Driver" 
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
            maxActive="100"
            maxIdle="10"
            maxWait="5000"
            maxAge="60000"
            username="username"
            password="password"
            url="jdbc:mysql://dbname:port/appint?useUnicode=true&amp;characterEncoding=UTF-8&amp;rewriteBatchedStatements=true&amp;characterSetResults=utf8&amp;serverTimezone=UTC&amp;socketTimeout=300000&amp;useSSL=false&amp;requireSSL=false&amp;allowPublicKeyRetrieval=true"
            testOnBorrow="true"
            validationQuery="Select '1'"/>      
</Context>

Then I get below issue:-

Caused by: java.sql.SQLException: Error creating delegate data source for 'JNDI: java:comp/env/jdbc/APP1OS'
Error looking up JNDI DataSource (not found).

If I change my server.xml to add resource definition in context then it works fine.

<Host appBase="webapps" deployOnStartup="false" autoDeploy="false" name="localhost" unpackWARs="true">
  <Context path="/app1" swallowOutput="true">
      Resource name="jdbc/APP1OS" auth="Container" type="javax.sql.DataSource" 
        driverClassName="com.mysql.cj.jdbc.Driver" 
        factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
        maxActive="100"
        maxIdle="10"
        maxWait="5000"
        maxAge="60000"
        username="username"
        password="password"
        url="jdbc:mysql://dbname:port/appint?useUnicode=true&amp;characterEncoding=UTF-8&amp;rewriteBatchedStatements=true&amp;characterSetResults=utf8&amp;serverTimezone=UTC&amp;socketTimeout=300000&amp;useSSL=false&amp;requireSSL=false&amp;allowPublicKeyRetrieval=true"
        testOnBorrow="true"
        validationQuery="Select '1'"/>   
 </Context>

</Host>
0

There are 0 answers