views:

1084

answers:

3

I'm developing a web app using Java servlet to access Mysql db, how can I get the number of connections to my DB that is currently open ?

Edit :

I tried "show processlist", it showed me : 2695159, but that's not right, I'm just developing this new project, I'm the only user, couldn't have that many processes running, what I want is the number of users accessing my project's DB, not the number of all db users, but just the ones logged in to my database which has only one table.

A: 

show processlist

Dmitry Khalatov
+3  A: 

You could use the MySQL command show processlist to get the number of connections.

However that'll also show you any connections made with the same userID to the database which may not be coming from your servlet.

In general I would suggest that you're probably better off using a Connection Pool object (see http://java-source.net/open-source/connection-pools) to manage your connections to the MySQL server. This can increase performance by making DB connections persistent, so you don't always have the overhead of a new DB connection for each page load.

If your servlet needs to know the number of connections then your Connection Pool should come with a method that tells you how many connections are currently active.

Alnitak
A: 

show status like 'Threads_connected' or show global status like 'Threads_connected'

Not sure about the difference between those two in a user-context, and you might still suffer from the problem that you would see all connections, not only those from your app.

you can even check Threads_running to only see running threads (e.g not sleeping).

jishi