簡體   English   中英

使用數據庫池的連接錯誤太多

[英]Too many connections error using DB pooling

因此,我正在開發應該處理大量負載的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);
    }
}

太多的客戶端同時訪問該池,或者某些客戶端沒有退還其連接。 我認為是后者。

您可以嘗試通過使用一個較小的存儲池(例如5)順序進行單個呼叫並查看問題是否在第6個呼叫上發生來進行測試。 這表明客戶端沒有正確清理。

之所以創建JDK7資源嘗試功能,是因為有很多人遇到相同的問題(例如: 我應該如何在JDBC中使用try-with-resources? )。

我還看到您沒有關閉StatementResultset (請參閱JDBC Resultsset和Statements是否必須先關閉,盡管Connection隨后會關閉嗎? ),這可能是相關的或導致了其他問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM