javaregexeclipsefindbugscode-search

searching for multiple occurrences of variables in a method


I have a problem with cursor leakage in my Java project.

Typical example:

private void doSomething() throws Exception {
    String sql1= "some sql statement";
    String sql2= "some other sql statement";        
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {

        Connection con = getConnection();
        ps = con.prepareStatement(sql1);
        rs = ps.executeQuery();
        //do something with the ResultSet rs        
        //[Need to call ps.close here. Otherwise I risk getting ORA-01000.]
        ps = con.prepareStatement(sql2);              
        ps.executeQuery();
    } catch (Exception e) {
    } finally {
        ps.close();
        rs.close();
    }
}

Since I have a rather large code base I would like to be able to find all methods having two or more variables named sql.

Alternativly finding methods with two (or more) calls for prepareStatement without calling ps.close; between the two.

I am using Eclipse and the file search has a regex option. Maybe thet is the way to go? If so, what would it look like?


Solution

  • Recent versions of Eclipse (I used Juno [4.2]) will show a warning on those lines:

    editor screenshot

    You can enable this warning in the Eclipse preferences:

    enter image description here

    Even for larger size code bases, you can filter the problems view for this warning in order to find these places in the code.