Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Connection.close()

 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, and close the connection in your finally block.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic