0

I am implementing mongodb in java. Whenever my application starts new connection pool for mongoDB is created. Is there any way I can destroy that connection pool and create a new one, without restarting my application. I know that if any argument is change in mongoURI, a connection pool is reinitialized, but I want to know if there is anyway we can do that without making any changes in URI arguments.By main goal is to destroy the connection pool and create new connection pool ! This connection pool is created by MongoClient bean. Therefore I wanted to destroy and recreate the MongoClient Bean.

<bean id="monURI" class="com.mongodb.MongoClientURI"> <constructor-arg name="uri" value="${MONGO_URI}"/> </bean>

<bean id="mongoC" class="com.mongodb.MongoClient"> <constructor-arg ref="monURI"/> </bean>

ArielGro
  • 795
  • 1
  • 11
  • 24
Ravi Bhanushali
  • 145
  • 2
  • 9

1 Answers1

3

Is it possible for you to share some code so we can be more helpful to your specific situation?

Where I work, we are using com.mongodb.MongoClient. If you are using it as well you can make a call to mongoClient.close() before destroying the connection manger component (working with Spring, so we call the close() in the @PreDestroy method of the component)

------- EDIT -------

Following our comments on this answer I would go with either of these approaches:

  1. Wrap the MongoClient with a class of your own that holds a MongoClient instance. This class will expose a method (let's call it resetConnectionPool), and inside that method you will call mongoClient.close() and mongoClient = new MongoClient().
    You might have to @Autowire the MongoClientURI bean to use it inside the class you create. Something along the line of this class:

;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.UnknownHostException;

@Component
public class MongoClientWrapper {
    @Autowired
    private MongoClientURI mongoClientURI;

    private MongoClient mongoClient;

    @PostConstruct
    public void init() {
        mongoClient = getNewMongoClientInstance();
    }

    @PreDestroy
    public void beforeTearDown() {
        mongoClient.close();
    }

    public void resetConnectionPool() {
        mongoClient.close();
        mongoClient = getNewMongoClientInstance();
    }

    private MongoClient getNewMongoClientInstance() {
        MongoClient client = null;
        try {
            client = new MongoClient(mongoClientURI.getURI());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } finally {
            return client;
        }
    }
}
  1. Reconsider the scope of the MongoClient bean. Maybe instead of singleton it should be per request or per http session? Have a look at these links for more on bean scopes:
  2. According to this javadoc: https://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/MongoClientURI.html you can add options to the URI. You can add an option that no one uses but contains the current timestamp. If you want to reset the pool, change only that timestamp, and that "changes" the URI and thus resets your connection pool. Something along the line of:
    mongodb://[username:password@]host1:port1/dbName?_=1534228866003

----- END EDIT -----

ArielGro
  • 795
  • 1
  • 11
  • 24
  • Please share the code as an edit to the question, and try to provide as much code as possible for us to try and reproduce and try to answer.... – ArielGro Aug 13 '18 at 13:10
  • Thank you for the suggestion, I have added few code in the question and also have explained the reason, for doing it ! – Ravi Bhanushali Aug 13 '18 at 13:19
  • Thanks for adding the code.. Can you add the code that uses the bean or explain how it is used? Is it per request is it a singleton, etc... – ArielGro Aug 13 '18 at 13:42
  • During the initialization, the bean of MongoClient is created, which also creates the connection pool with default value '100'. Now I want to destroy this connection pool and create a new connection pool, without restarting the mongodb or my application . The bean is singleton . I am using simple com.mongodb.MogoClient and com.mongodb.MongoCleintURI – Ravi Bhanushali Aug 14 '18 at 05:20
  • I have edited my answer and added 3 approaches I think might help you get what you want to do. I'm not sure I understand the full picture (for example why would you need to reset the connection pool in the first place? where do you use the MongoClient bean you create and in which context or scope?), but I hope one of the options I added will do the trick – ArielGro Aug 14 '18 at 06:45
  • Thanks alott for your efforts ! I was aware of the 3rd point but the first two will surely do some trick ... @ArielGro – Ravi Bhanushali Aug 14 '18 at 11:50
  • Sure, I will let you know about the results ! @ArielGro – Ravi Bhanushali Aug 14 '18 at 12:39
  • @ArielGo Thank you so much for your help, I implemented it using the first option and it worked well ! – Ravi Bhanushali Aug 20 '18 at 11:32
  • @RaviBhanushali - Nice!! Glad to hear it worked!! Good luck – ArielGro Aug 20 '18 at 12:06