java MySQLNonTransientConnectionException:数据源拒绝建立连接,来自服务器的消息:“连接太多”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28321182/
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
MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
提问by Predrag Maric
I have connection leak problem in my application , i see that number of connections creep up as some hours (requests ) pass by finally they are all consumed and we get , we are using JDBC with defualt tomcat 7 and default mysql 5.0
我的应用程序中有连接泄漏问题,我看到连接数随着几个小时(请求)过去而逐渐增加,最终它们都被消耗掉了,我们得到了,我们正在使用带有默认 tomcat 7 和默认 mysql 5.0 的 JDBC
Data source rejected establishment of connection, message from server: "Too many connections"
数据源拒绝建立连接,来自服务器的消息:“连接太多”
This is the message we see under our server
这是我们在服务器下看到的消息
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
at sun.reflect.GeneratedConstructorAccessor42.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1014)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1110)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.GeneratedConstructorAccessor30.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
And we are sure that we are closing all the DB Connections inside the java files
我们确定我们正在关闭 java 文件中的所有数据库连接
Utility class
实用类
public class DBConnection {
public static Connection getDBConnection() {
String sURL="jdbc:mysql://localhost:3306/sss";
//String sURL="jdbc:mysql://192.xxx.2.s:3306/sss";
String sUserName="root";
String sPwd="";
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(sURL, sUserName,sPwd);
return conn;
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection con)
{
if (con != null)
{
try
{
con.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
}
This is how i access the connection
这就是我访问连接的方式
public String adminLogin()
{
Connection dbConnection = null;
dbConnection = DBConnection.getDBConnection();
try
{
}
catch()
{
}
finally
{
DBConnection.close(dbConnection);
}
}
Could anybody plese let me know how to resolve this ??
有人可以让我知道如何解决这个问题吗??
回答by K M PATEL
it's crucial problem .... you should investigate this problem....
这是至关重要的问题......你应该调查这个问题......
you can take log of which method is creating new connections by putting below code in your create connection method
您可以通过在创建连接方法中放置以下代码来记录哪种方法正在创建新连接
StackTraceElement[] st = Thread.currentThread().getStackTrace();
System.out.println( "create connection called from " + st[2] );
It may possible some code is creating new connections in loop or your process is taking much time that before closing some old connection new connection is created and total connections increases..
可能某些代码正在循环中创建新连接,或者您的进程需要很长时间才能关闭一些旧连接,新连接被创建并且总连接数增加。
in mysql workbench you can check total number of connection by this way click here
在 mysql 工作台中,您可以通过这种方式检查连接总数, 请单击此处
回答by Predrag Maric
MySQL has a confugurable maximum number of connections allowed, which is 100 (or 151, dependinng on version). Make sure you didn't configure a connection pool with a larger number of connections.
MySQL 有一个可配置的最大允许连接数,即 100(或 151,取决于版本)。确保您没有配置具有大量连接的连接池。