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;
/**
*/
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) {
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);
+ }
}
*/
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 {
@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;
}
}