在Java应用程序中查找连接泄漏 [英] Find Connection leak in Java application

查看:130
本文介绍了在Java应用程序中查找连接泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,一段时间后开始出现内部服务器错误,我问过一些人告诉我,这可能是由于我的应用程序中的连接泄漏所致.我开始搜索并找到此查询来模拟连接泄漏.

I have an application which starts giving me internal server error after some time, Some people I asked told me that this can be because of Connection leak in my application. I started searching and found this query to simulate connection leak.

select LAST_CALL_ET, SQL_TEXT, username, machine, to_char(logon_time, 'ddMon hh24:mi') as login, SQL_HASH_VALUE, PREV_HASH_VALUE, status from v$session, v$sql where username='USERNAME' and HASH_VALUE = PREV_HASH_VALUE order by last_call_et desc;.

select LAST_CALL_ET, SQL_TEXT, username, machine, to_char(logon_time, 'ddMon hh24:mi') as login, SQL_HASH_VALUE, PREV_HASH_VALUE, status from v$session, v$sql where username='USERNAME' and HASH_VALUE = PREV_HASH_VALUE order by last_call_et desc;.

我使用此查询监视了我的应用程序,并关闭了此结果中显示的查询的所有泄漏连接.但是现在我的应用程序开始为更少的非活动会话给出同样的错误. 我是否使用正确的查询找出活动会话/连接泄漏?有人告诉我条件HASH_VALUE = PREV_HASH_VALUE在此查询中是错误的,但是我不知道这些列(数据库知识不多.)

I monitored my application with this query and closed all leaked connections for the query shown in this result. But now my application is starting to give same error for even less inactive sessions . Am I using the correct query to find out in active session / connection leak ? Someone told me condition HASH_VALUE = PREV_HASH_VALUE in this query is wrong, But I do not know about these columns (not much DB knowledge.)

谢谢

推荐答案

如果需要查找泄漏,可以使用 jprofiler 能够跟踪套接字/jdbc泄漏.

If you need to find out leaks you can use profilers like yourkit or jprofiler which is able to track socket/jdbc leaks.

要修复泄漏,您必须找出打开连接的位置并使用try-with-resources来为您完成所有close()任务

To fix leaks you have to find out places where you opening connections and use try-with-resources which will do all close() stuff for you

try (Connection conection = DriverManager.getConnection(url);
     PreparedStatement statement = createPreparedStatement(conection); 
     ResultSet resultSet = statement.executeQuery()) {
     // process the resultSet here, all resources will be cleaned up
}

这篇关于在Java应用程序中查找连接泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆