It's not a secret anymore!*
The moose likes JDBC and the fly likes Difference between close & null with regards to connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Difference between close & null with regards to connection" Watch "Difference between close & null with regards to connection" New topic
Author

Difference between close & null with regards to connection

Ashutosh Limaye
Ranch Hand

Joined: Oct 24, 2005
Posts: 58
Assume
Connection con=DriverManager.getConnection("Some Driver URL");

what is the difference between saying
con.close();
and
con=null;
Maximilian Xavier Stocker
Ranch Hand

Joined: Sep 20, 2005
Posts: 381
Originally posted by Ashutosh Limaye:
Assume
Connection con=DriverManager.getConnection("Some Driver URL");

what is the difference between saying
con.close();
and
con=null;


The first one is a good way to do it and the second one is a bad way to do it.

Yes. The second will cause the resources for at least the connection object in YOUR program to be garbage collected. BUT this does NOT release resources on the Database server side of things. When you have a program that uses JDBC you are using resources not only in your program but on a database server and it is your responsibility to make a best effort not leak resources or abuse the database server.

Setting database objects to null instead of using the proper close methods is horrible, wrong and you should NEVER ever do it. Never.
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
The first one will cause to close the connection and con object is ready to pick by garbage collector.

The second one will make con object is ready to pic by gabage collector but still the connection is open with dabase server mean database resources are still blocked unnecessarily which is wasting the resources.

So never never do the second way. If you are using connection pooling you use different approach.
Hemanth Pallavajula
Ranch Hand

Joined: Oct 07, 2004
Posts: 38
Hi Reddy,

I believe, we use the first approach (con.close()) while closing connections obtained from Connection Pool (Implemented via Data Sources in Weblogic).

Can you please give us the other approaches for closing connection obtained via the Connection Pool.
[ March 15, 2006: Message edited by: Hemanth Pallavajula ]

Cheers,<br />Hemanth...<br />(When opportunity doesn't knock, build a door.)
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

Originally posted by Hemanth Pallavajula:
Hi Reddy,

I believe, we use the first approach (con.close()) while closing connections obtained from Connection Pool (Implemented via Data Sources in Weblogic).

Can you please give us the other approaches for closing connection obtained via the Connection Pool.

[ March 15, 2006: Message edited by: Hemanth Pallavajula ]


Whether the connection is from connection pool or got directlt from DriverManager there is only one way to close the connection - Use the close method and in a finally block.


Groovy
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Originally posted by Hemanth Pallavajula:
Hi Reddy,

I believe, we use the first approach (con.close()) while closing connections obtained from Connection Pool (Implemented via Data Sources in Weblogic).

Can you please give us the other approaches for closing connection obtained via the Connection Pool.

[ March 15, 2006: Message edited by: Hemanth Pallavajula ]


If you get connection object from connetion pool, once you are done with connection you will give it back to connection pool and connection pool will take care of closing the connection.

As Pradip said for closing connection close method is the only better option.
[ March 16, 2006: Message edited by: KJ Reddy ]
suc jag
Greenhorn

Joined: May 11, 2006
Posts: 2
I have read some where that we need to close the connection pool as well. Where to close connection pool?? can any body help me??
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

Originally posted by suc jag:
I have read some where that we need to close the connection pool as well. Where to close connection pool?? can any body help me??


First you tell us where have you read this ?? and What exactly have you read ?

Shailesh


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
suc jag
Greenhorn

Joined: May 11, 2006
Posts: 2
Hi,
Here is the content from struts api on connection pooling. Doubt on opening and closing the dataSource...
-------------------------------------------------------------
To configure a GenericDataSource instance, you must first create one:

GenericDataSource dataSource =
new GenericDataSource();

Next, you must set the appropriate properties, by calling the corresponding JavaBeans property setter methods provided by this class. (See the Javadoc API for the GenericDataSource class for more details on the available properties). An example of configuring the connection pool object to a Postgres database might look like this:

dataSource.setAutoCommit(false);
dataSource.setDescription("My Database Connection Pool");
dataSource.setDriverClass("org.postgresql.Driver");
dataSource.setMaxCount(4);
dataSource.setMinCount(1);
dataSource.setPassword("mypassword");
dataSource.setUrl("jdbc ostgresql://localhost/mydatabase");
dataSource.setUser("myusername");

Finally, you must open() the connection pool. This will establish the initial connections to the database (based on the value you have configured for the minCount property). As you use connections from the pool in multiple threads, additional connections (up to the number you specify with the maxCount property) will be created as needed.

try {
dataSource.open();
} catch (SQLException e) {
... deal with exception ...
}

When you are completely through with the connection pool, you can gracefully close all of the currently open database connections by executing

try {
dataSource.close();
} catch (SQLException e) {
... deal with exception ...
}
--------------------------------------------------
Ekhlas Jewel
Greenhorn

Joined: Jun 30, 2012
Posts: 10
I am agreed with Maximilian Xavier Stocker, First one is good for applying as second one will keep the connection open.
 
Don't get me started about those stupid light bulbs.
 
subject: Difference between close & null with regards to connection