Java 如何计算打开的数据库连接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/269979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to count open db connections?
提问by Frank
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 ?
我正在开发一个使用 Java servlet 访问 Mysql 数据库的 Web 应用程序,如何获取当前打开的数据库的连接数?
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.
我试过“show processlist”,它显示给我:2695159,但这不对,我只是在开发这个新项目,我是唯一的用户,不能运行那么多进程,我想要的是访问我项目数据库的用户数,不是所有数据库用户的数量,而是登录到我只有一张表的数据库的用户数。
回答by Dmitry Khalatov
show processlist
显示进程列表
回答by Alnitak
You could use the MySQL command show processlist
to get the number of connections.
您可以使用 MySQL 命令show processlist
来获取连接数。
However that'll also show you any connections made with the same userID to the database which may not be coming from your servlet.
但是,这也会向您显示使用相同用户 ID 与数据库建立的任何连接,这些连接可能不是来自您的 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.
一般来说,我建议您最好使用连接池对象(请参阅http://java-source.net/open-source/connection-pools)来管理与 MySQL 服务器的连接。这可以通过使数据库连接持久化来提高性能,因此您不会总是为每个页面加载产生新的数据库连接的开销。
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.
如果您的 servlet 需要知道连接数,那么您的连接池应该带有一个方法,告诉您当前有多少连接处于活动状态。
回答by jishi
show status like 'Threads_connected' or show global status like 'Threads_connected'
显示状态如“Threads_connected”或显示全局状态如“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).
您甚至可以检查 Threads_running 以仅查看正在运行的线程(例如未休眠)。
回答by Gerryjun
Depending on your MySQL version, you can perform a select on
根据您的 MySQL 版本,您可以在
SELECT COUNT(*) FROM information_schema.PROCESSLIST;
SELECT COUNT(*) FROM information_schema.PROCESSLIST;
and you can do a where
between the user, database, and host IP.
你可以where
在用户、数据库和主机 IP 之间做一个。
For example:
例如:
USE information_schema;
SELECT COUNT(*) FROM PROCESSLIST WHERE db ="mycase" AND HOST LIKE "192.168.11.174%"
回答by DmitrySemenov
You can only select from Information_Schema.Processlist
the data that belongs to you. It means you can use it for monitoring ONLY if you're logged in as root, otherwise you will be seeing the connections coming from your user you got logged in with.
您只能从属于您Information_Schema.Processlist
的数据中进行选择。这意味着您只能在以 root 身份登录时使用它进行监控,否则您将看到来自您登录用户的连接。
If you want proper monitoring SQL, it will be:
如果您想要适当的监控 SQL,它将是:
SELECT variable_value
FROM INFORMATION_SCHEMA.GLOBAL_STATUS
WHERE variable_name='threads_connected'
回答by Vallabha Vamaravelli
Run the following query, it lists out host name and no. of connections from each host:
运行以下查询,它会列出主机名和编号。每个主机的连接数:
SELECT host,count(host) FROM information_schema.processlist GROUP BY host;
SELECT host,count(host) FROM information_schema.processlist GROUP BY host;
回答by subhash lamba
You may use this
你可以用这个
SHOW GLOBAL STATUS; or show global status like "Threads_connected";
显示全球状况;或显示全局状态,如“Threads_connected”;
from Connections status you can findout total number of connections.
从连接状态中,您可以找到连接总数。
回答by Idham Perdameian
You also can count open connection by show the status from Threads_connected
variable name like this:
您还可以通过显示Threads_connected
变量名称的状态来计算打开的连接,如下所示:
SHOW STATUS WHERE variable_name = 'Threads_connected';
Or you can also count the process list directly from information_schema.PROCESSLIST
like below:
或者您也可以直接从information_schema.PROCESSLIST
下面计算进程列表:
SELECT COUNT(*) FROM information_schema.PROCESSLIST;