I am wondering, if I have some sort of Connection or Stream in my code that should be closed to release ressources, what does it mean for the connection instance itself?
Code example:
CustomConnection connection;
try{
connection = //some code that opens a connection
//some code using the connection
}catch(IOException){
//Some logging and Handling of IOExceptions
}finally{
//resources cleanup
try{
connection.close();
}catch(IOException){
//Some Logging
//What else should be done here?
//Is my Connection closed at this point and can I just move on?
//Or should there be anything else done here
//to ensure that the connection is actually closed?
}
}
For example if I have an opened TCP-Connection to let's say an SQL-Server and I am unable to close it because the server has crushed or my device can't reach the device anymore. I would obviously get an IO or in this case an SQLException. If so:
- Should I consider the resource or socket to actually have benn released?
- Will the JVM or the OS handle it overtime? (the OS would probablly do that eventually but it's about what is actually a good programming practice in this case)
- Should I handle the issue myself by trying
Edit 1: I am aware of the "try with resources" construct. I only just wonder how to handle a connection closing if the Connection I am using does not implement AutoCloseable.