我在我的Web应用程序中使用Tomcat JDBC池作为MySQL数据库连接池。 JDBC池在应用程序的context.xml中声明为资源(不是在全局级别)。我正在Eclipse IDE中开发这个Web应用程序。因此,每当我对代码进行更改时,eclipse都会使Tomcat服务器重新加载上下文。
但问题是,当重新加载上下文时,tomcat会创建具有新连接的JDBC池而不释放旧池。在对代码进行了几次更改之后,tomcat最终耗尽了MySQL服务器的最大连接限制,并且tomcat开始在上下文重新加载时显示“Too many connections”错误。
我的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>
我的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就像:
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");
}
}
我得到的例外情况如下:
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
更多信息:
这些都是在没有测试应用程序的情况下发生的,也就是说,我不是执行Web应用程序来打开连接,仍然只是编写代码,因此不会打开连接并保持活着而不关闭它
使用Apache Tomcat:8.0版。和Eclipse Luna Release。
请帮我解决这个问题。
修改
每次上下文重新加载时,我都会在MySQL Workbench中看到更多15个与DB打开的连接,然后达到与MySQL服务器的151个连接的最大连接限制。
答案 0 :(得分:10)
我认为你必须在context.xml中为你的资源添加closeMethod =“close”。然后,当您重新加载上下文时,Tomcat应该关闭您的数据库连接。
修改后的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>
答案 1 :(得分:5)
删除factory =“org.apache.tomcat.jdbc.pool.DataSourceFactory”
在最近升级到apache-tomcat-8.0.28的过程中,“factory = ...”被添加到DataSource配置中,然后与oracle数据库的连接会话变得疯狂。取出后,连接会话进入noraml状态。我试图了解原因,但我不能。此评论可能对其他人有帮助。
以下是 Apache TomEE 的数据源配置 http://tomee.apache.org/datasource-config.html。对于正在寻找解决方案的人来说,它应该是一个很好的参考。
<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>
答案 2 :(得分:1)
如果我们使用hibernate属性文件进行配置,我认为这就足够了:
hibernate.transaction.auto_close_session
答案 3 :(得分:0)
我有类似的问题,但是我的环境略有不同。
我有 springboot 应用程序和 mariadb 10.34 。
我已经在 application-*。properties 中定义了所有属性。
我已评论(也可以删除)所有配置以jQuery(document).ready(function($){
var image = new Array ();
image[0] = "/08/anek-ferry-traghetti-grecia.mp4";
image[1] = "/12/Composizione-2.mp4";
image[2] = "/11/Composizione-1.mp4";
var hasard = Math.floor(Math.random() * image.length);
$('#fond-aleatoire video').attr('src', 'http://neos.anekitalia.com/wp-
content/uploads/2018' + image[hasard]);
});
它正在工作。
我想,我团队中的某人已签入,而没有代码审查。 :);)
(如果您不懂韩语,则可以使用<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id class="fond-aleatoire">
<mediaelementwrapper id="mejs_8947924178991862">
<video loop="loop" autoplay="" playsinline="" muted="" width="1920"
height="600" src="http://neos.anekitalia.com/wp-
content/uploads/2018/11/Composizione-1.mp4"
id="mejs_8947924178991862_html5"
preload="none" style="margin: 0px; width: 1903px; height: 594.688px;">
<source type="video/mp4" src="http://neos.anekitalia.com/wp-
content/uploads/2018/11/Composizione-1.mp4">
</video>
</mediaelementwrapper>
</div>
:D:D)