Find Connection leak in Java application

断了今生、忘了曾经 提交于 2019-12-22 09:59:34

问题


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;.

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.)

Thank you


回答1:


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

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
}



回答2:


Most connection pools have a configuration to log connection leaks. I am not familiar with DBCP but the documentation indicates that the logAbandoned property will log connection leaks. If you set logAbandoned to true, DCBP should log stack traces some time after the pool timeout property. The stack trace will contain the location where the leaked connection was opened.



来源:https://stackoverflow.com/questions/39474430/find-connection-leak-in-java-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!