Tomcat配置多数据源出现Unable to create initial connections

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat配置多数据源出现Unable to create initial connections相关的知识,希望对你有一定的参考价值。

迁自恶心人的网易博客 2017-06-27
完整标题:Tomcat配置多个JNDI数据源(Postgresql)出现Unable to create initial connections of pool

版本说明

tomcat7
Postgresql9.1

问题描述

在conf/context.xml中配置了两个数据源指向如下:

<Resource name="jdbc/web"
          auth="Container"
          type="javax.sql.DataSource"
          factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
          testWhileIdle="true"
          testOnBorrow="true"
          testOnReturn="false"
          validationQuery="SELECT 1"
          timeBetweenEvictionRunsMillis="30000"
          maxActive="10"
          maxIdle="5"
          minIdle="1"
          removeAbandonedTimeout="60"
          removeAbandoned="false"
          logAbandoned="true"
          minEvictableIdleTimeMillis="30000"
          jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
          username="postgres"
          password=""
          driverClassName="org.postgresql.Driver"
          url="jdbc:postgresql://localhost:5432/dbname?client_encoding=GBK"/>

<Resource name="jdbc/storage"
      auth="Container"
      type="javax.sql.DataSource"
      factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
      testWhileIdle="true"
      testOnBorrow="true"
      testOnReturn="false"
      validationQuery="SELECT 1"
      timeBetweenEvictionRunsMillis="30000"
      maxActive="10"
      maxIdle="5"
      minIdle="1"
      removeAbandonedTimeout="60"
      removeAbandoned="false"
      logAbandoned="true"
      minEvictableIdleTimeMillis="30000"
      jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer"
      username="postgres"
      password=""
      driverClassName="org.postgresql.Driver"
      url="jdbc:postgresql://localhost:5432/dbname?client_encoding=GBK"/>

启动tomcat出现如下异常:

严重: Unable to create initial connections of pool.
org.postgresql.util.PSQLException: ????????: ??????, ????????????????
    at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:291)
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:106)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
    at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:123)
    at org.postgresql.jdbc3.AbstractJdbc3Connection.<init>(AbstractJdbc3Connection.java:28)
    at org.postgresql.jdbc3g.AbstractJdbc3gConnection.<init>(AbstractJdbc3gConnection.java:20)
    at org.postgresql.jdbc4.AbstractJdbc4Connection.<init>(AbstractJdbc4Connection.java:30)
    at org.postgresql.jdbc4.Jdbc4Connection.<init>(Jdbc4Connection.java:22)
    at org.postgresql.Driver.makeConnection(Driver.java:391)
    at org.postgresql.Driver.connect(Driver.java:265)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:278)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connect(PooledConnection.java:182)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:730)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:664)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:482)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:156)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.createDataSource(DataSourceFactory.java:560)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:245)
    at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:141)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:848)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:836)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1110)
    at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:689)
    at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:273)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5522)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1260)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2002)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    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)

六月 27, 2017 9:43:35 下午 org.apache.tomcat.jdbc.pool.ConnectionPool abandon
警告: Connection has been abandoned PooledConnection[[email protected]]:java.lang.Exception
    at org.apache.tomcat.jdbc.pool.ConnectionPool.getThreadDump(ConnectionPool.java:1096)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:735)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.borrowConnection(ConnectionPool.java:664)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.init(ConnectionPool.java:482)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.<init>(ConnectionPool.java:156)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.pCreatePool(DataSourceProxy.java:116)
    at org.apache.tomcat.jdbc.pool.DataSourceProxy.createPool(DataSourceProxy.java:103)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.createDataSource(DataSourceFactory.java:560)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:245)
    at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:141)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:848)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:159)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:836)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:173)
    at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1110)
    at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:689)
    at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:273)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5522)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
    at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1260)
    at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:2002)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    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)

Google了半天也没找到啥原因,只能一点点尝试排除,简化配置,发现保留一个Resource节点时Tomcat可以正常启动,一旦大于一个就出异常,然后查了Tomcat官方文档明确写到Context节点下可以嵌套多个Resource节点,那难道是Postgresql的问题?尝试换了一个数据库驱动jar包也没啥用。还是把问题定位在Tomcat多JNDI数据源配置上,折腾了半天,最后换了一种数据源的配置方式,问题竟然解决了。

解决办法

换另一种配置方式:把Resource节点放到server.xml的GlobalNamingResources节点下,然后在context.xml中添加如下配置:

<ResourceLink global="jdbc/web" name="jdbc/web" type="javax.sql.DataSource" />
<ResourceLink global="jdbc/storage" name="jdbc/storage" type="javax.sql.DataSource" />

采用这种方式配置Tomcat可正常启动,数据源也可用,但是出现这个bug的原因还是没搞清楚。。。

以上是关于Tomcat配置多数据源出现Unable to create initial connections的主要内容,如果未能解决你的问题,请参考以下文章

01.Eclipse tomcat数据源配置Unable to find [jdbc]

解决tomcat was unable to start within问题

python多版本配置后使用pip出现 Fatal error in launcher: Unable to create process using '"'

tomcat报错-----》Unable to open debugger port IDEA Unable to open debugger port

tomcat启动超时_tomcat was unable to start within

Springboot项目编译正常启动Unable to start embedded Tomcat报错