Menu

#842 CloseResource Rule for Class level variable

open
pmd (543)
5
2012-10-07
2009-03-04
Arun Jain
No

I was working on the piece of code in which I created a class level reference variable for Connection. I instantiated this connection in main method in my class. When I check the code with PMD it does not ask or does not show any warning to close the connection.

public class myclass{
private Connection conn;

public static void main(String[] arg){
//..............
conn = DriverManager.getConnection(url,user,password);
//..............
}
}

Discussion

  • Ryan Gustafson

    Ryan Gustafson - 2009-03-04

    The example provided cannot compile. Can you fix please, so we know 100% what you are referring to? Thanks.

     
  • Arun Jain

    Arun Jain - 2009-03-04

    PMDTest.java for example of above issue

     
  • Arun Jain

    Arun Jain - 2009-03-04

    Kindly find the attached file PMDTest.java. You need to change the database driver, url, username, password to connect elsewise also it will compile.
    File Added: PMDTest.java

     

Log in to post a comment.