错误:数据源拒绝建立连接,来自服务器的消息:“连接太多” [英] Error: Data source rejected establishment of connection, message from server: "Too many connections"

查看:172
本文介绍了错误:数据源拒绝建立连接,来自服务器的消息:“连接太多”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个每5分钟将数据写入数据库的应用程序。

I created an application that writes data to the database every 5 minutes.

但是一段时间后会出现此错误:

However after some time this error appears:

错误:数据源被拒绝建立连接,来自服务器的消息:连接太多

我一直在搜索并告诉您关闭与数据库的连接在每个请求方之后。

I've been searching around and tells you to close the connection to the database after every request side.

我试过这个:

conexao.close();

但它给了我这个错误:

关闭连接后不允许任何操作。

如果问题没有很好地表达,我道歉。

I apologize if the question is not well formulated.

感谢您的帮助

---------------- -----我尝试但没有奏效---------------------------

---------------------What I tried but didn't work---------------------------

添加

finally{ 
    if(conexao!=null)
   conexao.close();
   }







  Class.forName("com.mysql.jdbc.Driver");
        Connection conexao = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/bdTeste", "root", "root");
        Statement stm = conexao.createStatement();
        BufferedReader reader = new BufferedReader(new FileReader("C:/Users/RPR1BRG/Desktop/test.txt"));

        String dados[] = new String[6];
        String linha = reader.readLine();

        while (linha != null) {

            StringTokenizer st = new StringTokenizer(linha, ";\"");

            dados[0] = st.nextToken();
            dados[1] = st.nextToken(); 
            dados[2] = st.nextToken();
            dados[3] = st.nextToken();
            dados[4] = st.nextToken();
            dados[5] = st.nextToken();

             DateFormat dateFormat = new SimpleDateFormat("d-M-yy");

    PreparedStatement stmt = (PreparedStatement) conexao.prepareStatement("replace into registos"
    + " (data_registo, hora_registo, IdSensor, Temperatura, Humidade, pt_orvalho) values (?,?,?,?,?,?)");
                    try {
                        stmt.setDate(1, new java.sql.Date(dateFormat.parse(dados[0]).getTime()));
                        stmt.setString(2, dados[1]);
                        stmt.setString(3, dados[2]);
                        stmt.setString(4, dados[3]);
                        stmt.setString(5, dados[4]);
                        stmt.setString(6, dados[5]);

                    } catch (java.text.ParseException ex) {
                        Exceptions.printStackTrace(ex);
                    }


stmt.executeUpdate();

            linha = reader.readLine();
            PrintWriter writer = new PrintWriter("C:/Users/RPR1BRG/Desktop/test.txt"); 
            writer.print("");
            writer.close();
            Verifica();
            }

    } catch (ClassNotFoundException | SQLException | IOException e) {

        System.err.println("Erro: " + e.getMessage());

    }finally{ 
    if(conexao!=null)
   conexao.close();
   }


推荐答案

这种问题出现时您 NOT 在使用后正确关闭连接。

This kind of problem arises when you are NOT properly closing the connection after usage.


请在 catch finally 阻止c $ c>适当地关闭连接。这是因为即使出现意外异常或错误,也要确保连接正确关闭。请注意, finally 块中的语句总是会被执行。它允许程序员避免因返回,继续或中断而意外绕过清理代码

Please use finally block after catch to close the connections appropriately. This is because to ensure that the connection gets closed properly even when there is an unexpected exception or error. Please note that statements inside finally block gets executed always. it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break

注意:如果JVM退出时尝试或捕获代码正在执行,然后finally块可能不会执行。同样,如果执行try或catch代码的线程被中断或终止,则即使整个应用程序继续执行,finally块也可能无法执行。

Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.

如您所问在评论中,我添加了代码示例以进行实际演示!

As you have asked in comment, I have added the code sample to demonstrate practically!

Connection con = null
try{
 //Establishing connection to datasource
 con = DBConnection.getConnection();
 //perform DB operations
 ...
 ...
 ...
}catch(SQLException sqlEx){
 /*To catch any SQLException thrown during DB 
  *Operations and continue processing like sending alert to admin
  *that exception occurred.
  */
}finally{
 /*This block should be added to your code
  * You need to release the resources like connections
  */
 if(con!=null)
  con.close();
}

请注意 Connection 变量应该在适当的范围内,在最后块中关闭它。

Please note that the declaration of Connection variable should be in proper scope to close it in finally block.

希望这会有所帮助!

这篇关于错误:数据源拒绝建立连接,来自服务器的消息:“连接太多”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆