使用PMD识别Java代码中未关闭的连接 [英] Identifying Connection not closed in java code using PMD

查看:144
本文介绍了使用PMD识别Java代码中未关闭的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了识别代码中的连接泄漏,我正在使用PMD. 现在使用PMD,如果找不到connection.close(),它将识别所有集合泄漏并显示以下错误.

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

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

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.

因此,我已经如下修改了design.xml(规则集).更改以**突出显示

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>

现在,使用ResourceClosureUtil.closeDBConnection关闭连接时,此更改不会发出任何错误警报.

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

现在,我们有许多自定义的关闭方法,例如"ClosureUtil.closeConnection","Resource.close",如何使用 这些方法?为了解决这个问题,我修改了closeTargets属性以包含所有此类方法.用**

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>

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

但是它没有按预期工作.它仍在发出错误的警报.当我有一个自定义的关闭方法时,它不会发出错误警报就可以正常工作,但是当我有很多这样的方法时,当我将所有这些方法都包含在closetargets属性中时,它就无法工作.

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.

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

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将具有连接未关闭违规和其他违规的输出.

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

推荐答案

closeTargets属性也是CloseResource实现中的HashSet<String> as types属性.因此,理想情况下,以下内容应为您工作:

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>

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

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

这篇关于使用PMD识别Java代码中未关闭的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆