user8363477
user8363477

Reputation: 695

Check MongoDB connection using MongoTemplate in Spring Boot

I am working on Spring Kafka Mongo Integration for insert/update operations and using mongotemplate to perform these actions. I want to know is there any way to check mongodb connection is up/down so that in case my db goes down I want to commit kafka offset manually. Currently, all the db configurations are provided in application.properties file

Upvotes: 3

Views: 5341

Answers (1)

Sampisa
Sampisa

Reputation: 1603

What about something like that?

@Autowired
private MongoTemplate mt;

public String ping()
{
    DBObject ping = new BasicDBObject("ping", "1");
    try {
        CommandResult answer = mt.getDb().command(ping);
        return answer.getErrorMessage();
    } catch (Exception e) {
        return e.getMessage();
    }

Upvotes: 4

Related Questions