pub struct Pool { /* private fields */ }Expand description
Pool serves to provide you with a PooledConn’s.
However you can prepare statements directly on Pool without
invoking Pool::get_conn.
Pool will hold at least min connections and will create as many as max
connections with possible overhead of one connection per alive thread.
Example of multithreaded Pool usage:
use mysql::{Pool, Opts};
use std::thread;
fn get_opts() -> Opts {
      // ...
}
let opts = get_opts();
let pool = Pool::new(opts).unwrap();
let mut threads = Vec::new();
for _ in 0..100 {
    let pool = pool.clone();
    threads.push(thread::spawn(move || {
        let mut result = pool.prep_exec("SELECT 1", ()).unwrap();
        assert_eq!(result.next().unwrap().unwrap().unwrap(), vec![1.into()]);
    }));
}
for t in threads.into_iter() {
    assert!(t.join().is_ok());
}For more info on how to work with mysql connection please look at
PooledConn documentation.
Implementations§
source§impl Pool
 
impl Pool
sourcepub fn new<T: Into<Opts>>(opts: T) -> MyResult<Pool>
 
pub fn new<T: Into<Opts>>(opts: T) -> MyResult<Pool>
Creates new pool with min = 10 and max = 100.
sourcepub fn new_manual<T: Into<Opts>>(
    min: usize,
    max: usize,
    opts: T
) -> MyResult<Pool>
 
pub fn new_manual<T: Into<Opts>>(
    min: usize,
    max: usize,
    opts: T
) -> MyResult<Pool>
Same as new but you can set min and max.
sourcepub fn use_cache(&mut self, use_cache: bool)
 
pub fn use_cache(&mut self, use_cache: bool)
A way to turn off searching for cached statement (on by default).
If turned on, then calls to Pool::{prepare, prep_exec, first_exec} will search for cached
statement through all connections in the pool. Useless if the value of the stmt_cache_size
option is 0.
sourcepub fn check_health(&mut self, check_health: bool)
 
pub fn check_health(&mut self, check_health: bool)
A way to turn off connection health check on each call to get_conn and prepare
(prep_exec is not affected) (on by default).
sourcepub fn get_conn(&self) -> MyResult<PooledConn>
 
pub fn get_conn(&self) -> MyResult<PooledConn>
Gives you a PooledConn.
Pool will check that connection is alive via
Conn::ping and will
call Conn::reset if
necessary.
sourcepub fn try_get_conn(&self, timeout_ms: u32) -> MyResult<PooledConn>
 
pub fn try_get_conn(&self, timeout_ms: u32) -> MyResult<PooledConn>
Will try to get connection for a duration of timeout_ms milliseconds.
Failure
This function will return Error::DriverError(DriverError::Timeout) if timeout was
reached while waiting for new connection to become available.
sourcepub fn prepare<T: AsRef<str>>(&self, query: T) -> MyResult<Stmt<'static>>
 
pub fn prepare<T: AsRef<str>>(&self, query: T) -> MyResult<Stmt<'static>>
Will prepare statement. See Conn::prepare.
It will try to find connection which has this statement cached.
sourcepub fn prep_exec<A, T>(
    &self,
    query: A,
    params: T
) -> MyResult<QueryResult<'static>>where
    A: AsRef<str>,
    T: Into<Params>,
 
pub fn prep_exec<A, T>(
    &self,
    query: A,
    params: T
) -> MyResult<QueryResult<'static>>where
    A: AsRef<str>,
    T: Into<Params>,
Shortcut for pool.get_conn()?.prep_exec(..). See
Conn::prep_exec.
It will try to find connection which has this statement cached.
sourcepub fn first_exec<Q, P>(&self, query: Q, params: P) -> MyResult<Option<Row>>where
    Q: AsRef<str>,
    P: Into<Params>,
 
pub fn first_exec<Q, P>(&self, query: Q, params: P) -> MyResult<Option<Row>>where
    Q: AsRef<str>,
    P: Into<Params>,
See Conn::first_exec.
sourcepub fn start_transaction(
    &self,
    consistent_snapshot: bool,
    isolation_level: Option<IsolationLevel>,
    readonly: Option<bool>
) -> MyResult<Transaction<'static>>
 
pub fn start_transaction(
    &self,
    consistent_snapshot: bool,
    isolation_level: Option<IsolationLevel>,
    readonly: Option<bool>
) -> MyResult<Transaction<'static>>
Shortcut for pool.get_conn()?.start_transaction(..).