Spring Boot多个JNDI连接

时间:2018-10-03 11:40:17

标签: java spring spring-boot

请以我的英语为基础,我正在使用Spring Boot 2,并与tomcat注册3 jndi连接,如下所示:

@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);


        }
    };

现在使用它时显示错误: javax.naming.NameNotFoundException:名称[java:comp / env / jdbc / SampleData]在此上下文中未绑定。找不到[jdbc]。

为什么会这样?

1 个答案:

答案 0 :(得分:0)

为resource2创建另一个上下文:ContextResource resource2 = new ContextResource();,然后在conext context.getNamingResources().addResource(resource2);中添加资源2

您正在使用与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);