Recent Pos

Closing JDBC Connection Statement and ResultSet

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail

jdbc close connection statement resultset

Closing the Connection:

⇒ The database Session are limited.

⇒ It is required to explicitly inform the database server about completion of the operation with the given Connection, so that the database server can release the resources (session). This can support allocating the resources for other clients. This improves the scalability of the system.

⇒ In case of JDBC the following method of Connection object is used to close the session:

 
⇒ This method will request to the database server to close the DB session represented by this connection.

Note: This will not nullify the con reference or garbage collect the connection object in the JVM. It will only close the DB session.

⇒ We can use the following method of Connection object to find if the Connection closed:

 
⇒ This returns true in case if the connection is already closed.
 
 

Closing the JDBC Statement:

⇒ The following method of Statement can be used to close the JDBC Statement:

 
⇒ This will close DB resources allocated for the statement.

Note: The statements are implicitly closed when Connection is closed.

Closing the ResultSet:

⇒ The ResultSet object also supports close() method.

⇒ The close() method of ResultSet will release the database Cursor which represented by this ResultSet.

⇒ The ResultSet is implicitly closed in the following cases:

1. When the Connection is closed.

2. When the Statement is closed.

3. When a Statement object is used to execute another SQL.

4. When the ResultSet RowPointer moves to the after last record (Note this is applicable only for FORWARD_ONLY ResultSet).
 
 
⇒ For best practice we can create one utility class which will be responsible for closing the Connection, simply calling the utility class method by passing the Connection, Statement and ResultSet object.

 
⇒ It is not required to pass Connection, Statement and ResultSet object at a time. We can pass any of the object which need to be closed. For example we want to close the Statement object only then we can pass null for Connection and ResultSet.

 
 
<-Previous: Working With JDBC CallableStatement Database Function | | Next->Using JDBC To Access Excel Sheet CSV and Text file

No Comments

Post a Comment