connection_pool 0.1.0 copy "connection_pool: ^0.1.0" to clipboard
connection_pool: ^0.1.0 copied to clipboard

outdatedDart 1 only

A generic connection pool

connection_pool #

Build Status

A very simple generic connection pool.

Example: How to build a MongoDB connection pool, and use it in a Redstone.dart app

import 'package:redstone/server.dart' as app;
import 'package:connection_pool/connection_pool.dart';
import 'package:mongo_dart/mongo_dart.dart';

/**
 * A MongoDB connection pool
 *
 */
class MongoDbPool extends ConnectionPool<Db> {
  
  String uri;
  
  MongoDbPool(String this.uri, int poolSize) : super(poolSize);
  
  @override
  void closeConnection(Db conn) {
    conn.close();
  }

  @override
  Future<Db> openNewConnection() {
    var conn = new Db(uri);
    return conn.open().then((_) => conn);
  }
}

/**
 * Retrieve and release a connection from the pool. 
 */
app.Interceptor(r'/.*')
dbInterceptor(MongoDbPool pool) {
 
  //get a connection
  db.getConnection().then((managedConnection) {

    //save the connection in the attributes map
    app.request.attributes["conn"] = managedConnection.conn;

    app.chain.next(() {
      if (app.chain.error is ConnectionException) {
        //if a connection is lost, mark it as invalid, so the pool can reopen it
        //in the next request
        db.releaseConnection(managedConnection, markAsInvalid: true);
      } else {
        //release the connection
        db.releaseConnection(managedConnection);
      }
    });

  });
}

//To use a connection, just retrieve it from the attributes map
@app.Route('/service')
service(@app.Attr() Db conn) {
  ...
}


main() {

  app.setupConsoleLog();
  
  //create a connection pool
  var mongodbUri = "mongodb://localhost/database";
  var poolSize = 3;

  app.addModule(new Module()
    ..bind(MongoDbPool, toValue: new MongoDbPool(mongoDbUri, poolSize)));

  app.start(address: cfg["host"], port: cfg["port"]);
}
0
likes
0
pub points
21%
popularity

Publisher

unverified uploader

A generic connection pool

Homepage

License

unknown (LICENSE)

More

Packages that depend on connection_pool