0
votes

I have done the bellow mentioned configuration for JNDI DataSource connection for Spring Boot Application that uses an external tomcat.

Tomcat's server.xml configuration like

         <GlobalNamingResources>
            <!-- Editable user database that can also be used by
                 UserDatabaseRealm to authenticate users
            -->

            <Resource name="jdbc/MyPostgresDB" 
              global="jdbc/MyPostgresDB" 
              auth="Container" 
              type="javax.sql.DataSource" 
              driverClassName="org.postgresql.Driver" 
              url="jdbc:postgresql://localhost:5432/postgres" 
              username="postgres" 
              password="postgres" 

              maxActive="100" 
              maxIdle="20" 
              minIdle="5" 
              maxWait="10000"/>

Context.xml like

        <Context>

            <!-- Default set of monitored resources -->
            <WatchedResource>WEB-INF/web.xml</WatchedResource>

            <ResourceLink name="jdbc/MyPostgresDB"
                        global="jdbc/MyPostgresDB”
                        auth="Container"
                        type="javax.sql.DataSource" />

Defined the application.properties for spring boot in right way like

spring.datasource.jndi-name=java:comp/env/jdbc/MyPostgres

Updated the web.xml like

        <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

    <resource-ref>
        <description>JNDI LookUp</description>
        <res-ref-name>jdbc/MyPostgresDB</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <!-- <res-sharing-scope>Shareable</res-sharing-scope> -->
    </resource-ref>

    </web-app>

Still getting the exception like

org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'java:comp/env/jdbc/MyPostgres'

(Full Stacktrace not given as every where it is given)

So what is the solution or why I am getting this exception.

Don't come with the idea of embedded tomcat server for spring boot. Please help me.

2
Defined the application.properties for spring boot in right way like spring.datasource.jndi-name=java:comp/env/jdbc/MyPostgresDB Still getting the exception(In the original post it was typo mistake sorry for that)008ak89

2 Answers

0
votes

we should add factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" in Resource tag of server.xml and make sure to set JndiDatasource in the class where you extends SpringBootServletInitializer class

For elaborate answer please refer here

-1
votes

Should be

spring.datasource.jndi-name=java:comp/env/jdbc/MyPostgresDB

from what you wrote above in your definitions.