使用PMD识别未在Java代码中关闭的连接

时间:2014-04-02 06:42:22

标签: java jdbc code-analysis pmd

为了识别代码中的连接泄漏,我使用PMD。 现在使用PMD,它将识别所有收集泄漏并显示以下错误,如果它没有找到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

但是,使用我们的项目代码,我们也使用许多自定义连接,即,而不是使用connection.close(), 我们调用一个方法并关闭连接,即ResourceClosureUtil.closeDBConnection 现在在运行pmd时,尽管我已经使用ResourceClosureUtil.closeDBConnection关闭了连接,但它给出了一个错误 警报,该连接未关闭。

所以我已经修改了design.xml(规则集),如下所示。更改以**

突出显示
<?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>

现在通过此更改,当使用ResourceClosureUtil.closeDBConnection关闭连接时,它不会发出任何误报。

现在我们有许多自定义的关闭方法,例如&#34; ClosureUtil.closeConnection&#34;,&#34; Resource.close&#34;,如何停止虚警 这些方法?为了解决这个问题,我修改了closeTargets属性以包含所有这些方法。用**

突出显示的更改
<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>

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

但它没有按预期工作。它仍然是虚惊一场。当我有一个自定义的闭包方法,它没有给出误报警工作正常,但是当我有很多这样的方法,当我在closetargets属性中包含所有这些方法时它不起作用。

我不希望看到任何这些自定义连接闭包的错误警报(例如)ResourceClosureUtil.closeDBConnection,ClosureUtil.closeConnection,Resource.close)。 如何用PMD解决这个问题(误报:多连接关闭)? 如何更改为closetargets属性以包含多个方法?

仅供参考:我从命令提示符运行它,以识别连接泄漏。

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将输出连接未被禁止的违规和其他违规行为。

1 个答案:

答案 0 :(得分:2)

closeTargets属性在HashSet<String>实施中也是typesCloseResource属性。所以,理想情况下,以下内容适合您:

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

请注意,我仅在closeTargets属性中提供方法名称,用于关闭资源。