Search code examples
springspring-boottomcatdatasourcejndi

Spring boot - External tomcat 7 error in JNDI configuration : JNDI name is not related to this context


i need serious help please!! i am stuck from 10 days in same problem :/ !!

i have a web application with java8 that was deployed in WAS and i am migrating it to tomcat 7.

I integrated the app in spring boot 1.5.22 and it works correctly till now but DB connection still doesn't work. I want to connect it to oracle 10g using OJDBC6.jar

In the DB configuration class i have this code :

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class TDFAdminDBConnection
{
    protected String JNDILookup = "rnz_fta/jdbc/Datasource";
    
    protected DataSource ds = null;
    
    public TDFAdminDBConnection() {}
    
    public void freeConnection(Connection conn) throws SQLException
    {
        if(conn != null) conn.close();}
    
    public void connectToDataSource(String lookup) throws NamingException
    {
        try {
        InitialContext context = new InitialContext();
        this.ds = (DataSource)context.lookup(lookup);
        context.close();
    }catch(NamingException e) {
        System.out.println("connection failed ," + e.toString()); }}
    
    public Connection getConnection() throws SQLException
    {
        if(ds == null)
        {
            try {
                this.connectToDataSource(this.JNDILookup);
            }catch (NamingException e) {
                throw new SQLException(e.toString()); }}
        
        return this.ds.getConnection();
    }
    public Connection getConnection(String lookup) throws NamingException, SQLException
    {
        Connection connection = null;
        InitialContext context = new InitialContext();
        DataSource ds = (DataSource)context.lookup(lookup);
        connection = ds.getConnection();
        context.close();
        return connection;
    }   }

in the web.xml of the app i added this :

<resource-ref>
         <description>Oracle Datasource</description>
         <res-ref-name>rnz_fta/jdbc/Datasource</res-ref-name>
         <res-type>javax.sql.DataSource</res-type>
         <res-auth>Container</res-auth>
      </resource-ref>

And i added same code to the external Tomcat web.xml;

In the server.xml i added this under :

<Resource  auth="Container" global="rnz_fta/jdbc/Datasource" name="rnz_fta/jdbc/Datasource"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@xxx:1509:xxx"
              username="xxx" password="xxx" maxActive="20" maxIdle="10"
              maxWait="5000" />

And in context.xml i added this :

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

But i always get this error :

connection failed ,javax.naming.NameNotFoundException: Le Nom [rnz_fta/jdbc/Datasource] n'est pas lié à ce Contexte

JNDI name is not related to this context I followed official Tomcat 7.0.108 documentation as the version i am using; Then i changed the DB config class like this :

import java.sql.Connection;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class TDFAdminDBConnection
{
    protected String JNDILookup = "rnz_fta/jdbc/Datasource";
    private LogService log_dbg;
    protected DataSource ds = null;
    
    public TDFAdminDBConnection() {}
    
    public void freeConnection(Connection conn) throws SQLException
    {
        if(conn != null) conn.close();}
    
    public void connectToDataSource(String lookup) throws NamingException
    {
        try {
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");
            this.ds = (DataSource)envCtx.lookup(lookup);
        envCtx.close();
        }catch(NamingException e) {
            System.out.println("connection failed ," + e.toString());
        }
         log_dbg.debug("datasource =" + ds);}

    public Connection getConnection() throws SQLException
    {
        if(ds == null)
        {
            try {
                this.connectToDataSource(this.JNDILookup);
            }catch (NamingException e ) {
                throw new SQLException(e.toString());}}
         log_dbg.debug("datasource =" + ds);
           
        return this.ds.getConnection();}

    public Connection getConnection(String lookup) throws NamingException, SQLException
    {
        Connection connection = null;
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");
        DataSource ds = (DataSource)envCtx.lookup(lookup);
        connection = ds.getConnection(); 
        envCtx.close();
        log_dbg.debug("datasource =" + ds);
        log_dbg.debug("connection =" + connection);
        return connection;}}
    
But also didn't solve the problem !!

I created a context.xml inside My app in /src/main/webapp/META-INF and added this code :

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\tdfadmin-0.0.1-SNAPSHOT.war" path="" reloadable="true"  debug="1">
<Resource name="rnz_fta/jdbc/Datasource" auth="Container"
              type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
              url="jdbc:oracle:thin:@xxx:1509:xxx"
              username="xxx" password="xxx" maxActive="20" maxIdle="10"
              maxWait="5000" removeAbandoned="true" removeAbandonedTimeout="30" logAbandoned="true"/>
<ResourceLink global="rnz_fta/jdbc/Datasource" name="rnz_fta/jdbc/Datasource" type="javax.sql.DataSource"/>
</Context>

also same error !!

i added ojdbc6.jar and tomcat-jdbc.jar in tomcat/lib and in vain!!

even the property spring.datasource.jndi-name in app.properties didn't solve the problem :/

here are some of my maven dependencies :

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
<dependency>
            <groupId>oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>

Here is last line of console output in tomcat :

connection failed ,javax.naming.NameNotFoundException: Le Nom [rnz_fta/jdbc/Datasource] n'est pas lié à ce Contexte
Sat Aug 14 17:44:38 CEST 2021
java.lang.NullPointerException
  at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Unknown Source)

Here is the ControlerConnection class:

import java.sql.Connection;
import java.sql.SQLException;


public class ControlerConnection {
    protected Connection connection;

    public TDFAdminDBConnection tdfDBConnection = null;
    
    public ControlerConnection() 
    {
        tdfDBConnection = new TDFAdminDBConnection();
    }
    
    public void freeConnection(Connection conn) throws SQLException
    {
        tdfDBConnection.freeConnection(conn);
    }

    public void getConnection() throws SQLException
    {
        this.connection = tdfDBConnection.getConnection();
//      connection.setAutoCommit(false);
    }
}

I am really stack!! does anyone had the same problem please??????


Solution

  • Solved! the problem is that i was using a wrong version of ojdbc driver ! ojdbc6-11.1.0.7.0.jar solved the problem