Failed to connect to Java Mongodb

I have a java application that uses about 3,000 repeat threads that always work and process items from the queue. I use MongoDB to store data, and every time I run it, it works fine for about 40 minutes, after which Mongo DB Object starts to return a Nullpointer exception for queries. At first I suspected that this might be due to connection loss, but, as you can see in the Google monitoring graph, the connections are still open, but the number of Mongo requests is significantly reduced. Is something missing here?

My MongoDB class is as follows:

public class MongoDB {

    private static MongoClient mongoClient; 

    private static MongoClient initMongoClient() {
        ServerAddress server = new ServerAddress("X.X.X.X");

        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.threadsAllowedToBlockForConnectionMultiplier(50000);
        builder.socketKeepAlive(true);
        builder.connectionsPerHost(10000);
        builder.minConnectionsPerHost(2500);


        MongoClientOptions options = builder.build();
        MongoClient mc = new MongoClient(server, options);
        mongoClient = mc;
        return mc;
    }

    public static MongoClient getMongoClient() {
        if(mongoClient == null) {
            mongoClient = initMongoClient();
        }

        return mongoClient;
    }

    public static DB getDb() {
        DB db;
        MongoClient mc;
        try {
            mc = getMongoClient();
            mc.getDatabaseNames();
        } catch(MongoException e) {
            mc = initMongoClient();
        }
        db = mc.getDB("tina");
        return db;
    }

}

enter image description here

+4
source share
1 answer

! https://github.com/mongodb/morphia

factory, Mongo Morphia Datastore. Datastore MongoDB.

public class DatastoreFactory {
    private static Datastore ds;

    public static Datastore getDatastore() {
        //Lazy load the datastore
        if(ds == null) {
            try {
                Morphia morphia = new Morphia();
                ds = morphia.createDatastore(
                    new MongoClient("server", port, "database"));
                //... Other datastore options
            } catch(Exception e) {
               // Handle it
            }
    }
    return ds;
}

, MongoDB, Datastore factory

Datastore ds = DatastoreFactory.getDatastore();

CDI , ,

@Singleton
public class DatastoreFactory {
    private Datastore ds;

    @Produces
    public Datastore getDatastore() {
        //Lazy load the datastore
        if(ds == null) {
            try {
                Morphia morphia = new Morphia();
                ds = morphia.createDatastore(
                    new MongoClient("server", port, "database"));
                //... Other datastore options
            } catch(Exception e) {
               // Handle it
            }
    }
    return ds;
}

:

@Inject
Datastore ds;

BONUS

MongoDB, (DAO) , Morphia Datastore. DAO , (, , , ). , MongoDB, DAO, !

+1

Source: https://habr.com/ru/post/1599523/


All Articles