本文介绍了如何终止Postgresql中的空闲连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Java servlet和pgadmin 9.1一起使用。问题是servlet中的连接未正确关闭,因此如果达到最大连接数会导致黑屏。我不希望每个用户都在pgadmin中扩展最大连接数。 ..i在servlet的开始和结束点使用了以下查询,但是由于管理员命令,它显示了类似..connection的错误。.

I am using java servlets along with pgadmin 9.1.Problem is the connection in servlets was not closed properly so it leads to blank screen if it reaches the maximum connection.I don't want every user to extend maximum connections in pgadmin...i have used the below query in starting and end ending point of servlet but its showing error like..connection terminated due to administrator command..

ResultSet rs_1q=st_Query3.executeQuery("SELECT pg_terminate_backend(pg_stat_activity.procpid)FROM pg_stat_activity WHERE pg_stat_activity.current_query = '<IDLE>' AND procpid <> pg_backend_pid();");


推荐答案

通常,正如@Rahul指出的那样,不建议这样做杀死连接。但是,如果这是您的最后选择,那么这就是终止空闲连接的方法:

Generally, as @Rahul points out, its not advisable to kill connections. But if it's your last resort, this is how to terminate idle connections:

SELECT pg_terminate_backend(pid) FROM pg_stat_activity
WHERE datname = 'databasename'
AND pid <> pg_backend_pid()
AND state in ('idle');

这篇关于如何终止Postgresql中的空闲连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 12:43