0

I'm using the MongoDB Kotlin Driver dependency. My project run with Spring Boot but I don't want to use the Spring Data MongoDB dependency (So as not to be dependant on Spring).

I use a bean to provide the db client :

    @Bean("mongoClient")
    fun mongoClient(
        dm: IDataManager,
        @Value("\${db.url:${DEFAULT_DATABASE_URL}}") ipAddress: String
    ): MongoClient {

        val codecRegistry = CodecRegistries.fromRegistries(
            CodecRegistries.fromProviders(
                KotlinSerializerCodecProvider(
                    dm.serializersModule + org.bson.codecs.kotlinx.defaultSerializersModule
                ),
            ),
            MongoClientSettings.getDefaultCodecRegistry(),
//            CodecRegistries.fromCodecs(ZonedDateTimeCodec(), URICodec()),
        )

        val settings = MongoClientSettings.builder()
            .applyConnectionString(ConnectionString(ipAddress))
            .codecRegistry(codecRegistry)
            .build()

        log.info("Mongo DB connection configured to $ipAddress.")
        return MongoClient.create(settings);
    }

My client is disconnected after a few hours, and I don't know how to reconnect it / keep the connection alive. I know it's possible to set a bigger timeout value, but I don't want to use this method (or only if it's the only way to do it). Is it possible to keep the connection alive ?

Edit :

There is my error :

2024-10-22T20:34:26.288Z  INFO 1 --- [null'}-db:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server db:27017

com.mongodb.MongoSocketReadTimeoutException: Timeout while receiving message
        at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.failed(AsynchronousChannelStream.java:265) ~[mongodb-driver-core-4.11.1.jar:na]
        at com.mongodb.internal.connection.AsynchronousChannelStream$BasicCompletionHandler.failed(AsynchronousChannelStream.java:235) ~[mongodb-driver-core-4.11.1.jar:na]
        at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:131) ~[na:na]
        at java.base/sun.nio.ch.Invoker$2.run(Invoker.java:221) ~[na:na]
        at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:113) ~[na:na]
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
        at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
Caused by: java.nio.channels.InterruptedByTimeoutException: null
        at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl$1.run(UnixAsynchronousSocketChannelImpl.java:473) ~[na:na]
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[na:na]
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
        at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
        ... 3 common frames omitted

My docker-compose file :

name: spring-project
services:
  reverse_proxy:
    image: nginx:1.26.0-alpine
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/certs:/etc/nginx/certs
      - ./nginx/html:/usr/share/nginx/html
    depends_on:
      - frontend
      - backend

  frontend:
    image: "frontend:1.0.0-SNAPSHOT"
    restart: always

  backend:
    image: "backend-server:1.0.0-SNAPSHOT"
    restart: always
    env_file:
      - ./.env
    environment:
      - spring.kafka.consumer.group-id=backend-admin
      - spring.ldap.urls=ldap://********
      - spring.ldap.domain=*********
      - spring.security.user.name=*******
      - spring.security.user.password=*******
    depends_on:
      - db

  db:
    image: mongodb/mongodb-community-server:7.0-ubuntu2204
    restart: always
    ports:
      - "27017:27017"
    volumes:
      - ./mongo_data:/data/db
3
  • I have not received any timeout ever with mongo.What is the symptom of the connection timeout? Where is your DB local, or in a remote hosting solution (where perhaps the connection to that place is liable to timeout)? Have you tried keep a Mongo Shell open to the same server and see if that times out? Commented Nov 5, 2024 at 9:24
  • @AndrewL I've just updated my message with the error log. This happens randomly and there are between 4 and 8 hours between timeouts. My database is running in docker with the image “mongodb/mongodb-community-server:7.0-ubuntu2204”, in the same stack as my Spring application. Commented Nov 5, 2024 at 12:00
  • I cannot answer the question, but I notice quite a bit of talk in places like the following links which suggest that the connection may need to be kept alive. (jira.mongodb.org/browse/DRIVERS-383 and medium.com/solving-mongodb-connection-drop-issues-by-tuning/…) I suppose I have been insulated by this by using a Spring driver. If you don't get answers from others, you could dip into the Spring Driver source code Commented Nov 5, 2024 at 17:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.