• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

why not set connection=null after close connection

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,

I have seen all examples about database conection like the following.

Connection cn = DriverManager.getConnection("jdbc:hsqldb:file:test5",
"sa", "");
Statement st = cn.createStatement();
ResultSet ts = null;
......
.....
.....
st.close();
ts.close();
cn.close();


after connection is closed , why not set cn = null?
what is disadvantage to set cn=null after cn is closed?

Thank you very much for your help.

Chaohua
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is generally better to let the variable fall out of scope, and the garbage collector will take care of it.
 
chaohua wang
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tnank you,

I wonder if not set connection = null after close, next thread can use this connection again.

I got an exception in my code:
Thread-579 I/O Error: Connection reset
net.sourceforge.jtds.jdbc.TdsCore:TdsCore.java Line: 1049
net.sourceforge.jtds.jdbc.JtdsStatement:JtdsStatement.java Line: 406
net.sourceforge.jtds.jdbc.JtdsStatement:JtdsStatement.java Line: 1258

I guess the reason is that I set connnection=null after close the connection. please help.
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In most instances, threads should not be sharing connections anyways. From the little information you presented here, it sounds to me like you have multi-threading issues going on.
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do you want to set it to null ? why do you think this could give you more problems ?

setting connection to null will not give you any problems (other than NullPointerException if you try to call a method on the null reference).

A reference is just a "pointer" to an Object. If you set this pointer to null, then this does not affect the Connection other than it might be garbage collected if this was the last pointer to the Object (of course there is more to garbage collection than this...).

if your connection has been resetted/closed then this sounds like a thread/timeout/pool problem.
and by the way:
always use try/catch/finally or try/finally to close connections/statements/resultsets/other resources/streams or to commit/rollback transactions... the code you've showed is not a god example!


cheers

p
 
Everybody's invited. Except this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic