java com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:连接太多

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13971460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:36:33  来源:igfitidea点击:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections

javajspjdbc

提问by Hardik Lotiya

My simple web-app sometimes crashes when navigating around the site, the error message I've gotten from the logs are found below. I have absolutely no idea of what is causing this and would be very thankful for any tips that'll lead me in the right direction.

我的简单 web 应用程序有时会在浏览站点时崩溃,我从日志中得到的错误消息如下所示。我完全不知道是什么导致了这种情况,并且非常感谢任何能引导我走向正确方向的提示。

HTTP Status 500 - org.hibernate.exception.JDBCConnectionException: Cannot open connection

type Exception report

输入异常报告

message org.hibernate.exception.JDBCConnectionException: Cannot open connection

description The server encountered an internal error that prevented it from fulfilling this request.

exception
org.apache.jasper.JasperException: org.hibernate.exception.JDBCConnectionException: Cannot open connection
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

根本原因

org.hibernate.exception.JDBCConnectionException: Cannot open connection
    org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97)
    org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
    org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
    org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:449)
    org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
    org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1573)
    org.hibernate.loader.Loader.doQuery(Loader.java:696)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    org.hibernate.loader.Loader.doList(Loader.java:2228)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
    org.hibernate.loader.Loader.list(Loader.java:2120)
    org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
    org.hibernate.impl.SessionImpl.list(SessionImpl.java:1596)
    org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
    com.html.XmlSetting.getListOfhtml_connect(XmlSetting.java:55)
    com.html.XmlSetting.removeXml(XmlSetting.java:121)
    com.html.MarginSetting.removeXml(MarginSetting.java:291)
    org.apache.jsp.check_jsp._jspService(check_jsp.java:183)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

根本原因

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Too many connections
    sun.reflect.GeneratedConstructorAccessor153.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.lang.reflect.Constructor.newInstance(Constructor.java:532)
    com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    com.mysql.jdbc.Util.getInstance(Util.java:381)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:985)
    com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3376)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3308)
    com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:894)
    com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3808)
    com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1256)
    com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2032)
    com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:729)
    com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    sun.reflect.GeneratedConstructorAccessor145.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    java.lang.reflect.Constructor.newInstance(Constructor.java:532)
    com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
    com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:283)
    java.sql.DriverManager.getConnection(DriverManager.java:620)
    java.sql.DriverManager.getConnection(DriverManager.java:169)
    org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:133)
    org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:446)
    org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:167)
    org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:161)
    org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1573)
    org.hibernate.loader.Loader.doQuery(Loader.java:696)
    org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    org.hibernate.loader.Loader.doList(Loader.java:2228)
    org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
    org.hibernate.loader.Loader.list(Loader.java:2120)
    org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:118)
    org.hibernate.impl.SessionImpl.list(SessionImpl.java:1596)
    org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
    com.html.XmlSetting.getListOfhtml_connect(XmlSetting.java:55)
    com.html.XmlSetting.removeXml(XmlSetting.java:121)
    com.html.MarginSetting.removeXml(MarginSetting.java:291)
    org.apache.jsp.check_jsp._jspService(check_jsp.java:183)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

I m using Apache Tomcat 7

我正在使用 Apache Tomcat 7

hibernate.cfg.xml

休眠文件.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>

        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/htmlcleaner</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.username">root</property>

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.zeroDateTimeBehavior">convertToNull</property>

        <property name="connection.pool_size">1000</property>

       <!--  <property name="show_sql">true</property> -->
        <mapping class="com.model.html_connect"/>

    </session-factory>
</hibernate-configuration>

采纳答案by JustDanyul

It looks like you are leaking connections big time. Are you closing your hibernate sessions when you are done with them?

看起来您正在大量泄漏连接。当您完成它们时,您是否关闭您的休眠会话?

You could also try and fiddle about with hibernate.connection.release_mode, by default, this is set to auto and it will release the connection when you close the session. However, you also have the options here to use after_transactionor after_statement(its rather self explanatory when it will release the connections for each of the two :P).

你也可以试着摆弄一下hibernate.connection.release_mode,默认情况下,它被设置为自动,当你关闭会话时它会释放连接。但是,您也可以在此处选择使用after_transactionor after_statement(当它释放两者中的每一个的连接时,它相当不言自明:P)。

With that said though, you do need to find out where you leaks are :)

尽管如此,您确实需要找出泄漏的位置:)

回答by Rais Alam

Its clearly seen hibernate is not able to create or open connection with database there could be many reason some them are

它清楚地看到休眠无法创建或打开与数据库的连接,可能有很多原因

  1. Your not closing session in your program after using session
  2. Connection pool is small and too many threads are accessing the pool.
  1. 使用 session 后你没有在你的程序中关闭 session
  2. 连接池很小,访问连接池的线程太多。

Kindly paste your configuration and exception point program

请粘贴您的配置和异常点程序

Add

添加

<property name="hibernate.connection.release_mode">on_close</property>

property and call session.close(); at the end of your method

属性并调用 session.close(); 在你的方法结束时

回答by AngelsandDemons

The possible reason for this is that your web application is possibly leaking connections or the connection pool size is very small due to which all the open connections are currently being used and the new connection cannot be obtained.

造成这种情况的可能原因是您的 Web 应用程序可能存在连接泄漏或连接池大小非常小导致当前所有打开的连接都被使用而无法获取新连接。

I am not sure about Tomcat but in JBoss, we generally refer to datasource file and you can set the connection pool size as

我不确定 Tomcat 但在 JBoss 中,我们通常参考数据源文件,您可以将连接池大小设置为

   <max-pool-size>80</max-pool-size>

You can even log into the administrative console of Tomcat and check how many connections are actually in use whenever you get this exception. The administrative console will give you the maximum connection objects ever created and a variety of other information related to connection pool.

您甚至可以登录 Tomcat 的管理控制台,并在遇到此异常时检查实际使用的连接数。管理控制台将为您提供有史以来创建的最大连接对象以及与连接池相关的各种其他信息。