aspose file tools*
The moose likes Object Relational Mapping and the fly likes Number of Open Connections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Number of Open Connections" Watch "Number of Open Connections" New topic
Author

Number of Open Connections

Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613
Hi All,

I have to print number of open connections hold by Hibernate,is there any way I can achieve this?

I am using hibernate 3.0.5 with org.hibernate.transaction.JBossTransactionManagerLookup datasource.

I found following method,but it tells number of connections opened so far and not actual opened connections at given time.



-Praful
[ April 24, 2006: Message edited by: Praful Thakare ]

All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613
Oh come on guys,there must be some way to get it from hibernate !!!

please help me

-P
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2906
Originally posted by Praful Thakare:
Oh come on guys,there must be some way to get it from hibernate!!!

Not necessarily. Hibernate uses a connection pool manager (c3p0 by default) so it's the pool manager that keeps them open, not Hibernate. You can configure the connections used by the manager.

You may be able to configure the connection manager to log the information you are looking for - however that configuration would be dependent on the connection pool manager that you are using.


"Don't succumb to the false authority of a tool or model. There is no substitute for thinking."
Andy Hunt, Pragmatic Thinking & Learning: Refactor Your Wetware p.41
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613
Hibernate uses a connection pool manager (c3p0 by default) so it's the pool manager that keeps them open, not Hibernate.



Yes Peer,thats true,but my question is as hibernate provides methods to print number of global connections open,number of prepared statements open so why not number of current open connections (or global closed connections if its for some reason not possible to get open connections count from connection pool).

Also,is there any setting to in connection pool descriptor for number of open connections?

-Praful
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
why do you want to get the number of open connections ? would be helpful if you can give this information...

currently to me it sounds like you try to debug an application where you do not close all TX/Sessions/Connections ?

you can setup the SessionFactory in different ways.... one way you specify the DataSource to be used. So create a DataSource first, then use it for SessionFactory creation and use the DataSource for your stats (as Peer mentioned this is probably a pooled source and if the pool traces this information then you can get it from there)... or enable DEBUG logging (this will show you when what is opened/closed/executed)


pascal
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613
why do you want to get the number of open connections


Yes I m trying to debug my application,that runs out of connections after few days,i m closing all the session and transactions in my code,but I guess my application server is not closing the physical connection I am using jboss datasource for SessionFactory creation and dont know how to get this information out of it.

cheers
-Praful
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
i doubt that this is an app server problem (unless you run older versions of JBoss).
if you do not use some sort of framework (spring/your homegrown stuff) for the hibernate part, then its easy to miss some close() operation.

- do you close all your sessions/TX in a finally clause ? how can you be sure you close all your sessions ?
- did you enable DEBUG level logging for hibernate ? did you read the logs ? (hibernate sometimes tells you when some close operation is missing)
- can you write a demo application that runs on your app server that reproduces the problem ?
- did you check the updates for your appserver ?
- did you check the jboss datasource API ?


pascal
Mahesh Gurav
Greenhorn

Joined: Oct 18, 2004
Posts: 21
If you are using c3p0 connection pooling this link will help.If a connection is unused it will be closed after certain time interval.
http://www.hibernate.org/214.html
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
closing the connections automagically is just a hack if you can not find the reason why it is not closed/returned...
so i would not recommend using this ;-)

pascal
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613
Hey Pascal,

Answering your questions below

I used profiling to find the problem,If I run my application under load even though session.close() is called in finally and tran.commit() at end of business logic,I find jboss (4.0.2) do not close the connections.

I did not write demo application,but used same business flow under load,and I am sure all sessions are closed in the business method.

Following is how mysql-ds.xml and hibernate files look.





Cheers
-Praful
[ May 02, 2006: Message edited by: Praful Thakare ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I did not write demo application,but used same business flow under load,and I am sure all sessions are closed in the business method

Closing the session does not necessarily imply that the connection is also closed. It may just be returned to the pool. If you are running your application under a heavy load, there is every possibility that your connection pool may not be configured to provide enough connections.
[ May 02, 2006: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613


Thats exactly why i need to find number of open connections,I have to test if changing "ConnectionReleaseMode" forces closing connection on executing the statement (after_statement).


-Praful
[ May 02, 2006: Message edited by: Praful Thakare ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


Thats exactly why i need to find number of open connections

Well this is not something Hibernate can tell you.


I have to test if changing "ConnectionReleaseMode" forces closing connection on executing the statement (after_statement).

If you are using a connection pool changing the ConnectionReleaseMode will release connections back to the pool. It will not physically close connections. How many actual connections have been made is information you will get from your connection pool implementation, not Hibernate. Since you are using a DataSource in JBoss you can look at this via the jmx-console, and hence you will be able to get it programatically from the JBossManagedConnectionPool MBean.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613
Thanks a bunch Paul.

This information sounds helpful.

Cheers
-Praful
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number of Open Connections
 
Similar Threads
Question about a SocketException
Hibernate and Jdbc
plugin - DB connection leak
How to close Hibernate C3P0 Connections properly?
Where to open Hibernate Session and Transactions