mongo_pool 1.3.1 mongo_pool: ^1.3.1 copied to clipboard
A MongoDB connection pool for Dart. Using this package, your application will open as many database connections as you specify as soon as it runs.
MongoDB Connection Pooling - Mongo Pool #
New features in 1.3.1 #
- Connection leak detection added
"Connection leak" refers to the situation where an application fails to properly release or close resources (typically connections, memory, etc.), causing them to gradually accumulate and exhaust. These resources continue to be used as the application runs, depleting over time. This can lead to performance degradation, increased memory usage, and even system crashes.
How to migrate from 1.2.0 to 1.3.0 #
- To use MongoDb PoolService you need to create a new Mongo Pool Configuration object. You can use the following code snippet to create a Mongo Pool Configuration object.
final MongoDbPoolService poolService = MongoDbPoolService(
const MongoPoolConfiguration(
/// [maxLifetimeMilliseconds] is the maximum lifetime of a connection in the pool.
/// If the connection is not used within the specified time, it is closed.
/// The default value is 30000 milliseconds (30 seconds)
/// This feature cannot be disabled
maxLifetimeMilliseconds: 90000,
/// [leakDetectionThreshold] is the threshold for connection leak detection.
/// If the connection is not released within the specified time, it is
/// considered as a leak.
/// It won't work if no value is set. It is recommended to set a value
leakDetectionThreshold: 10000,
uriString: 'mongodb://localhost:27017/my_database',
poolSize: 4,
),
);
- You can use the following code snippet to open the pool.
await openDbPool(poolService);
Future<void> openDbPool(MongoDbPoolService service) async {
try {
await service.open();
} on Exception catch (e) {
/// handle the exception here
print(e);
}
}
- You can use the following code snippet to get the instance of the pool.
final MongoDbPoolService poolService = MongoDbPoolService.getInstance();
Introduction #
This package is a simple connection pooling for MongoDB. It is based on the mongo_dart package.
Features #
- Connection pool size configuration
- Automatic connection pool expansion
- Instance where you can access the connection from the pool
- Connection lifetime configuration [NEW]
- Connection leak detection [NEW]
Getting started #
Using this package, your application will open as many database connections as you specify as soon as it runs. You can open, close and change the number of these links at any time.
Usage #
With mongodb package, you can use the MongoDbPoolService
class to create a pool of connections.
First, you need to create a MongoDbPoolService
instance. You can do this by passing the number of
connections you want
Next, you need to open the pool. You can do this by calling the open()
method. If you want to
close the pool, you can
call the close()
method.
MongoDbPoolService
class has a getInstance()
method. You can use this method to get the instance
of the pool.
You can use this instance to access the pool from anywhere in the project.
A simple usage example:
import 'package:mongo_pool/mongo_pool.dart';
Future<void> main() async {
/// Create a pool of 5 connections
final MongoDbPoolService poolService = MongoDbPoolService(
const MongoPoolConfiguration(
maxLifetimeMilliseconds: 90000,
leakDetectionThreshold: 10000,
uriString: 'mongodb://localhost:27017/my_database',
poolSize: 4,
),
);
/// Open the pool
await openDbPool(poolService);
/// Get a connection from pool
final Db connection = await poolService.acquire();
// Database operations
final DbCollection collection = connection.collection('my_collection');
final List<Map<String, dynamic>> result = await collection.find().toList();
result;
// Connection release for other operations
unawaited(poolService.release(connection));
// Pool close
await poolService.close();
}
Future<void> openDbPool(MongoDbPoolService service) async {
try {
await service.open();
} on Exception catch (e) {
/// handle the exception here
print(e);
}
}
class OtherClass {
OtherClass();
Future<void> openDbPool() async {
/// Get the instance of the pool
final MongoDbPoolService poolService = MongoDbPoolService.getInstance();
final Db connection = await poolService.acquire();
// Database operations
final DbCollection collection = connection.collection('my_collection');
final List<Map<String, dynamic>> result = await collection.find().toList();
result;
// Connection release for other operations
poolService.release(connection);
// Pool close
await poolService.close();
}
}
Testing #
To run the tests, you need to have a MongoDB instance running on your machine. You can use the following command to start a MongoDB instance using Docker:
docker run -d -p 27017:27017 --name mongo mongo
Problems Solved #
- Mongodb srv connection problem solved
- Fixed a bug that prevented a new connection from being created when no connection remained
- Unable to connect to MongoDB using SSL/TLS fixed