try{} catch{} finally{} Structural Search
Hi, I am trying to do a try{} catch{} finally{} structural search to help me find a database connection leak.
The basic code in my project is as such:
try {
Connection conn = DBConnection.getConnection(); //project DB connection function
}
catch (Exception) {
//some generic handling of exceptions such as rollback
}
finally {
DBConnection.closeConnection(conn); //project DB close connection function
}
I am trying to find calls to DBConnection.getConnection() in the try block without a corresponding DBConnection.closeConnection(conn) in the finally block. (Which is the cause of the DB connection leak)
The structural search syntax that I am using is as follows:
try {
$TryStatement$;
} catch($ExceptionType$ $ExceptionDcl$) {
$CatchStatement$;
} finally {
$FinallyStatement$;
}
The problem is that when I use IntelliJ to assign $TryStatement$ to Connection conn = DBConnection.getConnection(); (or any variations of this method call) I do not get any search results.
I do a least know that I am on the right track as the above structural search syntax only returns me results with all three try && catch && finally blocks.
My main idea is to somehow get IntelliJ to assign DBConnection.getConnection() to $TryStatement$ and DBConnection.closeConnection(conn) to $FinallyStatement$, as well as add the constraint that $TryStatment$ min:1 max:"anything", $FinallyStatement$ min:0, max:0. And hope to find all occurances of getConnection() without subsequent calls of closeConnection() in the finally block.
Thanks
请先登录再写评论。
Hi,
When you assign text constraint to
$TryStatement$ your pattern expects to have all statements in try body
to have that constraint fulfilled (and most likely there is no such try
statements). Instead, the pattern should also have
$StatementBeforeDcl$, $StatementAfterDcl$ before / after $TryStatement$
to match other statements in try body.
Btw, 'conn' var declared in try statement is not accessible in finally.
trumper wrote:
--
Best regards,
Maxim Mossienko
IntelliJ Labs / JetBrains Inc.
http://www.intellij.com
"Develop with pleasure!"
Hi Maxim,
Thanks for your reply.
I took your tip and modified my search syntax as such.
try {
$B4TryStatement$;
$TryStatement$;
$AfterTryStatement$;
} catch($ExceptionType$ $ExceptionDcl$) {
$CatchStatement$;
} finally {
$FinallyStatement$;
}
I tried to assign "conn = DBConnection.getConnection();" to $TryStatement$ with the constraint of min count:1. However the search result is still empty.
When I reduce the $TryStatement$ min count to 0, all search results with the try{} catch{} finally{} block would return.
Should I be configuring any other constraints to get it to work properly?
Btw, all my $variable$ seen above (besides $TryStatement$) have constraints min:0 max:unlimited. And no other options such as recursive searching have been selected. I have also left the text / expression field empty for all other $variable$.
Thanks