当前位置: 首页 > 知识库问答 >
问题:

Spring Boot、多数据源和JNDI连接

杨波娃
2023-03-14

我无法连接到两个数据源使用在JDNI与Spring Boot。

生成以下stacktrace:

TID: [0] [AS] [2015-04-06 10:53:50,795] ERROR
{org.apache.tomcat.jdbc.pool.ConnectionPool} -  Unable to create
initial connections of pool.
{org.apache.tomcat.jdbc.pool.ConnectionPool} java.sql.SQLException:
The url cannot be null  at
java.sql.DriverManager.getConnection(DriverManager.java:556)    at
java.sql.DriverManager.getConnection(DriverManager.java:187)    at
org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:305)
    at
org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:200)
    at
org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:699)
    at
org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:633)
    at
org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:484)
    at
org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:142)
    at
org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:115)
    at
org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:102)
    at
org.apache.tomcat.jdbc.pool.DataSourceProxy.getConnection(DataSourceProxy.java:126)
    at
org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
    at
org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
    at
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:339)
    at
org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection.isEmbedded(EmbeddedDatabaseConnection.java:126)
    at
org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate.getDefaultDdlAuto(JpaProperties.java:203)
    at
org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate.getOrDeduceDdlAuto(JpaProperties.java:191)
    at
org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate.getAdditionalProperties(JpaProperties.java:179)
    at
org.springframework.boot.autoconfigure.orm.jpa.JpaProperties$Hibernate.access$000(JpaProperties.java:130)
    at
org.springframework.boot.autoconfigure.orm.jpa.JpaProperties.getHibernateProperties(JpaProperties.java:127)
    at
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration.getVendorProperties(HibernateJpaAutoConfiguration.java:99)
    at
org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.entityManagerFactory(JpaBaseConfiguration.java:111)
    at
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$d2a55c5.CGLIB$entityManagerFactory$5(<generated>)
    at
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$d2a55c5$$FastClassBySpringCGLIB$$caf87827.invoke(<generated>)
    at
org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at
org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309)
    at
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration$$EnhancerBySpringCGLIB$$d2a55c5.entityManagerFactory(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)     at
org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)
    at
org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)
    at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111)
    at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006)
    at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at
org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
    at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
    at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
    at
org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
    at
org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:117)
    at
org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:108)
    at
org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:68)
    at
org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)
    at
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5274)
    at
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at
org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at
org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at
org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
    at
org.wso2.carbon.tomcat.internal.CarbonTomcat.addWebApp(CarbonTomcat.java:280)   at
org.wso2.carbon.tomcat.internal.CarbonTomcat.addWebApp(CarbonTomcat.java:177)   at
org.wso2.carbon.webapp.mgt.TomcatGenericWebappsDeployer.handleWebappDeployment(TomcatGenericWebappsDeployer.java:222)
    at
org.wso2.carbon.webapp.mgt.TomcatGenericWebappsDeployer.handleWarWebappDeployment(TomcatGenericWebappsDeployer.java:174)
    at
org.wso2.carbon.webapp.mgt.TomcatGenericWebappsDeployer.handleHotDeployment(TomcatGenericWebappsDeployer.java:141)
    at
org.wso2.carbon.webapp.mgt.TomcatGenericWebappsDeployer.deploy(TomcatGenericWebappsDeployer.java:116)
    at
org.wso2.carbon.webapp.mgt.AbstractWebappDeployer.deployThisWebApp(AbstractWebappDeployer.java:140)
    at
org.wso2.carbon.webapp.mgt.AbstractWebappDeployer.deploy(AbstractWebappDeployer.java:90)
    at
org.wso2.carbon.webapp.deployer.WebappDeployer.deploy(WebappDeployer.java:42)   at
org.wso2.carbon.webapp.mgt.AbstractWebappDeployer.handleRedeployment(AbstractWebappDeployer.java:410)
    at
org.wso2.carbon.webapp.mgt.AbstractWebappDeployer.undeploy(AbstractWebappDeployer.java:188)
    at
org.apache.axis2.deployment.DeploymentEngine.unDeploy(DeploymentEngine.java:914)
    at
org.apache.axis2.deployment.repository.util.WSInfoList.update(WSInfoList.java:143)
    at
org.apache.axis2.deployment.RepositoryListener.update(RepositoryListener.java:377)
    at
org.apache.axis2.deployment.RepositoryListener.checkServices(RepositoryListener.java:254)
    at
org.apache.axis2.deployment.RepositoryListener.startListener(RepositoryListener.java:371)
    at
org.apache.axis2.deployment.scheduler.SchedulerTask.checkRepository(SchedulerTask.java:59)
    at
org.apache.axis2.deployment.scheduler.SchedulerTask.run(SchedulerTask.java:67)
    at
org.wso2.carbon.core.deployment.CarbonDeploymentSchedulerTask.runAxisDeployment(CarbonDeploymentSchedulerTask.java:79)
    at
org.wso2.carbon.core.deployment.CarbonDeploymentSchedulerTask.run(CarbonDeploymentSchedulerTask.java:124)
    at
java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
    at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
    at
java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
spring:
    profiles:
        active: development

---

spring:
    profiles: local

datasource:
    future:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://<host>:<port>/<database>
        username: <username>
        password: <password>
        jpa:
            hibernate:
                ddl-auto: validate
            database-platform: org.hibernate.dialect.MySQL5Dialect
            show-sql: true
    past:
        driver-class-name: com.informix.jdbc.IfxDriver
        url: jdbc:informix-sqli://<host>:<port>/<database>:INFORMIXSERVER=<server>
        jpa:
            hibernate:
                ddl-auto: validate
            database-platform: org.hibernate.dialect.InformixDialect
            show-sql: true
jmx:
    enabled: false
logging:
    file: /logs/my.log
    level:
        org.springframework: 'OFF'
---

spring:
    profiles: development
datasource:
    future:
        datasource:
            jndi-name: java:/comp/env/jdbc/ds1
            platform: mysql
        jpa:
            hibernate:
                ddl-auto: validate
            database-platform: org.hibernate.dialect.MySQL5Dialect
            show-sql: true
    past:
        datasource:
            jndi-name: java:/comp/env/jdbc/ds2
            platform: informix
        jpa:
            hibernate:
                ddl-auto: validate
            database-platform: org.hibernate.dialect.InformixDialect
            show-sql: true
jmx:
    enabled: false
logging:
    file: /logs/my.log
    level:
        org.springframework: 'OFF'
package com.mydomain.app.appconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.sql.DataSource;

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = {"com.mydomain.app"})
@EnableJpaRepositories(basePackages = "com.mydomain.app.repository")
@EntityScan(basePackages = {"com.mydomain.app.entity"})
public class Application {
    @Bean
    @Primary
    @ConfigurationProperties(prefix = "datasource.future")
    public DataSource futureDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "datasource.past")
    public DataSource pastDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    public JdbcTemplate futureJdbcTemplate() {
        return new JdbcTemplate(futureDataSource());
    }

    @Bean
    public JdbcTemplate pastJdbcTemplate() {
        return new JdbcTemplate(pastDataSource());
    }

    public static void main(String[] args) {
        SpringApplication.run(StayServiceApplication.class, args);
    }
}
<Context>

    <!-- Default set of monitored resources -->

    <Loader className="org.wso2.carbon.webapp.mgt.loader.CarbonWebappLoader" loaderClass="org.wso2.carbon.webapp.mgt.loader.CarbonWebappClassLoader"/>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->

<Resource name="jdbc/stayds" auth="Container"
          type="javax.sql.DataSource"
          driverClassName="com.mysql.jdbc.Driver"
          maxActive="30" maxIdle="10" maxWait="10000"
          url="jdbc:mysql://<host>:<post>/<database>"
          username="<username>" password="<password>" />

<Resource name="jdbc/gpmds" auth="Container"
          type="javax.sql.DataSource"
          driverClassName="com.informix.jdbc.IfxDriver"
          url="jdbc:informix-sqli://<host>:<post>/<database>:INFORMIXSERVER=<server>" 
          maxActive="30" maxIdle="10" maxWait="10000"
          removeAbandoned="true" removeAbandonedTimeout="60"
          logAbandoned="true" />    
</Context>

我做错了什么?

共有1个答案

朱宇航
2023-03-14

应用程序已部署到WSO2应用程序服务器上。在WSO2应用程序控制台的JNDI连接配置中添加一个JNDI属性解决了这个问题。

 类似资料:
  • 我正在尝试使用两个数据源与我的SpringBoot应用程序,但无法获得第二个数据源自动连接。我尝试过很多事情,但这是我最接近的一次: 我的Yaml文件: 这是我到目前为止最接近的一次。我之所以说它是最接近的,是因为如果我删除@qualifier,那么我的两个dao方法实际上都可以工作,假设SECOND_SELECT语句对于我的DB1是有效的SQL语句。当我为非主datasouce输入@Qualif

  • 问题内容: 大家好,我使用Hibernate + Struts2 + Tomcat6 + Mysql作为我的J2EE框架。我一直在使用hibernate的内置连接池机制,但事实证明,在8小时后mysql关闭其连接的地方,这是一个问题。无论如何,我在Google上搜索了一下,发现我应该通过JNDI数据源获得连接,但是我无法获得关于此的完整的教程。我应该采取什么步骤呢?请提供足够的详细信息,对此我有点

  • 我试图创建一个Spring Boot应用程序,它连接两个数据源。我可以通过遵循Spring文档来实现这一点,但我面临的挑战是实现以下目标 null 是否有任何方法可以使用Spring Boot的自动配置功能来实现这一点,或者我必须基于概要文件创建不同的数据源bean。

  • 问题内容: 我想在Java SE应用程序中使用JNDI配置数据源。做这个的最好方式是什么? 到目前为止,我遇到了两个项目: Apache命名。项目页面上有一个用于配置数据源的特定示例,但看起来该项目已过时并且不再处于活动状态。 JBossNS。使用似乎很容易配置仅本地的JNDI ,但我还没有找到任何有关如何实际配置数据源的文档。 如果可能,我还要使用JTA事务管理器(使用JOTM?)配置数据源。

  • 嗨,我正在尝试使用jndi数据源。下面是代码 context.xml 在spring servlet配置bean中是: 我得到这个错误 org.springframework.beans.factory.beanCreationException:创建名为“Contact Controller”的bean时出错:注入autowired依赖项失败;嵌套异常为org.SpringFramework.B

  • 简单地说。我希望有一个oracle jndi数据源可用于带有连接池的webapp。我想maven安装该项目,然后手动将其部署到jetty hightide 7。xx服务器并运行。我在WEB-INF文件夹中放置了一个jetty env文件,如下所示。 这是我的dataSource bean... Maven安装很好,但当我部署到jetty服务器并启动它时,我会遇到以下错误。 我已经在jetty pl