查出java未关闭连接_使用PMD maven检测Java代码中的连接未关闭 (Detecting Connection not closed in java code using PMD maven...

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值