I have to check connection leak in my application i.e. open connections which have not been closed.
After searching, after I found out that a thousands times connections have been opened but I can’t manually go to each and every code fragment and check for connection close thing.
I think, it could be possible using a regular expression but the fact is, I am not that well versed with regex that I could write one.
Please suggest a regular expression for checking in a no of java files that a opened connection has been closed or not.
My code pattern is something like this.
try {
/*
Some code goes here
*/
con = EJBUtil.getConnection(JNDI_NAME);
/*
Some code goes here
*/
}
finally
{
/*
Some code goes here
*/
DBUtil.close(con);
or
closeConnection.close(con);
/*
Some code goes here
*/
}
As a first cut, run FindBugs or PMD over your code. Both of the have rules for detecting source code in which database resources are not or may not be properly closed. And they will probably highlight lots of other issues that are worth fixing.