In case of missing resources for prepared statements detect and clearly indicate this issue.

This commit is contained in:
Stefan Neuhaus
2017-07-13 21:22:15 +02:00
parent 239c5f2e46
commit cbb10a1b1c

View File

@@ -258,14 +258,19 @@ public final class CveDB implements AutoCloseable {
* database connection * database connection
*/ */
private synchronized void open() throws DatabaseException { private synchronized void open() throws DatabaseException {
if (!instance.isOpen()) { try {
instance.connection = ConnectionFactory.getConnection(); if (!instance.isOpen()) {
final String databaseProductName = determineDatabaseProductName(instance.connection); instance.connection = ConnectionFactory.getConnection();
instance.statementBundle = databaseProductName != null final String databaseProductName = determineDatabaseProductName(instance.connection);
? ResourceBundle.getBundle("data/dbStatements", new Locale(databaseProductName)) instance.statementBundle = databaseProductName != null
: ResourceBundle.getBundle("data/dbStatements"); ? ResourceBundle.getBundle("data/dbStatements", new Locale(databaseProductName))
instance.prepareStatements(); : ResourceBundle.getBundle("data/dbStatements");
instance.databaseProperties = new DatabaseProperties(instance); instance.prepareStatements();
instance.databaseProperties = new DatabaseProperties(instance);
}
} catch(DatabaseException e) {
releaseResources();
throw e;
} }
} }
@@ -290,14 +295,18 @@ public final class CveDB implements AutoCloseable {
LOGGER.error("There was an exception attempting to close the CveDB, see the log for more details."); LOGGER.error("There was an exception attempting to close the CveDB, see the log for more details.");
LOGGER.debug("", ex); LOGGER.debug("", ex);
} }
instance.statementBundle = null; releaseResources();
instance.preparedStatements.clear();
instance.databaseProperties = null;
instance.connection = null;
} }
} }
} }
private synchronized void releaseResources() {
instance.statementBundle = null;
instance.preparedStatements.clear();
instance.databaseProperties = null;
instance.connection = null;
}
/** /**
* Returns whether the database connection is open or closed. * Returns whether the database connection is open or closed.
* *
@@ -315,15 +324,15 @@ public final class CveDB implements AutoCloseable {
*/ */
private void prepareStatements() throws DatabaseException { private void prepareStatements() throws DatabaseException {
for (PreparedStatementCveDb key : values()) { for (PreparedStatementCveDb key : values()) {
final String statementString = statementBundle.getString(key.name());
final PreparedStatement preparedStatement; final PreparedStatement preparedStatement;
try { try {
final String statementString = statementBundle.getString(key.name());
if (key == INSERT_VULNERABILITY || key == INSERT_CPE) { if (key == INSERT_VULNERABILITY || key == INSERT_CPE) {
preparedStatement = connection.prepareStatement(statementString, new String[]{"id"}); preparedStatement = connection.prepareStatement(statementString, new String[]{"id"});
} else { } else {
preparedStatement = connection.prepareStatement(statementString); preparedStatement = connection.prepareStatement(statementString);
} }
} catch (SQLException exception) { } catch (SQLException | MissingResourceException exception) {
throw new DatabaseException(exception); throw new DatabaseException(exception);
} }
preparedStatements.put(key, preparedStatement); preparedStatements.put(key, preparedStatement);
@@ -492,7 +501,7 @@ public final class CveDB implements AutoCloseable {
mergeProperty.setString(1, key); mergeProperty.setString(1, key);
mergeProperty.setString(2, value); mergeProperty.setString(2, value);
mergeProperty.executeUpdate(); mergeProperty.executeUpdate();
} catch (MissingResourceException mre) { } catch (SQLException e) {
// No Merge statement, so doing an Update/Insert... // No Merge statement, so doing an Update/Insert...
final PreparedStatement updateProperty = getPreparedStatement(UPDATE_PROPERTY); final PreparedStatement updateProperty = getPreparedStatement(UPDATE_PROPERTY);
updateProperty.setString(1, value); updateProperty.setString(1, value);