English 中文(简体)
DD异常现象,清理数据库资源:是否有干净的解决办法?
原标题:DD anomaly, and cleaning up database resources: is there a clean solution?

这里有一个代码,我们都写了:

    public CustomerTO getCustomerByCustDel(final String cust, final int del)
            throws SQLException {
        final PreparedStatement query = getFetchByCustDel();
        ResultSet records = null;
        try {
            query.setString(1, cust);
            query.setInt(2, del);
            records = query.executeQuery();

            return this.getCustomer(records);
        } finally {
            if (records != null) {
                records.close();
            }
            query.close();
        }
    }

如果您忽略了最后的块块, 那么您就会留下数据库资源, 这显然是一个潜在的问题。 但是, 如果您做我在这里做的事 - 将结果部分设置为在 ** try** 块外的无效, 然后将其设定到块内所需的值 - PMD 报告DDD异常点。 在文档中, DD异常点描述如下:

DataflowAnomalyAnalysis: The dataflow analysis tracks local definitions, undefinitions and references to variables on different paths on the data flow.From those informations there can be found various problems. [...] DD - Anomaly: A recently defined variable is redefined. This is ominous but don t have to be a bug.

如果您在不设定值的情况下在块外声明结果共享, 当您做 < i> if (records! = null) 测试时, 正确的获得一个变量可能不是初始错误 。

在我看来,我在这里的用法并不是一个错误。但是,有没有一种清洁的改写方式不会触发PMD警告?我不想特别想禁用PMD S DataFlowAnomalyAnavication 规则,因为识别UR和DU异常实际上是有益的;但是,这些DD异常让我怀疑我可以做更好的事情――如果没有更好的方法,它们就等于是混乱(我也许应该看看我是否可以改写PMD规则)

问题回答

我认为这一点更清楚:

PreparedStatement query = getFetchByCustDel();
try {
    query.setString(1, cust);
    query.setInt(2, del);
    ResultSet records = query.executeQuery();
    try {
        return this.getCustomer(records);
    } finally {
        records.close();
    }
} finally {
    query.close();
}

另外,在您的版本中,如果记录. close () 抛出一个例外, 查询不会被关闭 。

I think that DD anomaly note is more bug, than a feature
Also, the way you free resources is a bit incomplete, for example

PreparedStatement pstmt = null;
Statement st = null; 

try {
    ...
} catch (final Exception e) {
    ...
} finally {
    try{
        if (pstmt != null) {
            pstmt.close();
        }
    } catch (final Exception e) {
        e.printStackTrace(System.err);
    } finally {
        try {
            if (st != null) {
                st.close();
            }
        } catch (final Exception e) {
            e.printStackTrace(System.err);
        }
    }
}

况且,这又说不对,因为你应该这样关闭资源

PreparedStatement pstmt = null;
Throwable th = null;

try {
    ...
} catch (final Throwable e) {
    <something here>
    th = e;
    throw e;
} finally {
    if (th == null) {
        pstmt.close();
    } else {
        try {
            if (pstmt != null) {
                pstmt.close();
            }
        } catch (Throwable u) {
        }
    }
}




相关问题
MaxPooledStatements setting in JDBC oracle

I can t figure out how to set MaxPooledStatements in Oracle using the Oracle thin JDBC driver. Could someone point me in the right direction?

pass ResultSet from servlet to JSP

I am doing the following in my SampleServlet.java //Fill resultset from db .... try { ArrayList Rows = new ArrayList(); while (resultSet.next()){ ArrayList row = new ArrayList(); ...

How to correctly use ResultSet with h:dataTable

The problem is, that after displaying the ResultSet with <h:dataTable>, the connection is left open. If I close it, it closes the ResultSet too. I m thinking about copying the ResultSet data ...

Mysql session variable in JDBC string

am using this connection string to connect to mysql from java: jdbc:mysql://localhost:3306/db?noDatetimeStringSync=true&useUnicode=yes&characterEncoding=UTF-8 is it possible to set the ...

热门标签