2017-04-24 29 views
0

maven pmdプラグインを使用してプロジェクト全体の接続リークをすべて検出しようとしています.Pbaseを使用して接続を終了すると、接続を開いている誰でもこのメソッドを使用してクローズすると、接続リークを検出できます。 接続を閉じるためのカスタマイズされたクラスを使用しているので、次のruleset.xmlに**で強調表示されたルールセットの変更を作成しました。PMDを使用してJavaコードで接続が検出されない

<?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="closeStatementQuietly,closeResultSetQuietly,commitAndCloseQuietly,rollbackAndCloseQuietly,closeQuietly,sqlUtil.closeQuietly,resetAndCloseQuietly"/>** 
    </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> 

今これはcommitAndCloseQuietlyようunparametrisedメソッド()残念ながら「sqlUtil.closeQuietly(接続)」、その与え誤報などのパラメータとして接続を受け入れている他の方法のためにのために正常に動作しています。

私は尋ねた同様の質問を参照しようとしましたが、この特定のシナリオに助けになることができませんでした: Identifying Connection not closed in java code using PMD

答えて

0

PMDは、現在のリソースを閉じるためのヘルパー・メソッドを使用するためにサポートしていません。たとえcloseメソッドを構成することができたとしても、それらはすべてリソース自体で呼び出されるものとみなされます。

PMDがマルチクラスDFAを実行した場合、これは解決される可能性がありますが、現時点ではそうではなく、多少時間がかかりません(結果をキャッシュする能力に大きな影響を与えます)。

より良い回避策は、リソースを引数として取るクローズヘルパーメソッドを指定するサポートを追加することです。 Github(https://github.com/pmd/pmd)で機能リクエストを作成しても構いません。またはそれ以上に、PRを提出してください。

関連する問題