使用数据库池的连接错误太多

时间:2014-11-06 00:44:51

标签: java mysql database

所以我正在开发一个应该处理大量负载的Web服务。我使用了snaq DBpooling。但问题是我总是得到错误

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);
    }
}

1 个答案:

答案 0 :(得分:1)

太多客户端同时访问池,或者某些客户端没有回馈其连接。我推测后者。

您可以尝试通过使用小型池(例如5)顺序进行单次调用来测试哪个,并查看第6次调用是否出现问题。这表明客户端没有正确清理。

创建了JDK7 try with resources功能,因为很多人遇到了同样的问题(例如:How should I use try-with-resources with JDBC?)。

我也发现您没有关闭可能与其他问题相关或有助于其他问题的StatementResultset(请参阅Must JDBC Resultsets and Statements be closed separately although the Connection is closed afterwards?)。