*
The moose likes BEA/Weblogic and the fly likes About Connection.close() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » BEA/Weblogic
Bookmark "About Connection.close()" Watch "About Connection.close()" New topic
Author

About Connection.close()

Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Hello!!

I am getting Connection from the using DataSouce by following code...



I am using following code to close the Connection

I want to know here, by doing Connection.close(), does the Connection object returns to the ConnectionPool?

Reg,
Chetan


My blood is tested +ve for Java.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Yes, and close the connection in your finally block.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Yeah closing in finally block only.

But suppose if I have 10 open connetions, and I close a connection using above mentioned code, then I will have 10-1 = 9 open connections, right?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Let me rephrase my post. You should close your connection the moment that you do not need it and also close it in your finally block. This will keep the number of open connections to a minimum.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Originally posted by Chetan Parekh:
Yeah closing in finally block only.

But suppose if I have 10 open connetions, and I close a connection using above mentioned code, then I will have 10-1 = 9 open connections, right?


No. If you are getting connections from a connection pool set up in weblogic using that datasource, when you close the connection it is returned to the pool. It would then be available for the next request that asks for a connection.

You can monitor connections in the console, see Monitoring Connections in a JDBC Connection Pool.

If a new connection is opened for each new user or request, you have probabaly missed a place where the connection isn't getting closed.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
You can also configure WebLogic Server to pin a connection to a particular thread. Closing the connection will in such a case cause WebLogic Server to reserve the connection to the thread, so the connection will not be returned to the pool. When the same thread needs a connection again, WebLogic Server will provide the reserved connection. If the thread needs additional connections, then WebLogic Server will create additional connections and pin them to the thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Connection.close()
 
Similar Threads
InitialContext WebSphere 3.5.4
Datasource Configuration
Reuse an object?
Datasource JNDI Lookup in WSAD from standalone app
Max Connection Problem