Java 使用数据库池时出现连接过多错误
因此,我正在开发一个web服务,它应该能够处理大量的负载。我使用snaq数据库池。但问题是我总是会出错Java 使用数据库池时出现连接过多错误,java,mysql,database,Java,Mysql,Database,因此,我正在开发一个web服务,它应该能够处理大量的负载。我使用snaq数据库池。但问题是我总是会出错 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections" 我已经检查了其他线程,主要问题是con
com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected
establishment of connection, message from server: "Too many connections"
我已经检查了其他线程,主要问题是conn.close调用不正确。但是在这里,我在finally方法中调用它,正如到处建议的那样。这是我的密码。你能告诉我怎么了吗
public class ExampleServlet extends HttpServlet {
public static final String MESSAGE = "message";
private String message;
private ConnectionPool pool;
@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
message = config.getInitParameter(MESSAGE);
try {
Class c = Class.forName("com.mysql.jdbc.Driver");
Driver driver = (Driver) c.newInstance();
DriverManager.registerDriver(driver);
String url = "jdbc:mysql://localhost/db15619";
pool = new ConnectionPool("local", 20, 25, 30, 180, url, "root", "db15319root");
} catch (/*InstantiationException | ClassNotFoundException | SQLException */ Exception ex) {
System.out.println("Error:" + ex.toString());
}
}
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
//System.out.println(req.getQueryString());
String[] string_key = req.getQueryString().split("=|\\&");
//exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
string_key[3] = string_key[3].replace('+', ' ');
String query_str = "select tweetid,score,tweettext from tweets where userid = '" + string_key[1] + "' and dttm = '" + string_key[3] + "';";
Connection conn = null;
long timeout = 10000;
try {
conn = pool.getConnection();
} catch (SQLException ex) {
System.out.println("Error While Creating Connection :=> " + ex.toString());
}
if (conn != null) {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(query_str);
String str = "CloudNinjas," + "0369-8371-3735" + "," + "9830-4777-6269" + "," + "3472-5239-0207" + "\n";
while (rs.next()) {
for (int i = 1; i <= 3; i++) {
str = str + rs.getString(i);
if (i != 3) {
str = str + ":";
} else {
str = str + "\n";
}
}
}
byte[] bytes = str.getBytes(Charset.forName("UTF-8"));
System.out.println(str);
System.out.println(bytes);
writer.write(str);
} catch (SQLException ex) {
System.out.println("Error While Creating Connection :=> " + ex.toString());
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
}
}
}
}
writer.close();
}
@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
要么太多客户端同时访问池,要么一些客户端没有返回连接。我想是后者吧 您可以尝试通过使用一个小池(比如5个)连续进行单个调用,并查看问题是否发生在第6次调用中来测试。这将表明客户端没有正确清理 创建JDK7 try with resources功能是因为有太多人有相同的问题,例如:
我还看到您没有关闭语句或结果集,请查看哪些可能与其他问题相关或导致其他问题。已投票,因为您提醒OQ关闭语句和结果集对象。在可以gced结果集之前,连接不会断开。如果您使用服务器端游标对象连接到dbms,如果未能发布结果集,可能会导致dbms崩溃。使用了您给出的建议,但仍然无效。如果我只使用两个连接运行一个查询,那么即使我手动查询100次,它也可以正常运行。是不是负载太大,无法承受。因为它运行时会创建40个连接并运行大约3秒钟,然后我收到一个读取超时错误,很可能是因为您的池已耗尽,因为有太多的客户端无法提供服务。您可以尝试增加池大小,但我也会看看为什么有这么多长时间运行的查询。请确保在发布大多数IDE时,代码的格式正确