import org.sonar.api.utils.log.Loggers;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.dialect.Oracle;
import org.sonar.db.version.SqTables;
import org.sonar.server.component.index.ComponentIndexDefinition;
import org.sonar.server.es.BulkIndexer;
clearIndex(ComponentIndexDefinition.INDEX_TYPE_COMPONENT.getIndex());
}
- private static void truncateAnalysisTables(Connection connection) throws SQLException {
+ private void truncateAnalysisTables(Connection connection) throws SQLException {
try (Statement statement = connection.createStatement()) {
// Clear inspection tables
for (String table : ANALYSIS_TABLES) {
- statement.execute("TRUNCATE TABLE " + table.toLowerCase());
+ statement.execute(createTruncateSql(table.toLowerCase(Locale.ENGLISH)));
// commit is useless on some databases
connection.commit();
}
}
}
+ private String createTruncateSql(String table) {
+ if (dbClient.getDatabase().getDialect().getId().equals(Oracle.ID)) {
+ // truncate operation is needs to lock the table on Oracle. Unfortunately
+ // it fails sometimes in our QA environment because table is locked.
+ // We never found the root cause (no locks found when displaying them just after
+ // receiving the error).
+ // Workaround is to use "delete" operation. It does not require lock on table.
+ return "DELETE FROM " + table;
+ }
+ return "TRUNCATE TABLE " + table;
+ }
+
private static void deleteManualRules(Connection connection) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement("DELETE FROM rules WHERE rules.plugin_name='manual'")) {
statement.execute();