From cdb3d3dd5fd085f57ff031560520290b8f36214b Mon Sep 17 00:00:00 2001 From: Vitalii Makarchenkov Date: Fri, 29 Mar 2024 12:50:18 +0200 Subject: [PATCH] Implement method to send ping sygnal to DB server for connect command --- .../database/MongoLiquibaseDatabase.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java b/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java index cf1e85fd..cf4bda1c 100644 --- a/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java +++ b/src/main/java/liquibase/ext/mongodb/database/MongoLiquibaseDatabase.java @@ -20,10 +20,14 @@ * #L% */ +import com.mongodb.MongoException; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoDatabase; import liquibase.CatalogAndSchema; import liquibase.Scope; import liquibase.changelog.ChangeLogHistoryServiceFactory; +import liquibase.database.DatabaseConnection; +import liquibase.exception.DatabaseException; import liquibase.exception.LiquibaseException; import liquibase.executor.Executor; import liquibase.executor.ExecutorService; @@ -32,6 +36,9 @@ import liquibase.nosql.database.AbstractNoSqlDatabase; import lombok.NoArgsConstructor; import lombok.Setter; +import org.bson.BsonDocument; +import org.bson.BsonInt64; +import org.bson.conversions.Bson; import static liquibase.nosql.executor.NoSqlExecutor.EXECUTOR_NAME; @@ -118,5 +125,19 @@ public Boolean getSupportsValidator() { return MongoConfiguration.SUPPORTS_VALIDATOR.getCurrentValue(); } + @Override + public void checkDatabaseConnection() throws DatabaseException { + DatabaseConnection connection = getConnection(); + try { + if (connection instanceof MongoConnection) { + MongoClient mongoClient = ((MongoConnection) connection).getMongoClient(); + Bson command = new BsonDocument("ping", new BsonInt64(1)); + MongoDatabase adminDb = mongoClient.getDatabase(ADMIN_DATABASE_NAME); + adminDb.runCommand(command); + } + } catch (MongoException e) { + throw new DatabaseException(e); + } + } }