Search code examples
javasessionconnectionhttpsession

Does it make sense to set session=null after session.close()?


consider this bit of code:

public void foo(){
       try{
        //some cool statements here
      }
      finally {
         if(session != null){
             session.close();
             sesssion = null;
          }
     }
   }

Since the session is closed, is the value assigned to session going to make sense? Please provide some valuable insights.


Solution

  • Setting local variable to null is unnecessary, to wit, it's part of CERT Oracle Secure Coding Standard's Recommendations

    Setting local reference variables to null to "help the garbage collector" is unnecessary. It adds clutter to the code and can make maintenance difficult. Java just-in-time compilers (JITs) can perform an equivalent liveness analysis, and most implementations do so.