下面就是我得到的聲納違反狡猾 - 已知值冗餘nullcheck爲空
public static Connection getConnection(
String db)
throws SQLException {
Connection connection = null;
try {
Driver dbRriver = (Driver) Class.forName(driver).newInstance();
Properties props = new Properties();
props.put("user", PropertyLoader.get("user123"));
props.put("password", PropertyLoader.get("pass"));
connection = dbRriver
.connect(PropertyLoader.get("URL"), props);
} catch (Exception e) {
**if(connection!= null){
connection.close();**
}
LOGGER.error(IN_EXCEPTION, e);
}
return connection;
}
的連接需要關閉它之前要檢查NULL條件的代碼,但聲納給違規爲「狡猾 - 已知值爲空的冗餘空值檢查」。請建議我如何在不刪除空檢查的情況下避免此違規行爲。
你的親密關係不應該在你的抓住,而是在你最後的聲明。 – Stultuske
@Stultuske通常,是的。但是這種方法應該返回一個開放的連接。它不使用連接本身。 –
@Stultuske:David是對的,我需要返回一個開放的連接,所以我不能把connection.close()放在finally塊中。 –