Domanda

For identifying connection leaks in code, i'm using PMD. Now with PMD, it will identify all collection leaks and show the following error, if it didn't find connection.close().

C:\raghu\Harmony\branch\GlobalSKUPhase1A\source\Java\DataLoadServiceApp\src\dell\harmony\data\JdoServer.java:861:   Ensure that resources like this Connection object are closed after use

However, with our project code, we also use many customized connection close, i.e) instead of using connection.close(), we call a method and close the connection i.e) ResourceClosureUtil.closeDBConnection Now while running pmd, eventhough i've closed the connection using ResourceClosureUtil.closeDBConnection, it gives a false alarm, that connection is not closed.

So i've modified the design.xml (ruleset) as follows. changes are highlighted with **

<?xml version="1.0"?>

<ruleset name="Design"
    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">

  <description>
The Design ruleset contains rules that flag suboptimal code implementations. Alternate approaches
are suggested.
  </description>

  <rule name="CloseResource"
          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">
    <description>
Ensure that resources (like Connection, Statement, and ResultSet objects) are always closed after use.
    </description>
    <priority>3</priority>
    <properties>
    <property name="types" value="Connection,Statement,ResultSet"/>
        **<property name="closeTargets" value="ResourceClosureUtil.closeDBConnection"/>**
    </properties>
    <example>
<![CDATA[
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();
    }
  }
}
]]>
    </example>
  </rule>

</ruleset>

Now with this change it is not giving any false alarm, when connection is closed with ResourceClosureUtil.closeDBConnection.

Now we have many customized close methods like , "ClosureUtil.closeConnection", "Resource.close", how to stop false alarm with these methods? In order to solve, i've modified closeTargets property to include all such methods. changes highlighted with **

<property name="types" value="Connection,Statement,ResultSet"/>
    **<property name="closeTargets" value="ResourceClosureUtil.closeDBConnection"/>
    <property name="closeTargets" value="ClosureUtil.closeConnection"/>
    <property name="closeTargets" value="Resource.close"/>**
</properties>

or

<property name="types" value="Connection,Statement,ResultSet"/>
    <property name="closeTargets"
        value="ResourceClosureUtil.closeDBConnection,ClosureUtil.closeConnection,Resource.close"/>
</properties>

But it is not working as expected. It is still giving false alarm. When i hve one customized closure method , it works fine by not giving false alarm, but when i have many such method, it is not working, when i include all such methods in closetargets property.

I dont want to see any false alarm with any of these customized connection closures(e.g.) ResourceClosureUtil.closeDBConnection,ClosureUtil.closeConnection,Resource.close). How to solve this issue(false alarm: multiple connection closure) with PMD? How to change to closetargets property to include multiple methods?

FYI: I'm running it from command prompt, to identify the connection leaks.

pmd -d C:\raghu\Harmony\branch\GlobalSKUPhase1A\source\Java\DataLoadServiceApp\src -f text 
-R rulesets/java/unusedcode.xml,rulesets/java/controversial.xml,rulesets/java/basic.xml,rulesets/java/strings.xml,rulesets/java/design.xml,rulesets/java/naming.xml,rulesets/java/finalizers.xml,rulesets/java/braces.xml,rulesets/java/clone.xml,rulesets/java/codesize.xml,rulesets/java/imports.xml,rulesets/java/javabeans.xml,rulesets/java/logging-jakarta-commons.xml,rulesets/java/logging-java.xml,rulesets/java/migrating.xml,rulesets/java/optimizations.xml,rulesets/java/strictexception.xml,rulesets/java/sunsecure.xml,rulesets/java/coupling.xml > allexceptions.txt

command: pmd -d codesource -f output -R Ruleset > allexception.txt

allexception.txt will have the output of connection not closed violation and other violations.

È stato utile?

Soluzione

closeTargets property is also a HashSet<String> as types property in CloseResource implementation. So, ideally the below should work for you:

<properties>
    <property name="types" value="Connection,Statement,ResultSet"/>
    <property name="closeTargets" value="closeDBConnection, closeConnection, close"/>
</properties>

Note that I am providing the method names only in closeTargets property which are used to close resources.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top