我在SONAR中遇到以下代码问题。但是该类实现了BaseDatabase接口,其方法不是从构造函数调用的。
public class DerbyDatabaseHelper implements BaseDatabase {
private static boolean isInitializationDone;
private static Connection connection;
private static final Logger LOGGER = Logger
.getLogger(DerbyDatabaseHelper.class);
/**
* @param connection
* the connection to set
*/
private static void setConnection(Connection connection) {
DerbyDatabaseHelper.connection = connection;
}
public DerbyDatabaseHelper() {
deleteDerbyFolder(new File("NewDB"));
if (!isInitializationDone) {
initializeDB();
setInitializationDone(true);
}
}
/**
* This method initializes the Derby Schema.
*/
private void initializeDB() {
try {
// ScriptRunner of Apache ibatis to execute the sql file for
// creating database schema in DerbyDB .
new ScriptRunner(getConnection())
.runScript(new InputStreamReader(new FileInputStream(
ConfigReader.getValue("schemaFilePath")), Charset
.defaultCharset()));
} catch (Exception e) {
LOGGER.error(
"Exception occurred during Initialization of DB. Exception details :: ",
e);
} finally {
closeConnection();
}
}
/**
* This method is used to get the derby database connection object
*/
@Override
public Connection getConnection() {
if (connection == null) {
try {
loadDerbyClass();
// Create jdbc Connection
setConnection(DriverManager.getConnection(ConfigReader
.getValue("initialUrl")
+ ConfigReader.getValue("dbName")
+ ConfigReader.getValue("createDb")));
} catch (SQLException e) {
LOGGER.error(
"Exception occurred during creating connection. Exception details :: ",
e);
}
}
return connection;
}
/**
* This method is used to close the acquired derby database connection
* object.
*/
@Override
public void closeConnection() {
if (connection != null) {
try {
connection.close();
} catch (Exception e) {
LOGGER.error(
"Exception occurred during closing connection. Exception details :: ",
e);
}
}
}
和界面
public interface BaseDatabase {
Connection getConnection();
void closeConnection();
}
正如我们所看到的,没有从构造函数调用的覆盖方法,声纳无法解释该问题。