3

Is it required/good practice to set the connection reference to null after closing it?

I am closing connections in finally block.

conn.close();
conn = null;

I am facing connection wait timeout exception during performance testing as the maximum connections are getting exceeded.

Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
androidDev
  • 1,179
  • 4
  • 13
  • 31
  • 3
    Is this a local variable? A field? What do you *hope* to achieve here? – Jon Skeet Aug 05 '15 at 11:15
  • 1
    Maybe this helps you to identify what happend if i set an object to null: [Link](http://stackoverflow.com/questions/18009909/clearing-or-set-null-to-objects-in-java) – MrT Aug 05 '15 at 11:17
  • @Jon ..I m trying to find if there is any connection leakage in my code .. although all connections are being closed....at one point application run out of connections...I am using IBM Was 7 – androidDev Aug 05 '15 at 11:21
  • 2
    The Garbage Collector is going to removed it, don't need to put a `null` reference after you closed it. – Murat Karagöz Aug 05 '15 at 11:22
  • If I set conn to null..Will this close the connection held by it or it will remain open....according to me that should get closed – androidDev Aug 06 '15 at 07:51

1 Answers1

0

You'll see this pattern in use when people want to explicitly throw away a reference, and thus aid garbage collection, and trigger finalisers. This is virtually always redundant.

If the conn variable is used to indicate that a new connection is required (by making it a field, perhaps, and resetting it to null before later reference) then that's a different pattern, but unrelated to immediate resource management.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440