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
90 views
in Technique[技术] by (71.8m points)

Connection management in MongoDB using PHP

I am using PHP 7.2 on a website hosted on Amazon. I have a code similar to this one that writes a record in the MongoDB:

Database connection class:

class Database {
    private static $instance;
    private $managerMongoDB;
    
    private function __construct() {
        #Singleton private constructor
    }

    public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new Database();
        }
        return self::$instance;
    }

    function writeMongo($collection, $record) {
        if (empty($this->managerMongoDB)) {
            $this->managerMongoDB = new MongoDBDriverManager(DB_MONGO_HOST ? DB_MONGO_HOST : null);
        }
        $writeConcern = new MongoDBDriverWriteConcern(MongoDBDriverWriteConcern::MAJORITY, 1000);
        $bulk = new MongoDBDriverBulkWrite();
        $bulk->insert($record);
        try {
            $result = $this->managerMongoDB->executeBulkWrite(
                DB_MONGO_NAME . '.' . $collection, $bulk, $writeConcern
            );
        } catch (MongoDBDriverExceptionBulkWriteException $e) {
            // Not important
        } catch (MongoDBDriverExceptionException $e) {
            // Not important
        }
        return $result->getInsertedCount() > 0;
    }
}

Execution:

Database::getInstance()->writeMongo($tableName, $dataForMongo);

The script is working as intended and the records are added in MongoDB.

The problem is that connections are not being closed at all and once there are 500 inserts (500 is the limit of connections in MongoDB on our server) it stops working. If we restart php-fpm the connections are also reset and we can insert 500 more records.

The connection is reused during the request, but we have requests coming from 100s of actual customers.

As far as I can see there is no way to manually close the connections. Is there something I'm doing wrong? Is there some configuration that needs to be done on the driver? I tried setting socketTimeoutMS=1000&wTimeoutMS=1000&connectTimeoutMS=1000 in the connection string but the connections keep staying alive.

question from:https://stackoverflow.com/questions/66061989/connection-management-in-mongodb-using-php

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

1 Answer

0 votes
by (71.8m points)

You are creating a client instance every time the function is invoked, and never closing it, which would produce the behavior you are seeing.

If you want to create the client instance in the function, close it in the same function.

Alternatively create the client instance once for the entire script and use the same instance in all of the operations done by that script.


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

...