2017-04-24 07:42:56
1
I am trying to use maven pmd plugin to detect all the connection leaks across the project.We close the connection using BaseSqlUtl.close, so somehow if we can use PMD to find that whosoever has opened connection has used this method to close, we can detect connection leaks.
As we are using customized classes for closing connection , I created a rule set changes highlighted with ** in the following ruleset.xml.
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
The Design ruleset contains rules that flag suboptimal code implementations. Alternate approaches
are suggested.
since="1.2.2"
message="Ensure that resources like this {0} object are closed after use"
class="net.sourceforge.pmd.lang.java.rule.design.CloseResourceRule"
externalInfoUrl="http://pmd.sourceforge.net/pmd-5.0.5/rules/java/design.html#CloseResource">
Ensure that resources (like Connection, Statement, and ResultSet objects) are always closed after use.
3
****
public class Bar {
public void foo() {
Connection c = pool.getConnection();
try {
// do stuff
} catch (SQLException ex) {
// handle exception
} finally {
// oops, should close the connection using 'close'!
// c.close();
}
}
}
]]>
Now this is working fine for unparametrised methods like commitAndCloseQuietly() but unfortunately for the other methods which is accepting connection as a parameter like "sqlUtil.closeQuietly(connection)", its giving false alarms.
I tried referring to the similar question asked but could not be of help on this specific scenario:
Identifying Connection not closed in java code using PMD