From a0ab39c9a0cfb62a8621c180f86c9d2063a8bbd2 Mon Sep 17 00:00:00 2001 From: Areeb Gillani Date: Mon, 4 Sep 2023 06:56:13 +0500 Subject: [PATCH] Config object not found issue fixed --- .../areebgillani/db/boost/ConnectionManager.java | 8 ++++---- .../github/areebgillani/db/boost/CrudRepository.java | 5 +++-- .../github/areebgillani/db/mssql/MSSQLConnection.java | 10 ---------- .../github/areebgillani/db/mssql/MSSQLRepository.java | 4 ++-- .../github/areebgillani/db/mysql/MySQLConnection.java | 9 --------- .../github/areebgillani/db/mysql/MySQLRepository.java | 4 ++-- .../areebgillani/db/oracle/OracleConnection.java | 11 ----------- .../areebgillani/db/oracle/OracleRepository.java | 5 +++-- .../areebgillani/db/postgres/PostgresConnection.java | 11 ----------- .../areebgillani/db/postgres/PostgresRepository.java | 4 ++-- 10 files changed, 16 insertions(+), 55 deletions(-) diff --git a/src/main/java/io/github/areebgillani/db/boost/ConnectionManager.java b/src/main/java/io/github/areebgillani/db/boost/ConnectionManager.java index cb159d4..5034662 100644 --- a/src/main/java/io/github/areebgillani/db/boost/ConnectionManager.java +++ b/src/main/java/io/github/areebgillani/db/boost/ConnectionManager.java @@ -13,8 +13,8 @@ public class ConnectionManager { private static final HashMap dbConnectionMap = new HashMap<>(); - private static Object createDatabaseConnection(String connectionName){ - JsonObject conf = Vertx.currentContext().owner().getOrCreateContext().config().getJsonObject("dbConnections").getJsonObject(connectionName); + private static Object createDatabaseConnection(String connectionName, JsonObject config){ + JsonObject conf = config.getJsonObject("dbConnections").getJsonObject(connectionName); return switch (DatabaseType.valueOf(conf.getString("dbType"))){ case POSTGRESQL -> PostgresConnection.getInstance(conf); case MYSQL -> MySQLConnection.getInstance(conf); @@ -22,7 +22,7 @@ private static Object createDatabaseConnection(St case MSSQL -> MSSQLConnection.getInstance(conf); }; } - public static T getDBConnection(String connectionName){ - return (T) dbConnectionMap.computeIfAbsent(connectionName, ConnectionManager::createDatabaseConnection); + public static T getDBConnection(String connectionName, JsonObject config){ + return (T) dbConnectionMap.computeIfAbsent(connectionName, cn->createDatabaseConnection(cn,config)); } } diff --git a/src/main/java/io/github/areebgillani/db/boost/CrudRepository.java b/src/main/java/io/github/areebgillani/db/boost/CrudRepository.java index 44f8828..fbf7bd2 100644 --- a/src/main/java/io/github/areebgillani/db/boost/CrudRepository.java +++ b/src/main/java/io/github/areebgillani/db/boost/CrudRepository.java @@ -3,10 +3,11 @@ import io.github.areebgillani.db.utils.AbstractRepository; import io.vertx.core.Vertx; +import io.vertx.core.json.Json; import io.vertx.core.json.JsonObject; public class CrudRepository extends AbstractRepository { - public CrudRepository(String connectionName){ - databaseConnection = ConnectionManager.getDBConnection(connectionName); + public CrudRepository(String connectionName, JsonObject config){ + databaseConnection = ConnectionManager.getDBConnection(connectionName, config); } } diff --git a/src/main/java/io/github/areebgillani/db/mssql/MSSQLConnection.java b/src/main/java/io/github/areebgillani/db/mssql/MSSQLConnection.java index dabc07b..edeb34f 100644 --- a/src/main/java/io/github/areebgillani/db/mssql/MSSQLConnection.java +++ b/src/main/java/io/github/areebgillani/db/mssql/MSSQLConnection.java @@ -12,12 +12,6 @@ public class MSSQLConnection extends AbstractConnection { private static MSSQLConnection instance; private DatabaseConfig config; - public MSSQLConnection(String connectionName) { - this.config = DatabaseConfig.getInstance(Vertx.currentContext().config().getJsonObject("dbConnections").getJsonObject(connectionName)); - this.connectionOptions = getConnectionOption(); - this.poolOptions = getPoolOptions(); - this.client = getSQLPool(); - } public MSSQLConnection(JsonObject config) { this.config = DatabaseConfig.getInstance(config); this.connectionOptions = getConnectionOption(); @@ -43,10 +37,6 @@ protected MySQLConnectOptions getConnectionOption() { .setReconnectAttempts(config.DB_RETRY_COUNT) .setReconnectInterval(config.DB_RETRY_INTERVAL): this.connectionOptions; } - - public static MSSQLConnection getInstance(String connectionName) { - return instance == null ? new MSSQLConnection(connectionName) : instance; - } public static MSSQLConnection getInstance(JsonObject config) { return instance == null ? new MSSQLConnection(config) : instance; } diff --git a/src/main/java/io/github/areebgillani/db/mssql/MSSQLRepository.java b/src/main/java/io/github/areebgillani/db/mssql/MSSQLRepository.java index 734b103..7e00fb4 100644 --- a/src/main/java/io/github/areebgillani/db/mssql/MSSQLRepository.java +++ b/src/main/java/io/github/areebgillani/db/mssql/MSSQLRepository.java @@ -5,7 +5,7 @@ import io.vertx.core.json.JsonObject; public class MSSQLRepository extends AbstractRepository { - public MSSQLRepository(String connectionName) { - databaseConnection = MSSQLConnection.getInstance(connectionName); + public MSSQLRepository(String connectionName, JsonObject config) { + databaseConnection = MSSQLConnection.getInstance(config.getJsonObject("dbConnections").getJsonObject(connectionName)); } } diff --git a/src/main/java/io/github/areebgillani/db/mysql/MySQLConnection.java b/src/main/java/io/github/areebgillani/db/mysql/MySQLConnection.java index e08c3be..4973020 100644 --- a/src/main/java/io/github/areebgillani/db/mysql/MySQLConnection.java +++ b/src/main/java/io/github/areebgillani/db/mysql/MySQLConnection.java @@ -11,12 +11,6 @@ public class MySQLConnection extends AbstractConnection { private static MySQLConnection instance; private DatabaseConfig config; - public MySQLConnection(String connectionName) { - this.config = DatabaseConfig.getInstance(Vertx.currentContext().config().getJsonObject("dbConnections").getJsonObject(connectionName)); - this.connectionOptions = getConnectionOption(); - this.poolOptions = getPoolOptions(); - this.client = getSQLPool(); - } public MySQLConnection(JsonObject config) { this.config = DatabaseConfig.getInstance(config); this.connectionOptions = getConnectionOption(); @@ -42,9 +36,6 @@ protected MySQLConnectOptions getConnectionOption() { .setReconnectAttempts(config.DB_RETRY_COUNT) .setReconnectInterval(config.DB_RETRY_INTERVAL): this.connectionOptions; } - public static MySQLConnection getInstance(String connectionName) { - return instance == null ? new MySQLConnection(connectionName) : instance; - } public static MySQLConnection getInstance(JsonObject config) { return instance == null ? new MySQLConnection(config) : instance; } diff --git a/src/main/java/io/github/areebgillani/db/mysql/MySQLRepository.java b/src/main/java/io/github/areebgillani/db/mysql/MySQLRepository.java index 68d0f1c..1701aaa 100644 --- a/src/main/java/io/github/areebgillani/db/mysql/MySQLRepository.java +++ b/src/main/java/io/github/areebgillani/db/mysql/MySQLRepository.java @@ -6,7 +6,7 @@ import io.vertx.core.json.JsonObject; public class MySQLRepository extends AbstractRepository { - public MySQLRepository(String connectionName){ - databaseConnection = MySQLConnection.getInstance(connectionName); + public MySQLRepository(String connectionName, JsonObject config){ + databaseConnection = MySQLConnection.getInstance(config.getJsonObject("dbConnections").getJsonObject(connectionName)); } } diff --git a/src/main/java/io/github/areebgillani/db/oracle/OracleConnection.java b/src/main/java/io/github/areebgillani/db/oracle/OracleConnection.java index 5ea7ecc..d3dc475 100644 --- a/src/main/java/io/github/areebgillani/db/oracle/OracleConnection.java +++ b/src/main/java/io/github/areebgillani/db/oracle/OracleConnection.java @@ -12,12 +12,6 @@ public class OracleConnection extends AbstractConnection { private static OracleConnection instance; private DatabaseConfig config; - public OracleConnection(String connectionName) { - this.config = DatabaseConfig.getInstance(Vertx.currentContext().config().getJsonObject("dbConnections").getJsonObject(connectionName)); - this.connectionOptions = getConnectionOption(); - this.poolOptions = getPoolOptions(); - this.client = getSQLPool(); - } public OracleConnection(JsonObject config) { this.config = DatabaseConfig.getInstance(config); this.connectionOptions = getConnectionOption(); @@ -41,11 +35,6 @@ protected OracleConnectOptions getConnectionOption() { .setUser(config.DB_USERNAME) .setPassword(config.DB_PASSWORD) : this.connectionOptions; } - - - public static OracleConnection getInstance(String connectionName) { - return instance == null ? new OracleConnection(connectionName) : instance; - } public static OracleConnection getInstance(JsonObject config) { return instance == null ? new OracleConnection(config) : instance; } diff --git a/src/main/java/io/github/areebgillani/db/oracle/OracleRepository.java b/src/main/java/io/github/areebgillani/db/oracle/OracleRepository.java index d0ee277..dcb8859 100644 --- a/src/main/java/io/github/areebgillani/db/oracle/OracleRepository.java +++ b/src/main/java/io/github/areebgillani/db/oracle/OracleRepository.java @@ -3,9 +3,10 @@ import io.github.areebgillani.db.utils.AbstractRepository; import io.vertx.core.Vertx; +import io.vertx.core.json.JsonObject; public class OracleRepository extends AbstractRepository { - public OracleRepository(String connectionName) { - databaseConnection = OracleConnection.getInstance(connectionName); + public OracleRepository(String connectionName, JsonObject config) { + databaseConnection = OracleConnection.getInstance(config.getJsonObject("dbConnections").getJsonObject(connectionName)); } } diff --git a/src/main/java/io/github/areebgillani/db/postgres/PostgresConnection.java b/src/main/java/io/github/areebgillani/db/postgres/PostgresConnection.java index fda18f8..61e66b5 100644 --- a/src/main/java/io/github/areebgillani/db/postgres/PostgresConnection.java +++ b/src/main/java/io/github/areebgillani/db/postgres/PostgresConnection.java @@ -12,13 +12,6 @@ public class PostgresConnection extends AbstractConnection { private static PostgresConnection instance; private DatabaseConfig config; - - public PostgresConnection(String connectionName) { - this.config = DatabaseConfig.getInstance(Vertx.currentContext().config().getJsonObject("dbConnections").getJsonObject(connectionName)); - this.connectionOptions = getConnectionOption(); - this.poolOptions = getPoolOptions(); - this.client = getSQLPool(); - } public PostgresConnection(JsonObject config) { this.config = DatabaseConfig.getInstance(config); this.connectionOptions = getConnectionOption(); @@ -44,10 +37,6 @@ protected PgConnectOptions getConnectionOption() { .setReconnectAttempts(config.DB_RETRY_COUNT) .setReconnectInterval(config.DB_RETRY_INTERVAL): this.connectionOptions; } - - public static PostgresConnection getInstance(String connectionName) { - return instance == null ? new PostgresConnection(connectionName) : instance; - } public static PostgresConnection getInstance(JsonObject config) { return instance == null ? new PostgresConnection(config) : instance; } diff --git a/src/main/java/io/github/areebgillani/db/postgres/PostgresRepository.java b/src/main/java/io/github/areebgillani/db/postgres/PostgresRepository.java index efccf08..f4fe7f6 100644 --- a/src/main/java/io/github/areebgillani/db/postgres/PostgresRepository.java +++ b/src/main/java/io/github/areebgillani/db/postgres/PostgresRepository.java @@ -7,7 +7,7 @@ public class PostgresRepository extends AbstractRepository { - public PostgresRepository(String connectionName) { - databaseConnection = PostgresConnection.getInstance(connectionName); + public PostgresRepository(String connectionName, JsonObject config) { + databaseConnection = PostgresConnection.getInstance(config.getJsonObject("dbConnections").getJsonObject(connectionName)); } }