【问题标题】:Closing MySQL Database Connections in java在java中关闭MySQL数据库连接
【发布时间】:2020-09-16 15:49:33
【问题描述】:

我的软件在使用几分钟后变慢了。我发现问题可能是因为我有许多我没有关闭的连接。我有一个带有数据库连接的 java 类,我从该类中调用连接函数到我需要执行查询的软件的各个部分。我不知道应该在什么时候关闭我的连接,因为如果我在每次查询后尝试关闭,我将无法重做查询,因为我收到错误“数据库连接关闭后无法完成查询”。当我将它放在我的数据库连接类中时,登录后立即无法执行任何查询。我应该在什么时候放置 db close 函数? 以下是我的连接类。

public class databaseConnection {
   public static Connection connection(){
       Connection con = null;
       try{
           //test server
          //con = DriverManager.getConnection("jdbc:mysql://194.5.156.94:3306/u843360242_tukule?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","user","password");
          //real server 
        con = DriverManager.getConnection("jdbc:mysql://254gamers2.softether.net:3306/tukule_kwanza?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","user","password");
       //localhost server 
         //con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tukule?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","password");
       }catch (SQLException ex) {
            try{
           //test server
          //con = DriverManager.getConnection("jdbc:mysql://194.5.156.94:3306/u843360242_tukule?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","user","password");
          //real server 
          con = DriverManager.getConnection("jdbc:mysql://kwanzatukule.ddns.net:3306/tukule_kwanza?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","user","password");
       }catch (SQLException x) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        }
       return con;
   } 
}

我需要软件各个部分的查询如下。

public ArrayList categoriesQuery() {
       String query2 = "SELECT * FROM category";
        ArrayList categories = new ArrayList();
    try {
            pst = connect.prepareStatement(query2);
            rs=pst.executeQuery();
            while(rs.next()){
                Object o[]={rs.getInt("id"),
                            rs.getString("Category_Name")
                            };
                categories.add(o);         
            }
          } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    return categories;
    }

我应该在什么时候放置我的 db connect 函数。

【问题讨论】:

  • 如果您使用的是连接池,您应该在完成后立即关闭连接。如果您不使用连接池,则需要知道创建连接很慢(因此您可能至少不应该经常关闭它)。这里有数百个数据库问题。我建议您搜索并阅读这些内容以更好地理解。

标签: java mysql dbconnection


【解决方案1】:

关闭连接的代码应该写在 finally 块中,这样如果在执行数据库操作时出现错误,连接也会被关闭。你可以这样做:

public ArrayList categoriesQuery() {
       //your code
       Connection connect=null;
    try {
            connect = databaseConnection.connection();
            if(connect!=null){   
                //your code for database operations      
            }
          } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally{
            if(connect!=null){
                connect.close();
            }
        }
    return categories;
}

【讨论】:

  • 非常感谢@Ankit。它奏效了。它在使用后关闭连接。虽然我注意到这样做会以某种方式降低应用程序的速度。有什么解决办法吗?
  • 如果一个连接被打开,那么它必须尽快关闭。您可以使用相同的连接来执行多个数据库操作,即您可以在 if(connect!=null){ //your code for database operations } 块中调用多个函数并将连接变量作为参数传递。这样您就不必一次又一次地打开和关闭连接,并且速度会得到提高。
猜你喜欢
  • 2012-07-12
  • 2011-01-14
  • 1970-01-01
  • 2010-09-24
  • 2014-07-01
  • 2013-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多