pub type MySqlPool = Pool<MySql>;
Expand description
An alias for Pool
, specialized for MySQL.
Aliased Type§
struct MySqlPool(/* private fields */);
Implementations
source§impl<DB> Pool<DB>where
DB: Database,
impl<DB> Pool<DB>where
DB: Database,
sourcepub async fn connect(uri: &str) -> Result<Pool<DB>, Error>
pub async fn connect(uri: &str) -> Result<Pool<DB>, Error>
Creates a new connection pool with a default pool configuration and the given connection URI; and, immediately establishes one connection.
sourcepub async fn connect_with(
options: <<DB as Database>::Connection as Connection>::Options
) -> Result<Pool<DB>, Error>
pub async fn connect_with( options: <<DB as Database>::Connection as Connection>::Options ) -> Result<Pool<DB>, Error>
Creates a new connection pool with a default pool configuration and the given connection options; and, immediately establishes one connection.
sourcepub fn connect_lazy(uri: &str) -> Result<Pool<DB>, Error>
pub fn connect_lazy(uri: &str) -> Result<Pool<DB>, Error>
Creates a new connection pool with a default pool configuration and the given connection URI; and, will establish a connections as the pool starts to be used.
sourcepub fn connect_lazy_with(
options: <<DB as Database>::Connection as Connection>::Options
) -> Pool<DB>
pub fn connect_lazy_with( options: <<DB as Database>::Connection as Connection>::Options ) -> Pool<DB>
Creates a new connection pool with a default pool configuration and the given connection options; and, will establish a connections as the pool starts to be used.
sourcepub fn acquire(
&self
) -> impl Future<Output = Result<PoolConnection<DB>, Error>> + 'static
pub fn acquire( &self ) -> impl Future<Output = Result<PoolConnection<DB>, Error>> + 'static
Retrieves a connection from the pool.
Waits for at most the configured connection timeout before returning an error.
sourcepub fn try_acquire(&self) -> Option<PoolConnection<DB>>
pub fn try_acquire(&self) -> Option<PoolConnection<DB>>
Attempts to retrieve a connection from the pool if there is one available.
Returns None
immediately if there are no idle connections available in the pool.
sourcepub async fn begin(&self) -> Result<Transaction<'static, DB>, Error>
pub async fn begin(&self) -> Result<Transaction<'static, DB>, Error>
Retrieves a new connection and immediately begins a new transaction.
sourcepub async fn try_begin(&self) -> Result<Option<Transaction<'static, DB>>, Error>
pub async fn try_begin(&self) -> Result<Option<Transaction<'static, DB>>, Error>
Attempts to retrieve a new connection and immediately begins a new transaction if there is one available.
sourcepub async fn close(&self)
pub async fn close(&self)
Shut down the connection pool, waiting for all connections to be gracefully closed.
Upon .await
ing this call, any currently waiting or subsequent calls to Pool::acquire and
the like will immediately return Error::PoolClosed and no new connections will be opened.
Any connections currently idle in the pool will be immediately closed, including sending a graceful shutdown message to the database server, if applicable.
Checked-out connections are unaffected, but will be closed in the same manner when they are returned to the pool.
Does not resolve until all connections are returned to the pool and gracefully closed.
Note: async fn
Because this is an async fn
, the pool will not be marked as closed unless the
returned future is polled at least once.
If you want to close the pool but don’t want to wait for all connections to be gracefully
closed, you can do pool.close().now_or_never()
, which polls the future exactly once
with a no-op waker.
sourcepub fn is_closed(&self) -> bool
pub fn is_closed(&self) -> bool
Returns true
if .close()
has been called on the pool, false
otherwise.
sourcepub fn close_event(&self) -> CloseEvent ⓘ
pub fn close_event(&self) -> CloseEvent ⓘ
Get a future that resolves when Pool::close()
is called.
If the pool is already closed, the future resolves immediately.
This can be used to cancel long-running operations that hold onto a PoolConnection
so they don’t prevent the pool from closing (which would otherwise wait until all
connections are returned).
Examples
These examples use Postgres and Tokio, but should suffice to demonstrate the concept.
Do something when the pool is closed:
use sqlx::PgPool;
let pool = PgPool::connect("postgresql://...").await?;
let pool2 = pool.clone();
tokio::spawn(async move {
// Demonstrates that `CloseEvent` is itself a `Future` you can wait on.
// This lets you implement any kind of on-close event that you like.
pool2.close_event().await;
println!("Pool is closing!");
// Imagine maybe recording application statistics or logging a report, etc.
});
// The rest of the application executes normally...
// Close the pool before the application exits...
pool.close().await;
Cancel a long-running operation:
use sqlx::{Executor, PgPool};
let pool = PgPool::connect("postgresql://...").await?;
let pool2 = pool.clone();
tokio::spawn(async move {
pool2.close_event().do_until(async {
// This statement normally won't return for 30 days!
// (Assuming the connection doesn't time out first, of course.)
pool2.execute("SELECT pg_sleep('30 days')").await;
// If the pool is closed before the statement completes, this won't be printed.
// This is because `.do_until()` cancels the future it's given if the
// pool is closed first.
println!("Waited!");
}).await;
});
// This normally wouldn't return until the above statement completed and the connection
// was returned to the pool. However, thanks to `.do_until()`, the operation was
// cancelled as soon as we called `.close().await`.
pool.close().await;