Skip to content

Commit

Permalink
Config object not found issue fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Areeb-Gillani committed Sep 4, 2023
1 parent f175ec9 commit a0ab39c
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
public class ConnectionManager<T> {
private static final HashMap<String, Object> dbConnectionMap = new HashMap<>();

private static <T extends AbstractConnection> Object createDatabaseConnection(String connectionName){
JsonObject conf = Vertx.currentContext().owner().getOrCreateContext().config().getJsonObject("dbConnections").getJsonObject(connectionName);
private static <T extends AbstractConnection> 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);
case ORACLE -> OracleConnection.getInstance(conf);
case MSSQL -> MSSQLConnection.getInstance(conf);
};
}
public static <T extends AbstractConnection> T getDBConnection(String connectionName){
return (T) dbConnectionMap.computeIfAbsent(connectionName, ConnectionManager::createDatabaseConnection);
public static <T extends AbstractConnection> T getDBConnection(String connectionName, JsonObject config){
return (T) dbConnectionMap.computeIfAbsent(connectionName, cn->createDatabaseConnection(cn,config));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> extends AbstractRepository<T> {
public CrudRepository(String connectionName){
databaseConnection = ConnectionManager.getDBConnection(connectionName);
public CrudRepository(String connectionName, JsonObject config){
databaseConnection = ConnectionManager.getDBConnection(connectionName, config);
}
}
10 changes: 0 additions & 10 deletions src/main/java/io/github/areebgillani/db/mssql/MSSQLConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
public class MSSQLConnection extends AbstractConnection<MySQLConnectOptions, MySQLPool> {
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();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import io.vertx.core.json.JsonObject;

public class MSSQLRepository<T> extends AbstractRepository<T> {
public MSSQLRepository(String connectionName) {
databaseConnection = MSSQLConnection.getInstance(connectionName);
public MSSQLRepository(String connectionName, JsonObject config) {
databaseConnection = MSSQLConnection.getInstance(config.getJsonObject("dbConnections").getJsonObject(connectionName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
public class MySQLConnection extends AbstractConnection<MySQLConnectOptions, MySQLPool> {
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();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import io.vertx.core.json.JsonObject;

public class MySQLRepository<T> extends AbstractRepository<T> {
public MySQLRepository(String connectionName){
databaseConnection = MySQLConnection.getInstance(connectionName);
public MySQLRepository(String connectionName, JsonObject config){
databaseConnection = MySQLConnection.getInstance(config.getJsonObject("dbConnections").getJsonObject(connectionName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
public class OracleConnection extends AbstractConnection<OracleConnectOptions, OraclePool> {
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();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> extends AbstractRepository<T> {
public OracleRepository(String connectionName) {
databaseConnection = OracleConnection.getInstance(connectionName);
public OracleRepository(String connectionName, JsonObject config) {
databaseConnection = OracleConnection.getInstance(config.getJsonObject("dbConnections").getJsonObject(connectionName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
public class PostgresConnection extends AbstractConnection<PgConnectOptions, PgPool> {
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();
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class PostgresRepository<T> extends AbstractRepository<T> {

public PostgresRepository(String connectionName) {
databaseConnection = PostgresConnection.getInstance(connectionName);
public PostgresRepository(String connectionName, JsonObject config) {
databaseConnection = PostgresConnection.getInstance(config.getJsonObject("dbConnections").getJsonObject(connectionName));
}
}

0 comments on commit a0ab39c

Please sign in to comment.