File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Query regarditng Connection Pooling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Query regarditng Connection Pooling" Watch "Query regarditng Connection Pooling" New topic
Author

Query regarditng Connection Pooling

RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
If i didn't close the Connection (didn't perform con.close or con==null)

After the request completes :

will this Connection instance will return back to the Connection pool or will this result in a Memory Leak ?

( I am using DataSource support from Weblogic)


If you want something you never had do something which you had never done
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

If you do not close your connection, this will result in connection leak.

conn == null will not result in closing a connection. You merely check if variable conn h is null or not.


OCUP UML fundamental and ITIL foundation
youtube channel
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Jann , Thanks for the reply .

what i was expecting is that DBCP from Apache has the ability to close the connection for useven if we didn't close .

Did other containers like weblogic provided this facilty ?
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

No. You have to return the connection to the pool, by calling it's close(). Also for other ConnectionPool implementations.
The mechanism is:

You ask a connection from the pool.
You use the connection to perform one or more sql activities
You return the connection to the pool by closing the connection.

Regards, Jan
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30752
    
156

RaviNada Kiran wrote:what i was expecting is that DBCP from Apache has the ability to close the connection for useven if we didn't close .

Most datasource providers will close the connection eventually if you don't. Relying on this still causes a connection leak because it doesn't happen right away.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Relying on this still causes a connection leak because it doesn't happen right away


Can you please tell what do you mean by "it doesn't happen right away"

Thanks in advance .
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

RaviNada Kiran wrote:Can you please tell what do you mean by "it doesn't happen right away"Thanks in advance .
It doesn't happen when you are finished with the connection. It *might* happen when the pool decides to do so.

The hint that we are all trying to give you is: don't rely on it. Close your connections properly.
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Good point Jan .

can you please tell me what are the conditions on which the the container decides to return the Connection instance to the pool .

Thanks in advance.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30752
    
156

RaviNada Kiran wrote:can you please tell me what are the conditions on which the the container decides to return the Connection instance to the pool .

They aren't specified and might not be deterministic. I'm having trouble picturing why you need to know this. If you always close your connections, this is never a problem.
ken zhu
Greenhorn

Joined: Apr 09, 2009
Posts: 20
Jeanne Boyarsky wrote:
RaviNada Kiran wrote:can you please tell me what are the conditions on which the the container decides to return the Connection instance to the pool .

They aren't specified and might not be deterministic. I'm having trouble picturing why you need to know this. If you always close your connections, this is never a problem.

yes, anyway connection pool program have a mechanism to get the connection when timeout or unuseful connection check triggered.
but close it explicitly is good custom.

Actually, i have met the issue, my environment is oralce9i+jdk1.4+ojdbc14.jar
i don't know why get a connection from pool is slow to me, and the connection haven't back to pool although i closed.
Thanks for your any suggestion .

**
* java file1 named ServerInformationInit, initinal connection pool cache. Any update need?
*
*/
public static OracleDataSource ods = new OracleDataSource(); // is a statci variable will be used in other java file
prop.setProperty("MinLimit", "5");// set cache properties
prop.setProperty("MaxLimit", "20");
prop.setProperty("InitialLimit", "5");
prop.setProperty("ConnectionWaitTimeout","3");
prop.setProperty("InactivityTimeout", "3");
ods.setConnectionCachingEnabled(true); // be sure set to true
ods.setConnectionCacheProperties(prop);
ods.setConnectionCacheName("eform_db_cache"); // this cache's name
url = "jdbcracle:thin:@" + EformAplProperty.getDbHostName() + ":" +
EformAplProperty.getDbPort() + ":" + EformAplProperty.getDbSid();
ods.setURL(url);
ods.setUser(EformAplProperty.getDbUserName());
ods.setPassword(EformAplProperty.getDbPassword());


/**
* java file 2 named DefnSqlManager is all DAO file's parent
*/
connection = ServerInformationInit.ods.getConnection();
...............
OracleConnectionCacheManager occm =
OracleConnectionCacheManager.getConnectionCacheManagerInstance();
System.out.println
(occm.getNumberOfAvailableConnections("eform_db_cache")
+ " connections are available in cache " + "eform_db_cache");
System.out.println
(occm.getNumberOfActiveConnections("eform_db_cache")
+ " connections are active");

/**
* java file 3 named QaCmptCaseSqlManager extend DefnSqlManager, which retrieve data from database
*/
sqlMgr = new QaCmptCaseSqlManager();
cmptEntity = sqlMgr.genCmptCaseDoc(docId)
finally {
sqlMgr.closeDbConn();
}

below message is printed in console:
4 connections are available in cache eform_db_cache
14 connections are active
3 connections are available in cache eform_db_cache
15 connections are active
4 connections are available in cache eform_db_cache
14 connections are active
3 connections are available in cache eform_db_cache
15 connections are active
2 connections are available in cache eform_db_cache
2 connections are available in cache eform_db_cache
16 connections are active
16 connections are active

As you see, the connections are available in cache is less and most of connections are in active.
The result is system will create more and more connection for application, i don't what wrong about it ?

Filename toad_sessiones.JPG Download
Description the sessiones in toad are equal to java connectiones?
Filesize 55 Kbytes
Downloaded: 1 time(s)

the original url is http://www.coderanch.com/t/440913/Oracle-OAS/ojdbc-jar-connection-pool-cache
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2503
    
    8

It's better to create a new topic when you want to discuss a problem not related to the OP subject. (ah - you did that already)
ken zhu
Greenhorn

Joined: Apr 09, 2009
Posts: 20
Jan Cumps wrote:It's better to create a new topic when you want to discuss a problem not related to the OP subject. (ah - you did that already)

Yes, i did. But nobody reply me and i found this topic is similar with it so.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query regarditng Connection Pooling