1

I have a class which takes a database connection over constructor, as you can see from the sample code connCat = con_cat (con_cat is being passed in).

Please let me know: do I need to explicitly close the connCat connection inside this class, so the connection will be released properly?

If it's missing, will it cause connection leakage?

Will having a finally clause and doing connCat.close(); and connCat = null; be enough?

public class GenericDbProc extends DBProcessor {
Logger fileLogger = Logger.getLogger(GenericDbProc.class);
Connection connCat;

public GenericDbProc(Connection con_cat)
{
    connCat = con_cat;
}

public List<MxsCustomParam> getServiceEndPointDetails(String module_name) {
    StringBuffer sb = new StringBuffer();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    
    sb.append("select xxxxxxx ")
            .append(" where module = ?");
    List<MxsCustomParam> mxsCustomParamList = new ArrayList<MxsCustomParam>();

    try {
        stmt = connCat.prepareStatement(sb.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        stmt.setString(1, module_name);
        
        rs = stmt.executeQuery();
Marek Puchalski
  • 3,286
  • 2
  • 26
  • 35
KeanHo Leong
  • 67
  • 12

1 Answers1

0

You get the connection passed over the constructor from the outside. I'd say that closing the connection should not be managed here, but on the outside too.

And yes, the connection should be closed once it is not needed any more, as it holds a socket open in the background and there is a limit to how many connections you can open. If you simply release the reference to this object, then finalize should be called and close the connection at a point of time automaticaly, but it is considered bad practice to rely on this behaviour.

And yes, connections are closed in a finally block by calling the close method, but since Java 7 there are better ways to do that. Please look at this: Closing Database Connections in Java

Marek Puchalski
  • 3,286
  • 2
  • 26
  • 35
  • thanks for the response! so by passing in a connection over the constructor. if i explicitly close the connection within this class, i.e. closing connCat in this class. the actual connection that was being passed in will not be closed right? cause i am just closing a reference to this connection. because on the outside of this class, i still need this connection to be used in subsequent sections. – KeanHo Leong Jul 08 '20 at 09:57
  • 2
    @KeanHoLeong There's no "closing a reference to this connection" only "closing a connection". If you close a connection, it's closed, not only inside the method where you did it, but completely - meaning that all references to this connection will see a closed connection and get an exception when trying to use it. So, if you still need the connection after your method call, don't close it. Rule of thumb: close a connection in the method where you created it. Methods that get a connection as an argument don't close it. – Ralf Kleberhoff Jul 08 '20 at 10:44