How to check connection leak using regular expression?
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);
开发者_如何学Go /*
Some code goes here
*/
}
finally
{
/*
Some code goes here
*/
DBUtil.close(con);
or
closeConnection.close(con);
/*
Some code goes here
*/
}
regular expression is not good tool here. Just search in your files for getConnection
then search for close
for corresponding variable.
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.
you can write script to count open and close clauses
for example this one (perl script) outputs files with lines where open bracket wasn't closed:
#!/usr/bin/perl -w # brackets.pl: output filenames with unmatched brackets (with hint to linenumber) # example: ./brackets *.c while(<>) { if(!defined($f) or $f ne $ARGV){$f=$ARGV;$l=0} $l++; push @ar,"$f: $l" if(/{/); pop @ar if(/}/) } print"$_\n" for(@ar);
you can substitute brackets with your regex'es
精彩评论