From 3623f0760534e2c6e04941b320ba4f9daf366963 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Thu, 18 May 2017 17:26:28 +0200 Subject: [PATCH] Fix stability of integration tests on Oracle Error is "ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired" when resetting data. --- .../sonar/server/platform/BackendCleanup.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/BackendCleanup.java b/server/sonar-server/src/main/java/org/sonar/server/platform/BackendCleanup.java index 9a9b40c4b20..6cf963f55f4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/BackendCleanup.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/BackendCleanup.java @@ -31,6 +31,7 @@ import org.sonar.api.server.ServerSide; 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; @@ -126,11 +127,11 @@ public class BackendCleanup { 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(); } @@ -142,6 +143,18 @@ public class BackendCleanup { } } + 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(); -- 2.39.5