简体   繁体   English

Tomcat JDBC池:连接太多

[英]Tomcat JDBC Pool: Too many connections

I am using Tomcat JDBC pool for MySQL DB connection pool in my web application. 我在我的Web应用程序中使用Tomcat JDBC池作为MySQL数据库连接池。 The JDBC pool is declared as resource in context.xml of application (not at global level). JDBC池在应用程序的context.xml中声明为资源(不是在全局级别)。 I am developing this web application in Eclipse IDE. 我正在Eclipse IDE中开发这个Web应用程序。 So, whenever I make changes to code, eclipse makes Tomcat server to reload context. 因此,每当我对代码进行更改时,eclipse都会使Tomcat服务器重新加载上下文。

But the problem is, when context gets reloaded, tomcat creates JDBC pool with new connections without releasing old pool. 但问题是,当重新加载上下文时,tomcat会创建具有新连接的JDBC池而不释放旧池。 And after few more changes to code, the tomcat eventually exhausts MySQL server maximum connection limit, and tomcat starts displaying "Too many connections" error on context reloads. 在对代码进行了几次更改后,tomcat最终耗尽了MySQL服务器的最大连接限制,并且tomcat开始在上下文重新加载时显示“Too many connections”错误。

My context.xml file is like: 我的context.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Resource
        name="jdbc/myDB"
        auth="Container"
        type="javax.sql.DataSource"
        factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
        username="root"
        password="admin"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/myDB?allowMultiQueries=true"
        defaultAutoCommit="false"
        initialSize="20"
        maxActive="50"
        maxIdle="30"
        minIdle="15"
        maxWait="5000"
        testOnBorrow="true"
        testWhileIdle="true"
        validationQuery="SELECT 1"
        timeBetweenEvictionRunsMillis="35000"
        minEvictableIdleTimeMillis="55000"
        removeAbandoned="true"
        removeAbandonedTimeout="3600"
        logAbandoned="true"
        validationInterval="35000"
    />
</Context>

My web.xml is like: 我的web.xml是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>MyApp</display-name>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <resource-ref>
      <description>MyApp DB Connection</description>
      <res-ref-name>jdbc/myDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>

  <context-param>
    <param-name>data-source-lookup-name</param-name>
    <param-value>java:comp/env/jdbc/myDB</param-value>
  </context-param>

  <listener>
    <listener-class>com.project.listeners.AppListener</listener-class>
  </listener>
</web-app>

App Listener is like: App Listener就像:

package com.project.listeners;

import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import com.mysql.jdbc.AbandonedConnectionCleanupThread;

public class AppListener implements ServletContextListener {
    private static Logger logger = Logger.getLogger(AppListener.class);

    @Override
    public void contextInitialized(ServletContextEvent event) {
        logger.info("Application context initialing");

        ServletContext context = event.getServletContext();

        DBObjectNames.dataSourceLookupName = context.getInitParameter("data-source-lookup-name");
        if(DBObjectNames.dataSourceLookupName == null){
            logger.fatal("data-source-lookup-name init parameter not supplied, using default: java:comp/env/jdbc/myDB");
            DBObjectNames.dataSourceLookupName = "java:comp/env/jdbc/myDB";
        }
        DBObjectNames.dataSourceLookupName = DBObjectNames.dataSourceLookupName.trim();
        logger.info("data-source-lookup-name set to: " + DBObjectNames.dataSourceLookupName);

        logger.info("Application context initialed");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        logger.info("Application context destroying");

        try {
            logger.info("Shutting down AbandonedConnectionCleanupThread");
            AbandonedConnectionCleanupThread.shutdown();
            logger.info("Shut down AbandonedConnectionCleanupThread");
        } catch (Throwable t) {
            logger.fatal("Error while shutting down AbandonedConnectionCleanupThread", t);
        }

        Enumeration<Driver> drivers = DriverManager.getDrivers();
        while (drivers.hasMoreElements()) {
            Driver driver = drivers.nextElement();
            try {
                logger.info("Deregistering driver: " + driver);
                DriverManager.deregisterDriver(driver);
                logger.info("Deregistered driver: " + driver);
            } catch (Throwable t) {
                logger.fatal("Error while deregistering driver: " + driver, t);
            }
        }

        logger.info("Application context destroyed");
    }
}

And I get exceptions like: 我得到例外情况:

Oct 30, 2014 4:16:54 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/myapp] is completed
Oct 30, 2014 4:17:04 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/myapp] has started
2014-10-30 16:17:04,497 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Application context destroying
2014-10-30 16:17:04,498 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Shutting down AbandonedConnectionCleanupThread
2014-10-30 16:17:04,498 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Shut down AbandonedConnectionCleanupThread
2014-10-30 16:17:04,498 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Application context destroyed
Oct 30, 2014 4:17:04 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2014-10-30 16:17:04,660 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Application context initialing
2014-10-30 16:17:04,661 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - data-source-lookup-name set to: java:comp/env/jdbc/myDB
2014-10-30 16:17:04,662 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Application context initialed
Oct 30, 2014 4:17:04 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/myapp] is completed
Oct 30, 2014 4:17:14 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/myapp] has started
2014-10-30 16:17:14,665 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Application context destroying
2014-10-30 16:17:14,666 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Shutting down AbandonedConnectionCleanupThread
2014-10-30 16:17:14,666 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Shut down AbandonedConnectionCleanupThread
2014-10-30 16:17:14,666 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Application context destroyed
Oct 30, 2014 4:17:14 PM org.apache.tomcat.jdbc.pool.ConnectionPool init
SEVERE: Unable to create initial connections of pool.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.Util.getInstance(Util.java:360)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1037)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2234)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.GeneratedConstructorAccessor12.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:307)
    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.DataSourceFactory.createDataSource(DataSourceFactory.java:553)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:241)
    at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:94)
    at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:841)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
    at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1084)
    at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:663)
    at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:256)
    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:5120)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3821)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:292)
    at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5576)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1377)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
    at java.lang.Thread.run(Unknown Source)

Oct 30, 2014 4:17:14 PM org.apache.tomcat.jdbc.pool.ConnectionPool abandon
WARNING: Connection has been abandoned PooledConnection[com.mysql.jdbc.JDBC4Connection@1090e8d6]:java.lang.Exception
    at org.apache.tomcat.jdbc.pool.ConnectionPool.getThreadDump(ConnectionPool.java:1052)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:704)
    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.DataSourceFactory.createDataSource(DataSourceFactory.java:553)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:241)
    at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:94)
    at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:841)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
    at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1084)
    at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:663)
    at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:256)
    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:5120)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3821)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:292)
    at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5576)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1377)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
    at java.lang.Thread.run(Unknown Source)

Oct 30, 2014 4:17:14 PM org.apache.tomcat.jdbc.pool.ConnectionPool abandon
WARNING: Connection has been abandoned PooledConnection[com.mysql.jdbc.JDBC4Connection@c572aa4]:java.lang.Exception
    at org.apache.tomcat.jdbc.pool.ConnectionPool.getThreadDump(ConnectionPool.java:1052)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:704)
    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.DataSourceFactory.createDataSource(DataSourceFactory.java:553)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:241)
    at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:94)
    at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:841)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
    at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1084)
    at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:663)
    at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:256)
    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:5120)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3821)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:292)
    at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5576)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1377)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
    at java.lang.Thread.run(Unknown Source)

Oct 30, 2014 4:17:14 PM org.apache.tomcat.jdbc.pool.ConnectionPool abandon
WARNING: Connection has been abandoned PooledConnection[com.mysql.jdbc.JDBC4Connection@72e8fcdd]:java.lang.Exception
    at org.apache.tomcat.jdbc.pool.ConnectionPool.getThreadDump(ConnectionPool.java:1052)
    at org.apache.tomcat.jdbc.pool.ConnectionPool.createConnection(ConnectionPool.java:704)
    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.DataSourceFactory.createDataSource(DataSourceFactory.java:553)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:241)
    at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:94)
    at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:841)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
    at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1084)
    at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:663)
    at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:256)
    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:5120)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3821)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:292)
    at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5576)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1377)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
    at java.lang.Thread.run(Unknown Source)

Oct 30, 2014 4:17:14 PM org.apache.naming.NamingContext lookup
WARNING: Unexpected exception resolving reference
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection,  message from server: "Too many connections"
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.Util.getInstance(Util.java:360)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1037)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2234)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2265)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2064)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:790)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
    at sun.reflect.GeneratedConstructorAccessor12.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:395)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:307)
    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.DataSourceFactory.createDataSource(DataSourceFactory.java:553)
    at org.apache.tomcat.jdbc.pool.DataSourceFactory.getObjectInstance(DataSourceFactory.java:241)
    at org.apache.naming.factory.FactoryBase.getObjectInstance(FactoryBase.java:94)
    at javax.naming.spi.NamingManager.getObjectInstance(Unknown Source)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:841)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:152)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:829)
    at org.apache.naming.NamingContext.lookup(NamingContext.java:166)
    at org.apache.catalina.core.NamingContextListener.addResource(NamingContextListener.java:1084)
    at org.apache.catalina.core.NamingContextListener.createNamingContext(NamingContextListener.java:663)
    at org.apache.catalina.core.NamingContextListener.lifecycleEvent(NamingContextListener.java:256)
    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:5120)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3821)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:292)
    at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5576)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1377)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
    at java.lang.Thread.run(Unknown Source)

Oct 30, 2014 4:17:14 PM org.apache.catalina.core.NamingContextListener addResource
WARNING: Failed to register in JMX: javax.naming.NamingException: Data source rejected establishment of connection,  message from server: "Too many connections"
Oct 30, 2014 4:17:14 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
2014-10-30 16:17:14,788 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Application context initialing
2014-10-30 16:17:14,789 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - data-source-lookup-name set to: java:comp/env/jdbc/myDB
2014-10-30 16:17:14,791 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  com.project.listeners.AppListener  - Application context initialed
Oct 30, 2014 4:17:14 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/myapp] is completed

More information: 更多信息:

These happens without testing application, that is, I am not executing web application to open connections, still just writing code, so there wont be any matter of opening connection and keeping it alive without closing it. 这些都是在没有测试应用程序的情况下发生的,也就是说, 我没有执行Web应用程序来打开连接,仍然只是编写代码,因此不会打开连接并保持活着而不关闭它。

Using Apache Tomcat: Version 8.0. 使用Apache Tomcat:8.0版。 and Eclipse Luna Release. 和Eclipse Luna发布。

Please help me on this. 请帮帮我。

EDIT 编辑

Every time context is getting reloaded, I see more 15 opened connections to DB in MySQL Workbench before hitting maximum connection limit of 151 connections to MySQL server. 每次上下文重新加载时,我都会在MySQL Workbench中看到更多15个与DB打开的连接,然后达到与MySQL服务器的151个连接的最大连接限制。

I think you have to add closeMethod="close" to your Resource in context.xml. 我认为你必须在context.xml中为你的资源添加closeMethod =“close”。 Then Tomcat should close your DB connections when you reload context. 然后,当您重新加载上下文时,Tomcat应该关闭数据库连接。

Modified context.xml: 修改后的context.xml:

<?xml version="1.0" encoding="UTF-8"?>
 <Context>
    <Resource
        name="jdbc/myDB"
        auth="Container"
        type="javax.sql.DataSource"
        factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
        username="root"
        password="admin"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/myDB?allowMultiQueries=true"
        defaultAutoCommit="false"
        initialSize="20"
        maxActive="50"
        maxIdle="30"
        minIdle="15"
        maxWait="5000"
        testOnBorrow="true"
        testWhileIdle="true"
        validationQuery="SELECT 1"
        timeBetweenEvictionRunsMillis="35000"
        minEvictableIdleTimeMillis="55000"
        removeAbandoned="true"
        removeAbandonedTimeout="3600"
        logAbandoned="true"
        validationInterval="35000"
        closeMethod="close"
    />
</Context>

Remove factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" 删除factory =“org.apache.tomcat.jdbc.pool.DataSourceFactory”

Within the most recent upgrade to apache-tomcat-8.0.28, "factory=..." was added to the DataSource Configuration, then connection sessions to oracle database went crazy. 在最近升级到apache-tomcat-8.0.28的过程中,“factory = ...”被添加到DataSource配置中,然后连接到oracle数据库的会话变得疯狂。 After taking it out, the connection sessions went to the noraml status. 取出后,连接会话进入noraml状态。 I have tried to understand the reason, but I couldn't. 我试图了解原因,但我不能。 This comment might be helpful to someone else. 此评论可能对其他人有帮助。

The following is the datasource config from Apache TomEE http://tomee.apache.org/datasource-config.html . 以下是Apache TomEE http://tomee.apache.org/datasource-config.html的数据源配置。 It should be a good reference for anyone who is looking for a solution. 对于正在寻找解决方案的人来说,它应该是一个很好的参考。

 <Resource id="myDataSource" type="javax.sql.DataSource"> accessToUnderlyingConnectionAllowed = false alternateUsernameAllowed = false connectionProperties = defaultAutoCommit = true defaultReadOnly = definition = ignoreDefaultValues = false initialSize = 0 jdbcDriver = org.hsqldb.jdbcDriver jdbcUrl = jdbc:hsqldb:mem:hsqldb jtaManaged = true maxActive = 20 maxIdle = 20 maxOpenPreparedStatements = 0 maxWaitTime = -1 millisecond minEvictableIdleTime = 30 minutes minIdle = 0 numTestsPerEvictionRun = 3 password = passwordCipher = PlainText poolPreparedStatements = false serviceId = testOnBorrow = true testOnReturn = false testWhileIdle = false timeBetweenEvictionRuns = -1 millisecond userName = sa validationQuery = </Resource> 

如果我们使用hibernate属性文件进行配置,我认为这就足够了:

hibernate.transaction.auto_close_session

I have similar problem but my environment is slightly different. 我有类似的问题,但我的环境略有不同。

I have springboot application and mariadb 10.34 . 我有springboot应用程序和mariadb 10.34

I have defined all the properties in application-*.properties . 我在应用程序中定义了所有属性- *。属性

I have commented(you can remove as well) all the configuration starts with spring.datasource.tomcat 我已经评论过(您也可以删除)所有配置都以spring.datasource.tomcat

and it is working. 它正在发挥作用。

I guess, someone from my team has checked-in without code review. 我想,我的团队中有人在没有代码审查的情况下办理了登机手续。 :) ;) :);)

Reference: https://github.com/HomoEfficio/dev-tips/blob/master/%EA%B0%84%ED%97%90%EC%A0%81%EC%9D%B8%20Too%20many%20connections%20%EC%97%90%EB%9F%AC.md 参考: https//github.com/HomoEfficio/dev-tips/blob/master/%EA%B0%84%ED%97%90%EC%A0%81%EC%9D%B8%20Too%20many%20connections %20%乳油%97%90%EB%9F%AC.md

(If you do not know Korean, then you can use https://translate.google.com/ :D :D ) (如果您不懂韩语,那么您可以使用https://translate.google.com/

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

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