Jdbc connection close close close close close close close

Hi everyone, I knew this was an old question, but just curious today. As you know, connection.close will also close the prepared statement (correct me if I am wrong). but what if I close the connection then close the prepared statement

conn.close();
ps.close();

Will I get a nullpointer exception?

Someone said depends on your jvm speed. Sometimes ps.close () starts ahead and closes first before conn.close finishes its work and therefore you will not get nullpointer.

To check this, I changed the code

conn.close();
Thread.sleep(5000);//I give 5s to conn.close to finish his work. should be enough
ps.close();

But I did not get a null pointer.

So my question is what happened here if I close conn first and then ps.

thank you all.

+5
source share
3

JavaDoc Statement.close() :

close Statement, , .

, , , Connection.close().

+4

javadoc Statement

close
void close()
           throws SQLExceptionReleases this Statement object database and JDBC resources immediately instead of waiting for this to happen when it is automatically closed. It is generally good practice to release resources as soon as you are finished with them to avoid tying up database resources. 
**Calling the method close on a Statement object that is already closed has no effect.** 

, , .

+1

. // . , . Primrose , , close() Connection ( ).

0

All Articles