我使用 PreparedStatement
进行选择时遇到异常。
Got an exception accessing TestCase data! null
Problem to connect.
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
Too many connections
这是我的代码:
public Integer getTypeByInputAndProblemId(String inputTestCase, Long problemId) {
String sql = "SELECT type FROM test_case where problem_id= ? and input= ?";
Integer type = 0;
try {
PreparedStatement ps = connection.prepareStatement(sql);
ps.setLong(1, problemId);
ps.setString(2, inputTestCase);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
type = new Integer(rs.getInt("type"));
}
rs.close();
ps.close();
} catch (Exception e) {
System.err.println("Got an exception accessing TestCase data! " + e.getMessage());
}
return type;
}
行PreparedStatement ps = connection.prepareStatement(sql)
;
我的问题是因为连接有时为空(调试显示这一点)。
我猜这是因为有很多联系,但我不知道为什么会发生这种情况。
我需要一些帮助,请!
最佳答案
是的,出现此问题是因为您的服务器已达到 MySQL 服务器接受的最大多个连接数。
首先,您需要查看 MySQL 中是否为多个连接配置了正确的数字:max_connections 。 如果您认为这个值很低,您可以增加此数字以“修复”此问题。
其次,如果这个数字有意义,那么您使用的连接数可能比您想象的要多。 可能是因为您在应用程序中打开连接而不是关闭它们。
检查您的服务器到目前为止使用了多少个多个连接。
show status like 'Max_used_connections';
当您重新启动数据库服务时,此数字将会重置。
关于mysql - 为什么在这种情况下有很多连接(MySQLNonTransientConnectionException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22852403/