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