简体   繁体   中英

Closing MySQL Database Connections in java

I am having issues with my software becoming slow after some minutes of use. I figured out that the problems could be because I'm having numerous connections which i don't close. I have one java class with my Database connection from which i call the connect function to the various parts of the software that I need to execute queries. I don't know at which point I should close my connections because if I attempt to close after each query I can't have the query redone since I get the error 'Query cant be done after database connection is closed'. When I put it in my DB connection class, immediately after login, I cant execute any query. At what point should I put the db close function? The following is my connection class.

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

I need the queries in various parts of the software as follows.

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

At what point should I put my db connect function.

The code for closing connection should be written in finally block, so that if there is some error while performing database operations, then also the connection gets closed. You can do as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM