const classafMongo::ConnectionManagerPooled

sys::Obj
  afMongo::ConnectionManagerPooled : afMongo::ConnectionManager

Manages a pool of connections.

Connections are created on-demand and a total of minPoolSize are kept in a pool when idle. Once the pool is exhausted, any operation requiring a connection will block for (at most) waitQueueTimeout waiting for an available connection.

This connection manager is created with the standard Mongo Connection URL in the format:

mongodb://[username:password@]host[:port][/[database][?options]]

Examples:

mongodb://localhost:27017
mongodb://username:password@example1.com/puppies?maxPoolSize=50

If connecting to a replica set then multiple hosts (with optional ports) may be specified:

mongodb://db1.example.net,db2.example.net:2500/?connectTimeoutMS=30000

On startup() the hosts are queried to find the primary / master node. All read and write operations are performed on the primary node.

When a connection to the master node is lost, all hosts are re-queried to find the new master.

Note this connection manager is safe for multi-threaded / web-application use.

connectTimeout

Source

const Duration? connectTimeout

If specified, this is the time to attempt a connection before timing out. If null (the default) then a system timeout is used.

Set via the connectTimeoutMS connection string option.

mongodb://example.com/puppies?connectTimeoutMS=2500

Equates to inet::SocketOptions.connectTimeout.

connectionUrl

Source

const Uri connectionUrl

The original URL this ConnectionManager was configured with. May contain authentication details.

mongodb://username:password@example1.com/puppies?maxPoolSize=50
defaultDatabase

Source

const Str? defaultDatabase

The default database connections are authenticated against.

Set via the connectionUrl.

defaultPassword

Source

const Str? defaultPassword

The default password connections are authenticated with.

Set via the connectionUrl.

defaultUsername

Source

const Str? defaultUsername

The default username connections are authenticated with.

Set via the connectionUrl.

emptyPool

Source

Void emptyPool()

(Advanced) Closes all un-leased connections in the pool, and flags all leased connections to close themselves after use. Use to migrate connections to new host / master.

huntThePrimary

Source

Void huntThePrimary()

(Advanced) Searches the replica set for the Master node and instructs all new connections to connect to it. Throws MongoErr if a primary can not be found.

This method should be followed with a call to emptyPool().

leaseConnection

Source

virtual override Obj? leaseConnection(|Connection->Obj? c)

Makes a connection available to the given function. What ever is returned from the func is returned from the method.

If all connections are currently in use, a truncated binary exponential backoff algorithm is used to wait for one to become free. If, while waiting, the duration specified in waitQueueTimeout expires then a MongoErr is thrown.

All leased connections are authenticated against the default credentials.

makeFromUrl

Source

new makeFromUrl(ActorPool actorPool, Uri connectionUrl, |This? f := null)

Create a ConnectionManager from a Mongo Connection URL. If user credentials are supplied, they are used as default authentication for each connection.

conMgr := ConnectionManagerPooled(ActorPool(), `mongodb://localhost:27017`)

The following URL options are supported:

URL examples:

  • mongodb://username:password@example1.com/database?maxPoolSize=50
  • mongodb://example2.com?minPoolSize=10&maxPoolSize=50

@see http://docs.mongodb.org/manual/reference/connection-string/

maxPoolSize

Source

const Int maxPoolSize := 10

The maximum number of database connections this pool is allowed open. This is the maximum number of concurrent users you expect your application to have.

Set via the maxPoolSize connection string option. Defaults to 10.

mongodb://example.com/puppies?maxPoolSize=10
minPoolSize

Source

const Int minPoolSize := 1

The minimum number of database connections this pool should keep open. They are initially created during startup().

Set via the minPoolSize connection string option. Defaults to 1.

mongodb://example.com/puppies?minPoolSize=50
mongoUrl

Source

virtual override Uri? mongoUrl()

The host name of the MongoDB server this ConnectionManager connects to. When connecting to replica sets, this will indicate the primary.

This value is unavailable (returns null) until startup() is called.

noOfConnectionsInPool

Source

Int noOfConnectionsInPool()

Returns the number of connections currently in the pool.

noOfConnectionsInUse

Source

Int noOfConnectionsInUse()

Returns the number of pooled connections currently in use.

shutdown

Source

virtual override ConnectionManager shutdown()

Closes all connections. Initially waits for shutdownTimeout for connections to finish what they're doing before they're closed. After that, all open connections are forcibly closed regardless of whether they're in use or not.

shutdownTimeout

Source

const Duration? shutdownTimeout := 2sec

When the connection pool is shutting down, this is the amount of time to wait for all connections for close before they are forcibly closed.

Defaults to 2sec.

socketTimeout

Source

const Duration? socketTimeout

If specified, this is the time to attempt a send or receive on a socket before the attempt times out. null (the default) indicates an infinite timeout.

Set via the socketTimeoutMS connection string option.

mongodb://example.com/puppies?socketTimeoutMS=2500

Equates to inet::SocketOptions.receiveTimeout.

startup

Source

virtual override ConnectionManager startup()

Creates the initial pool and establishes minPoolSize connections with the server.

If a connection URL to a replica set is given (a connection URL with multiple hosts) then the hosts are queried to find the primary. The primary is currently used for all read and write operations.

waitQueueTimeout

Source

const Duration waitQueueTimeout := 15sec

The maximum time a thread can wait for a connection to become available.

Set via the maxPoolSize connection string option. Defaults to 15 seconds.

mongodb://example.com/puppies?waitQueueTimeoutMS=10
writeConcern

Source

virtual const override Str:Obj? writeConcern := ...

The default write concern for all write operations. Set by specifying the w, wtimeoutMS and journal connection string options.

Defaults to ["w": 1, "wtimeout": 0, "j": false]

  • write operations are acknowledged,
  • write operations never time out,
  • write operations need not be committed to the journal.