Skip to content

Commit

Permalink
CB-5440. Move more logic to InternalDB
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisSinelnikov committed Sep 19, 2024
1 parent 19dd574 commit 3fcaf75
Showing 1 changed file with 13 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,17 @@
import org.jkiss.dbeaver.model.security.SMAdminController;
import org.jkiss.dbeaver.model.security.user.SMTeam;
import org.jkiss.dbeaver.model.security.user.SMUser;
import org.jkiss.dbeaver.model.sql.SQLDialect;
import org.jkiss.dbeaver.model.sql.SQLDialectSchemaController;
import org.jkiss.dbeaver.model.sql.schema.ClassLoaderScriptSource;
import org.jkiss.dbeaver.model.sql.schema.SQLSchemaManager;
import org.jkiss.dbeaver.model.sql.schema.SQLSchemaVersionManager;
import org.jkiss.dbeaver.registry.storage.H2Migrator;
import org.jkiss.dbeaver.runtime.DBWorkbench;
import org.jkiss.dbeaver.utils.GeneralUtils;
import org.jkiss.dbeaver.utils.RuntimeUtils;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.IOUtils;
import org.jkiss.utils.SecurityUtils;

import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.sql.*;
import java.util.*;
Expand All @@ -78,15 +73,15 @@ public class CBDatabase extends InternalDB {
private static final String V1_DB_NAME = "cb.h2.dat";
private static final String V2_DB_NAME = "cb.h2v2.dat";
public static final String CB_SCHEMA_INFO_TABLE_NAME = "CB_SCHEMA_INFO";
public static final String MAC_ADDRESS = "macAddress";
public static final String HOST_NAME = "hostName";

private final WebApplication application;
private final WebDatabaseConfig databaseConfiguration;
private PoolingDataSource<PoolableConnection> cbDataSource;
private transient volatile Connection exclusiveConnection;

private String instanceId;
private SMAdminController adminSecurityController;
private SQLDialect dialect;

public CBDatabase(WebApplication application, WebDatabaseConfig databaseConfiguration) {
super(databaseConfiguration, application);
Expand All @@ -106,11 +101,7 @@ public Connection openConnection() throws SQLException {
if (exclusiveConnection != null) {
return exclusiveConnection;
}
return cbDataSource.getConnection();
}

public PoolingDataSource<PoolableConnection> getConnectionPool() {
return cbDataSource;
return getDbConnection().getConnection();
}

public void initialize() throws DBException {
Expand Down Expand Up @@ -186,19 +177,16 @@ public void initialize() throws DBException {
dbURL = getDbURL();

try {
this.cbDataSource = initConnectionPool(driver, dbURL, dbProperties, driverInstance);
super.setDbConnection(initConnectionPool(driver, dbURL, dbProperties, driverInstance));
} catch (SQLException e) {
throw new DBException("Error initializing connection pool");
}
dialect = driver.getScriptDialect().createInstance();

try (Connection connection = cbDataSource.getConnection()) {
try (Connection connection = getDbConnection().getConnection()) {
DatabaseMetaData metaData = connection.getMetaData();
log.debug("\tConnected to " + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion());

if (dialect instanceof SQLDialectSchemaController dialectSchemaController && CommonUtils.isNotEmpty(schemaName)) {
createSchemaIfNotExists(connection, dialectSchemaController, schemaName);
}
createSchema(driver, schemaName, connection);
SQLSchemaManager schemaManager = new SQLSchemaManager(
"CB",
new ClassLoaderScriptSource(
Expand All @@ -208,7 +196,7 @@ public void initialize() throws DBException {
),
monitor1 -> connection,
new CBSchemaVersionManager(),
dialect,
getDialect(),
null,
schemaName,
CURRENT_SCHEMA_VERSION,
Expand Down Expand Up @@ -432,15 +420,7 @@ protected void validateInstancePersistentState(Connection connection) throws IOE
}

private void checkInstanceRecord(Connection connection) throws SQLException, IOException {
String hostName;
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
hostName = "localhost";
}

byte[] hardwareAddress = RuntimeUtils.getLocalMacAddress();
String macAddress = CommonUtils.toHexString(hardwareAddress);
Map<String, String> hostInfo = getHostInfo();

instanceId = getCurrentInstanceId();

Expand All @@ -457,8 +437,8 @@ private void checkInstanceRecord(Connection connection) throws SQLException, IOE
"(INSTANCE_ID,MAC_ADDRESS,HOST_NAME,PRODUCT_NAME,PRODUCT_VERSION,UPDATE_TIME)" +
" VALUES(?,?,?,?,?,CURRENT_TIMESTAMP)"),
instanceId,
macAddress,
hostName,
hostInfo.get(MAC_ADDRESS),
hostInfo.get(HOST_NAME),
productName,
versionName);
} else {
Expand All @@ -467,7 +447,7 @@ private void checkInstanceRecord(Connection connection) throws SQLException, IOE
normalizeTableNames("UPDATE {table_prefix}CB_INSTANCE " +
"SET HOST_NAME=?,PRODUCT_NAME=?,PRODUCT_VERSION=?,UPDATE_TIME=CURRENT_TIMESTAMP " +
"WHERE INSTANCE_ID=?"),
hostName,
hostInfo.get(HOST_NAME),
productName,
versionName,
instanceId);
Expand Down Expand Up @@ -518,17 +498,11 @@ protected WebApplication getApplication() {
protected SMAdminController getAdminSecurityController() {
return adminSecurityController;
}

@NotNull
public SQLDialect getDialect() {
return dialect;
}

public void shutdown() {

Check warning on line 501 in server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabase.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabase.java#L501

METHOD_DEF should be separated from previous line.

Check warning on line 501 in server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabase.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.service.security/src/io/cloudbeaver/service/security/db/CBDatabase.java#L501

Missing a Javadoc comment.
log.debug("Shutdown database");
if (cbDataSource != null) {
if (getDbConnection() != null) {
try {
cbDataSource.close();
getDbConnection().close();
} catch (SQLException e) {
log.error(e);
}
Expand Down

0 comments on commit 3fcaf75

Please sign in to comment.