fixed issue #808

This commit is contained in:
Jeremy Long
2017-07-17 06:40:39 -04:00
parent 2ab92a940b
commit 91b1d5cbde

View File

@@ -268,7 +268,7 @@ public final class CveDB implements AutoCloseable {
instance.prepareStatements(); instance.prepareStatements();
instance.databaseProperties = new DatabaseProperties(instance); instance.databaseProperties = new DatabaseProperties(instance);
} }
} catch(DatabaseException e) { } catch (DatabaseException e) {
releaseResources(); releaseResources();
throw e; throw e;
} }
@@ -324,7 +324,7 @@ 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 PreparedStatement preparedStatement; PreparedStatement preparedStatement = null;
try { try {
final String statementString = statementBundle.getString(key.name()); final String statementString = statementBundle.getString(key.name());
if (key == INSERT_VULNERABILITY || key == INSERT_CPE) { if (key == INSERT_VULNERABILITY || key == INSERT_CPE) {
@@ -332,10 +332,16 @@ public final class CveDB implements AutoCloseable {
} else { } else {
preparedStatement = connection.prepareStatement(statementString); preparedStatement = connection.prepareStatement(statementString);
} }
} catch (SQLException | MissingResourceException exception) { } catch (SQLException ex) {
throw new DatabaseException(exception); throw new DatabaseException(ex);
} catch (MissingResourceException ex) {
if (!ex.getMessage().contains("key MERGE_PROPERTY")) {
throw new DatabaseException(ex);
}
}
if (preparedStatement != null) {
preparedStatements.put(key, preparedStatement);
} }
preparedStatements.put(key, preparedStatement);
} }
} }
@@ -357,6 +363,9 @@ public final class CveDB implements AutoCloseable {
* @throws SQLException thrown if a SQL Exception occurs * @throws SQLException thrown if a SQL Exception occurs
*/ */
private synchronized PreparedStatement getPreparedStatement(PreparedStatementCveDb key) throws SQLException { private synchronized PreparedStatement getPreparedStatement(PreparedStatementCveDb key) throws SQLException {
if (!preparedStatements.containsKey(key)) {
return null;
}
final PreparedStatement preparedStatement = preparedStatements.get(key); final PreparedStatement preparedStatement = preparedStatements.get(key);
preparedStatement.clearParameters(); preparedStatement.clearParameters();
return preparedStatement; return preparedStatement;
@@ -496,12 +505,12 @@ public final class CveDB implements AutoCloseable {
public synchronized void saveProperty(String key, String value) { public synchronized void saveProperty(String key, String value) {
clearCache(); clearCache();
try { try {
try { final PreparedStatement mergeProperty = getPreparedStatement(MERGE_PROPERTY);
final PreparedStatement mergeProperty = getPreparedStatement(MERGE_PROPERTY); if (mergeProperty != null) {
mergeProperty.setString(1, key); mergeProperty.setString(1, key);
mergeProperty.setString(2, value); mergeProperty.setString(2, value);
mergeProperty.executeUpdate(); mergeProperty.executeUpdate();
} catch (SQLException e) { } else {
// 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);