Crate mobc[][src]

Expand description

A generic connection pool with async/await support.

Opening a new database connection every time one is needed is both inefficient and can lead to resource exhaustion under high traffic conditions. A connection pool maintains a set of open connections to a database, handing them out for repeated use.

mobc is agnostic to the connection type it is managing. Implementors of the Manager trait provide the database-specific logic to create and check the health of connections.


Using an imaginary “foodb” database.

use mobc::{Manager, Pool, async_trait};

struct FooError;

struct FooConnection;

impl FooConnection {
   async fn query(&self) -> String {

struct FooManager;

impl Manager for FooManager {
   type Connection = FooConnection;
   type Error = FooError;

   async fn connect(&self) -> Result<Self::Connection, Self::Error> {

   async fn check(&self, conn: Self::Connection) -> Result<Self::Connection, Self::Error> {

async fn main() {
   let pool = Pool::builder().max_open(15).build(FooManager);
   let num: usize = 10000;
   let (tx, mut rx) = tokio::sync::mpsc::channel::<()>(16);

   for _ in 0..num {
       let pool = pool.clone();
       let mut tx = tx.clone();
       tokio::spawn(async move {
           let conn = pool.get().await.unwrap();
           let name = conn.query().await;
           assert_eq!(name, "nori".to_string());

   for _ in 0..num {


A batteries included runtime for applications using mobc. Mobc does not implement runtime, it simply exports runtime.


A builder for a connection pool.

A smart pointer wrapping a connection.

A generic connection pool.

Information about the state of a Pool.


The error type returned by methods in this crate.


A trait which provides connection-specific functionality.


Spawns a new asynchronous task.

Attribute Macros