]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14351 Add default transaction isolation to /api/system/info.
authorWojciech Wajerowicz <wojciech.wajerowicz@sonarsource.com>
Wed, 12 Oct 2022 09:31:53 +0000 (11:31 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 13 Oct 2022 20:03:18 +0000 (20:03 +0000)
SONAR-14351 Add message for unknown isolation level.

server/sonar-server-common/src/main/java/org/sonar/server/platform/monitoring/DbSection.java
server/sonar-server-common/src/test/java/org/sonar/server/platform/monitoring/DbSectionTest.java

index 813c36d9100cdb4d93a2dd8daf77caab0c4c78c1..917b2233d7507df5b6762e2365900debd8b28f05 100644 (file)
@@ -21,12 +21,18 @@ package org.sonar.server.platform.monitoring;
 
 import java.sql.DatabaseMetaData;
 import java.sql.SQLException;
+import java.util.Map;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.process.systeminfo.Global;
 import org.sonar.process.systeminfo.SystemInfoSection;
 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Section;
 
+import static java.sql.Connection.TRANSACTION_NONE;
+import static java.sql.Connection.TRANSACTION_READ_COMMITTED;
+import static java.sql.Connection.TRANSACTION_READ_UNCOMMITTED;
+import static java.sql.Connection.TRANSACTION_REPEATABLE_READ;
+import static java.sql.Connection.TRANSACTION_SERIALIZABLE;
 import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
 
 /**
@@ -34,6 +40,13 @@ import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
  */
 public class DbSection implements SystemInfoSection, Global {
 
+  private static final Map<Integer, String> ISOLATION_LEVEL_BY_ID = Map.of(
+    TRANSACTION_NONE, "TRANSACTION_NONE",
+    TRANSACTION_READ_UNCOMMITTED, "TRANSACTION_READ_UNCOMMITTED",
+    TRANSACTION_READ_COMMITTED, "TRANSACTION_READ_COMMITTED",
+    TRANSACTION_REPEATABLE_READ, "TRANSACTION_REPEATABLE_READ",
+    TRANSACTION_SERIALIZABLE, "TRANSACTION_SERIALIZABLE");
+
   private final DbClient dbClient;
 
   public DbSection(DbClient dbClient) {
@@ -52,9 +65,14 @@ public class DbSection implements SystemInfoSection, Global {
       setAttribute(protobuf, "URL", metadata.getURL());
       setAttribute(protobuf, "Driver", metadata.getDriverName());
       setAttribute(protobuf, "Driver Version", metadata.getDriverVersion());
+      setAttribute(protobuf, "Default transaction isolation", toTransactionIsolationLevelName(metadata.getDefaultTransactionIsolation()));
     } catch (SQLException e) {
       throw new IllegalStateException("Fail to get DB metadata", e);
     }
     return protobuf.build();
   }
+
+  private static String toTransactionIsolationLevelName(int level) {
+    return ISOLATION_LEVEL_BY_ID.getOrDefault(level, "Unknown transaction level: " + level);
+  }
 }
index 57cc4e086e9e2bc802346fc2ee88898bcaaaba47..e89dd25aedb87dcd502b47ce201dc5b41782872e 100644 (file)
  */
 package org.sonar.server.platform.monitoring;
 
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.SQLException;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 import static org.sonar.process.systeminfo.SystemInfoUtils.attribute;
 
 public class DbSectionTest {
@@ -33,14 +40,36 @@ public class DbSectionTest {
   @Rule
   public final DbTester dbTester = DbTester.create(System2.INSTANCE);
 
-  private final DbSection underTest = new DbSection(dbTester.getDbClient());
-
   @Test
   public void db_info() {
+    DbSection underTest = new DbSection(dbTester.getDbClient());
     ProtobufSystemInfo.Section section = underTest.toProtobuf();
     SystemInfoTesting.assertThatAttributeIs(section, "Database", "H2");
     assertThat(attribute(section, "Database Version").getStringValue()).startsWith("2.");
     SystemInfoTesting.assertThatAttributeIs(section, "Username", "SONAR");
     assertThat(attribute(section, "Driver Version").getStringValue()).startsWith("2.");
+    SystemInfoTesting.assertThatAttributeIs(section, "Default transaction isolation", "TRANSACTION_READ_COMMITTED");
+  }
+
+  @Test
+  public void db_info_unknown_transaction_isolation() throws SQLException {
+    DbClient dbClient = prepareClientWithUnknownTransactionLevel();
+
+    DbSection underTest = new DbSection(dbClient);
+    ProtobufSystemInfo.Section section = underTest.toProtobuf();
+    SystemInfoTesting.assertThatAttributeIs(section, "Default transaction isolation", "Unknown transaction level: 42");
+  }
+
+  private static DbClient prepareClientWithUnknownTransactionLevel() throws SQLException {
+    DbClient dbClient = mock(DbClient.class);
+    DbSession dbSession = mock(DbSession.class);
+    Connection connection = mock(Connection.class);
+    DatabaseMetaData metadata = mock(DatabaseMetaData.class);
+
+    when(dbClient.openSession(false)).thenReturn(dbSession);
+    when(dbSession.getConnection()).thenReturn(connection);
+    when(connection.getMetaData()).thenReturn(metadata);
+    when(metadata.getDefaultTransactionIsolation()).thenReturn(42);
+    return dbClient;
   }
 }