0

if i do close to connection database instead if i do close my resultSet ,what happend ? I did write example to under. For example.

Database.java
public class Database{
try{
Connection con;
PreparedStatement statement;
ResultSet resultSet;
public static void main{
       Class.forName("com.mysql.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/database";
        String _username="root";
        String _password = "password";
        Connection db = DriverManager.getConnection(url,_username,_password);
String sqlQuery = "select * from category";        
statement = db.prepareStatement(sqlQuery);
resultSet = statement .executeQuery();
while(resuletSet.next()){

......
}

// i dont close connect.
resultSet.close();
//what happen then it? connection.close() , will it be automatically down ?
}catch(Exception e){
e.printStackTrace();
}
     }
          }
Doktor
  • 73
  • 2
  • 6
  • You may find the behavior explained [here](https://docs.oracle.com/javadb/10.8.3.0/devguide/cdevconcepts839085.html) – user1987 May 11 '20 at 09:38
  • I don't think it's easy for a beginner to perceive @JoakimDanielson – Adrian Lagartera May 11 '20 at 09:42
  • Try checking the answers from [Closing Database Connections in Java](https://stackoverflow.com/questions/2225221/closing-database-connections-in-java/14176504) – Melchizedek May 11 '20 at 09:53
  • You will leak a connection and a socket and all the associated memory at this end, and also at the database end. The database server will probably close the idle connection after a timeout, which solves the problem at its end, but not at your end. – user207421 May 11 '20 at 10:44

1 Answers1

-1

You're right to close your ResultSet carefully. Leaving those objects around after you finish with them is definitely a way to get memory leaks.

You should also close your Connection objects when you no longer need them. If your Java program terminates any Connection objects you still have open are closed, automatically. If no queries are in progress on those objects then the MySQL server closes the connection and cleans up when Java abandons the connection.

Java since version 7 has a way to manage this cleanly. You can do

try (Connection db = DriverManager.getConnection(url,_username,_password)){
    //use the connection object
    ...
   try (ResultSet result = stmt.ExecuteQuery(whatever)) {
       // use the resultset
   } catch (whatever) { whatever }
}
catch(whatever) { whatever }

This is is a nice way to avoid these leaks; the Connection and ResultSet objects get closed automatically at the end of the try / catch block just as if they were closed in a finally{} clause. It's called automatic resource block management.

Yes, close Connections when you're done with them. If you open a lot of Connection objects without closing them, two bad things happen:

  • Less bad: your Java program's RAM will leak.
  • More bad: your MySQL server's connection slots will fill up and it will start rejecting new connections. Client bugs which use up server resources are generally bad bugs.

Failing to close Connections can be pernicious, because typical programs don't use as many of them as ResultSets. So it takes longer to accumulate lots of unclosed connections, and you may not detect the problem while testing. Testers should log into the MySQL server directly and run the SHOW PROCESSLIST; command during system testing to see if unclosed Connections are accumulating.

Close your ResultSets and your Connections.

O. Jones
  • 103,626
  • 17
  • 118
  • 172