2

please base with my English, i am using spring boot 2 and registering 3 jndi connection with the tomcat like below:

@Bean
public TomcatServletWebServerFactory  tomcatFactory() {
    return new TomcatServletWebServerFactory() {
        @Override
        protected TomcatWebServer getTomcatWebServer(org.apache.catalina.startup.Tomcat tomcat) {
            tomcat.enableNaming(); 

            return super.getTomcatWebServer(tomcat);
        }

        @Override
        protected void postProcessContext(Context context) {

             //Jndi connection 1


            ContextResource resource = new ContextResource();
            resource.setName("jdbc/masterTable");
            resource.setType(DataSource.class.getName());

            resource.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver");
            resource.setProperty("url", "jdbc:oracle:thin:@localhost:1521/xe");
            resource.setProperty("username", "root");
            resource.setProperty("password", "root");


             //Jndi connection 2

            context.getNamingResources().addResource(resource);
             resource = new ContextResource();
                resource.setName("jdbc/SampleData");
                resource.setType(DataSource.class.getName());

                resource.setProperty("driverClassName", "org.hsqldb.jdbcDriver");
                resource.setProperty("url", "jdbc:hsqldb:hsql://localhost:9001/sampledata");
                resource.setProperty("username", "pentaho_user");
                resource.setProperty("password", "password");
            context.getNamingResources().addResource(resource);


        }
    };

Now while using it shows error : javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/SampleData] is not bound in this Context. Unable to find [jdbc].

Why is this happening?

user3458271
  • 638
  • 12
  • 31
  • Can you try oracle url: jdbc:oracle:thin:@localhost:1521:xe – kj007 Oct 03 '18 at 12:28
  • @kj007 i don't think it's there any issue with connection url it's something related to the context or something else – user3458271 Oct 03 '18 at 12:30
  • ahh seems you are trying to configure multiple resources with same context, please create another context for hsqldb i think ContextResource resource2 = new ContextResource(); – kj007 Oct 03 '18 at 12:47
  • but i am creating new object every time resource = new ContextResource(); will it not be enough – user3458271 Oct 03 '18 at 13:04
  • it will override same because of objects are mutable..need to create new instance of ContextResource..is it not working after this change?? – kj007 Oct 03 '18 at 13:06
  • no not working it dosen't make any difference – user3458271 Oct 03 '18 at 13:43
  • refer - https://stackoverflow.com/questions/32776410/configure-multiple-datasource-in-spring-boot-with-jndi/59475518#59475518 – subrat22 Dec 25 '19 at 05:43

1 Answers1

1

Create another context for resource2 : ContextResource resource2 = new ContextResource(); then add resource 2 in conext context.getNamingResources().addResource(resource2);

You are using same context resource of first..

               //Jndi connection 2


                ContextResource resource2 = new ContextResource();
                resource2.setName("jdbc/SampleData");
                resource2.setType(DataSource.class.getName());

                resource2.setProperty("driverClassName", "org.hsqldb.jdbcDriver");
                resource2.setProperty("url", "jdbc:hsqldb:hsql://localhost:9001/sampledata");
                resource2.setProperty("username", "pentaho_user");
                resource2.setProperty("password", "password");
                context.getNamingResources().addResource(resource2);
kj007
  • 6,073
  • 4
  • 29
  • 47