[][src]Trait rocket_contrib::databases::Poolable

pub trait Poolable: Send + Sized + 'static {
    type Manager: ManageConnection<Connection = Self>;
    type Error;
    fn pool(config: DatabaseConfig) -> Result<Pool<Self::Manager>, Self::Error>;
}

Trait implemented by r2d2-based database adapters.

Provided Implementations

Implementations of Poolable are provided for the following types:

  • diesel::MysqlConnection
  • diesel::PgConnection
  • diesel::SqliteConnection
  • postgres::Connection
  • mysql::Conn
  • rusqlite::Connection
  • rusted_cypher::GraphClient
  • redis::Connection

Implementation Guide

As a r2d2-compatible database (or other resource) adapter provider, implementing Poolable in your own library will enable Rocket users to consume your adapter with its built-in connection pooling support.

Example

Consider a library foo with the following types:

  • foo::ConnectionManager, which implements r2d2::ManageConnection
  • foo::Connection, the Connection associated type of foo::ConnectionManager
  • foo::Error, errors resulting from manager instantiation

In order for Rocket to generate the required code to automatically provision a r2d2 connection pool into application state, the Poolable trait needs to be implemented for the connection type. The following example implements Poolable for foo::Connection:

use rocket_contrib::databases::{r2d2, DbError, DatabaseConfig, Poolable};
impl Poolable for foo::Connection {
    type Manager = foo::ConnectionManager;
    type Error = DbError<foo::Error>;

    fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
        let manager = foo::ConnectionManager::new(config.url)
            .map_err(DbError::Custom)?;

        r2d2::Pool::builder()
            .max_size(config.pool_size)
            .build(manager)
            .map_err(DbError::PoolError)
    }
}

In this example, ConnectionManager::new() method returns a foo::Error on failure. For convenience, the DbError enum is used to consolidate this error type and the r2d2::Error type that can result from r2d2::Pool::builder().

In the event that a connection manager isn't fallible (as is the case with Diesel's r2d2 connection manager, for instance), the associated error type for the Poolable implementation can simply be r2d2::Error as this is the only error that can be result. For more concrete example, consult Rocket's existing implementations of Poolable.

Associated Types

type Manager: ManageConnection<Connection = Self>

The associated connection manager for the given connection type.

type Error

The associated error type in the event that constructing the connection manager and/or the connection pool fails.

Loading content...

Required methods

fn pool(config: DatabaseConfig) -> Result<Pool<Self::Manager>, Self::Error>

Creates an r2d2 connection pool for Manager::Connection, returning the pool on success.

Loading content...

Implementations on Foreign Types

impl Poolable for SqliteConnection[src]

type Manager = ConnectionManager<SqliteConnection>

type Error = Error

impl Poolable for PgConnection[src]

type Manager = ConnectionManager<PgConnection>

type Error = Error

impl Poolable for MysqlConnection[src]

type Manager = ConnectionManager<MysqlConnection>

type Error = Error

impl Poolable for Connection[src]

impl Poolable for Conn[src]

impl Poolable for Connection[src]

type Manager = SqliteConnectionManager

type Error = Error

impl Poolable for GraphClient[src]

type Manager = CypherConnectionManager

type Error = Error

impl Poolable for Connection[src]

type Manager = RedisConnectionManager

type Error = DbError<RedisError>

impl Poolable for Database[src]

type Manager = MongodbConnectionManager

type Error = DbError<Error>

impl Poolable for Client[src]

type Manager = MemcacheConnectionManager

type Error = DbError<MemcacheError>

Loading content...

Implementors

Loading content...