This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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 ?
Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Joined: May 11, 2006
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:
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.