简体   繁体   English

Spring 启动多个 jndi 连接

[英]Spring boot multiple jndi connections

please base with my English, i am using spring boot 2 and registering 3 jndi connection with the tomcat like below:请以我的英语为基础,我正在使用 spring boot 2 并注册 3 个 jndi 与 tomcat 的连接,如下所示:

@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.现在在使用它时显示错误:javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/SampleData] is not bound in this Context。 Unable to find [jdbc].无法找到 [jdbc]。

Why is this happening?为什么会这样?

Create another context for resource2 : ContextResource resource2 = new ContextResource();为 resource2 创建另一个上下文: ContextResource resource2 = new ContextResource(); then add resource 2 in conext context.getNamingResources().addResource(resource2);然后在conext context.getNamingResources().addResource(resource2);添加资源2 context.getNamingResources().addResource(resource2);

You are using same context resource of first..您正在使用相同的上下文资源 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM