Search code examples
mongodbspring-bootherokuproduction

A Spring boot + mongo Heroku deployment fails due to error creating the mongoTemplate bean


I have a jhipster-generated Spring Boot + mongo project that deploys locally with success, but deploying to Heroku with the ObjectRocket add-on is failing with this error:

[org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; 
nested exception is org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting for a server that matches 
com.mongodb.client.internal.MongoClientDelegate$1@4339e0de. Client view of cluster state is {type=REPLICA_SET, servers=[];
nested exception is com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@4339e0de.
Client view of cluster state is {type=REPLICA_SET, servers=[]

I believe that my config vars with my MONGODB_URI variable are correct because in a shell, I have connected to my mongo database with the same connection string from my config vars with success. This connection string, stored in MONGODB_URI, has the form mongodb://<USERNAME>:<PASSWORD@<host1>:<port1>,<host2>:<port2>,<host3>:<port3>/<DATABASE_NAME>?replicaSet=<replicaSetID>. This also means I have already set up my mongodb dbname, user, and password on the ObjectRocket side.

My procfile has the line --spring.data.mongodb.database=$(echo "$MONGODB_URI" | sed "s/^.*:[0-9]*\///g") referencing MONGODB_URI. My application-prod.yml config file has the entry:

spring:
  data:
    mongodb:
      uri: ${MONGODB_URI}?authMode=scram-sha1

Yet I am not succeeding with deployment because of the failed connection to my mongo database instance. Any help would be appreciated.

The error with more context from the stacktrace at this pastebin paste: https://pastebin.com/wNd3TxUL


Solution

  • I found the solution to this and wanted to post in case anyone else runs into this and is persuaded to go down an unhelpful rabbit hole of config files and cloud infrastructure. The problem arose specifically from using Mongo Atlas DB and having a Spring Bean that employed Mongobee to perform initial database configuration steps, such as setting the db name, template, and running some changesets to persist user authorities and a handful of initial users. Most importantly, this initial db config attempted to modify system.indexes, which MongoDB Atlas deprecated starting in version 3.0. From the MongoDB Atlas documentation:

    Starting in MongoDB 4.2, .system.indexes has been removed (access to the collection has been deprecated since 3.0). To list the inndexes [sic], use the listIndexes command instead.

    This makes this problem the same as the one encountered in these posts:

    MongoDB Atlas deprecates access to system.indexes Forbidden action on system.indexes, answered by same author, Yuriy Yunikov, who answered the above in very helpful detail Jhipster and mongobee discussion

    It should be noted that the latest versions of JHipster (e.g. at least as far back as release 7 on) have dropped Mongobee in favor of Mongock.