Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

mongodb - Set Mongo Timeout in Spring Boot

I am using spring boot web application which connects to mongo db which is working out of the box. I just use the following properties:

spring.data.mongodb.host=myHost
spring.data.mongodb.port=27017
spring.data.mongodb.database=myDatabase
spring.data.mongo.repositories.enabled=true
spring.data.mongodb.username=myUser
spring.data.mongodb.password=myPassword

The default timeout to MongoDB is 10 seconds. I would like to configure the timeout. I tried doing spring.data.mongodb.socketTimeout=2 or spring.data.mongodb.connectionTimeout=2

None of the properties work. Is it something that I can specify in the properties and the Spring framework will take care of it or can someone give example of doing it by declaring the Bean.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To follow your theme, this would be the best to answer your question. Looks like you would like to set a timeout in your application.properties file.

In order to do so:

Instead of

spring.data.mongodb.host=myHost
spring.data.mongodb.port=27017
spring.data.mongodb.database=myDatabase

spring.data.mongodb.username=myUser
spring.data.mongodb.password=myPassword

Try this

spring.data.mongodb.uri=mongodb://myUser:myPassword@myHost:27017,myHost:27017/myDatabase?serverSelectionTimeoutMS=2000&connectTimeoutMS=2000
spring.data.mongodb.database=myDatabase
spring.data.mongo.repositories.enabled=true

Modify time in milliseconds to desired time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...