Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between close & null with regards to connection

 
Ashutosh Limaye
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume
Connection con=DriverManager.getConnection("Some Driver URL");

what is the difference between saying
con.close();
and
con=null;
 
Maximilian Xavier Stocker
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kj Reddy
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
suc jag
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am agreed with Maximilian Xavier Stocker, First one is good for applying as second one will keep the connection open.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic