以面向对象的方式管理 Java 中的 Mongodb 连接 [英] Managing Mongodb connections in Java as Object Oriented

查看:37
本文介绍了以面向对象的方式管理 Java 中的 Mongodb 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用多个类正确管理 mongo 连接?

How Do I properly Manage mongo connections using multiple classes ?

例如,我有 4 个管理 4 个集合的类.

For example I have 4 classes that manages 4 collections.

Collection1.classCollection2.class等等.

Collection1.class Collection2.class Etc..

我所做的是在每个类中创建一个连接和关闭方法,这会减慢应用程序中某些事务的连接速度

What I do is creating a connect and close method in each class which by the time slows the connection of some transactions in the app

除了创建每个类的对象并分别连接每个类之外,将应用程序连接到数据库一次并开始使用所有类实例的最佳方法是什么?

What would be the best way to connect the app to db once and start using all the classes instances other than creating object of each class and connecting each one separately ?

推荐答案

您是对的,每个类(代表一个 MongoDB 集合)不应该管理自己的数据库连接.相反,您应该将数据库连接传递到类中 - 通常在构造函数中.像这样:

You are correct that each class (representing a MongoDB collection) should not be managing its own connection to the database. Rather you should be passing the database connection into the class - generally in the constructor. Something like this:

class Animal {
    private String species;
    private String name;
    private int age;

    public Animal(DBObject dbObject) { ... }
}

class AnimalCollection {
    private final DBCollection collection;        

    public AnimalCollection(Database database) {
        collection = database.getCollection("animals");
    }

    public List<Animal> getAll() {
        List<Animal> animals 
        try (DBCursor cursor = collection.find(query)) {
            while (cursor.hasNext()) {
                animals.add(new Animal(cursor.next());
            }
        }
        return animals;
    }
}

您创建所有集合的代码应该获取 MongoClient,连接到数据库并在退出时管理关闭连接.这样您就可以管理一个连接.

Your code to create all the collections should get the MongoClient, connect to the DB and managing closing the connection at exit. That way you have a single connection you manage.

因此管理集合的类可能如下所示:

So the class to manage the collections might look like:

class CollectionManager implements AutoCloseable {
    private final Database database;
    private final AnimalCollection animals;

    public CollectionManager(MongoClient client) {
        database = client.getDB("Zoo");
        animals = new AnimalCollection(database);
    }

    @Override
    public void close() {
        database.close();
    }
}

让此类扩展 AutoCloseable 的原因是当您退出 try-with-resources 块时会自动调用 close.这将使您的代码更易于阅读且更安全.

The reason to have this class extend AutoCloseable is that close is called automatically when you exit a try-with-resources block. That'll make your code easier to read and safer.

这种方法还有一个很大的优势.您可以通过在构造函数中传递模拟的 Database 来对您的类进行单元测试,并测试响应各种 DB 输出的行为,而无需实际的 DB 中包含任何数据.

This approach has another big advantage. You can unit test your classes by passing a mocked Database in the constructor and test behaviour in response to various DB outputs without needing an actual DB with any data in it.

这篇关于以面向对象的方式管理 Java 中的 Mongodb 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆