Code Inspection - Resources Management Issues - Shows incorrect results.
Answered
Hi Team,
In am using Intellij idea 17.1.4 Community Edition for Java. I am trying to analyze code using Analyse --> Inspect Code.
Please find attached Sample code for reference. For both prepared statement it shows 'PreparedStatement' should be opened in front of a 'try' block and closed in the corresponding 'finally' block.
Request you to explain when this warning is shown by idea. I am using JDK1.7 for compilation and language level.
Please sign in to leave a comment.
Please find sample code below.
void method(){
PreparedStatement pstmt1,pstmt2;
String query1 = "Select 1 from dual";
String query2 = "Select 2 from dual"
Connection con;
ResultSet rs1,rs2;
try{
con=ConnectionUtility.getConnection();
pstmt1=con.prepareStatement(query1);//'PreparedStatement' should be opened in front of a 'try' block and closed in the corresponding 'finally' bloc
pstmt2=con.prepareStatement(query2);//'PreparedStatement' should be opened in front of a 'try' block and closed in the corresponding 'finally' bloc
rs1=pstmt1.executeQuery();
ConnectionUtility.close(rs1);
rs2=pstmt2.executeQuery();
ConnectionUtility.close(rs2);
}catch (SQLException e) {
//Handle Exception
}finally{
ConnectionUtility.close(pstmt1);
ConnectionUtility.close(pstmt2);
ConnectionUtility.close(con);
}
}
See https://youtrack.jetbrains.com/issue/IDEA-60577#comment=27-177003 for the explanation.