pub struct ConnectOptions { /* private fields */ }Expand description
Configuration for opening a DatabaseConnection: connection URL, pool
sizing, timeouts, logging, and backend-specific options.
Construct with ConnectOptions::new (or directly from a URL &str),
then chain the setter methods before passing to Database::connect.
Implementationsยง
Sourceยงimpl ConnectOptions
impl ConnectOptions
Sourcepub fn new<T>(url: T) -> Self
pub fn new<T>(url: T) -> Self
Create new ConnectOptions for a Database by passing in a URI string
Sourcepub fn max_connections(&mut self, value: u32) -> &mut Self
pub fn max_connections(&mut self, value: u32) -> &mut Self
Set the maximum number of connections of the pool
Sourcepub fn get_max_connections(&self) -> Option<u32>
pub fn get_max_connections(&self) -> Option<u32>
Get the maximum number of connections of the pool, if set
Sourcepub fn min_connections(&mut self, value: u32) -> &mut Self
pub fn min_connections(&mut self, value: u32) -> &mut Self
Set the minimum number of connections of the pool
Sourcepub fn get_min_connections(&self) -> Option<u32>
pub fn get_min_connections(&self) -> Option<u32>
Get the minimum number of connections of the pool, if set
Sourcepub fn connect_timeout(&mut self, value: Duration) -> &mut Self
pub fn connect_timeout(&mut self, value: Duration) -> &mut Self
Set the timeout duration when acquiring a connection
Sourcepub fn get_connect_timeout(&self) -> Option<Duration>
pub fn get_connect_timeout(&self) -> Option<Duration>
Get the timeout duration when acquiring a connection, if set
Sourcepub fn idle_timeout<T>(&mut self, value: T) -> &mut Self
pub fn idle_timeout<T>(&mut self, value: T) -> &mut Self
Set the idle duration before closing a connection.
Sourcepub fn get_idle_timeout(&self) -> Option<Option<Duration>>
pub fn get_idle_timeout(&self) -> Option<Option<Duration>>
Get the idle duration before closing a connection, if set
Sourcepub fn acquire_timeout(&mut self, value: Duration) -> &mut Self
pub fn acquire_timeout(&mut self, value: Duration) -> &mut Self
Set the maximum amount of time to spend waiting for acquiring a connection
Sourcepub fn get_acquire_timeout(&self) -> Option<Duration>
pub fn get_acquire_timeout(&self) -> Option<Duration>
Get the maximum amount of time to spend waiting for acquiring a connection
Sourcepub fn max_lifetime<T>(&mut self, lifetime: T) -> &mut Self
pub fn max_lifetime<T>(&mut self, lifetime: T) -> &mut Self
Set the maximum lifetime of individual connections.
Sourcepub fn get_max_lifetime(&self) -> Option<Option<Duration>>
pub fn get_max_lifetime(&self) -> Option<Option<Duration>>
Get the maximum lifetime of individual connections, if set
Sourcepub fn sqlx_logging(&mut self, value: bool) -> &mut Self
pub fn sqlx_logging(&mut self, value: bool) -> &mut Self
Enable SQLx statement logging (default true)
Sourcepub fn get_sqlx_logging(&self) -> bool
pub fn get_sqlx_logging(&self) -> bool
Get whether SQLx statement logging is enabled
Sourcepub fn record_stmt_in_spans(&mut self, value: bool) -> &mut Self
pub fn record_stmt_in_spans(&mut self, value: bool) -> &mut Self
Enable recording db.statement in tracing spans (default true).
Sourcepub fn get_record_stmt_in_spans(&self) -> bool
pub fn get_record_stmt_in_spans(&self) -> bool
Get whether db.statement recording in tracing spans is enabled.
Sourcepub fn sqlx_logging_level(&mut self, level: LevelFilter) -> &mut Self
pub fn sqlx_logging_level(&mut self, level: LevelFilter) -> &mut Self
Set SQLx statement logging level (default INFO).
(ignored if sqlx_logging is false)
Sourcepub fn sqlx_slow_statements_logging_settings(
&mut self,
level: LevelFilter,
duration: Duration,
) -> &mut Self
pub fn sqlx_slow_statements_logging_settings( &mut self, level: LevelFilter, duration: Duration, ) -> &mut Self
Set SQLx slow statements logging level and duration threshold (default LevelFilter::Off).
(ignored if sqlx_logging is false)
Sourcepub fn get_sqlx_logging_level(&self) -> LevelFilter
pub fn get_sqlx_logging_level(&self) -> LevelFilter
Get the level of SQLx statement logging
Sourcepub fn get_sqlx_slow_statements_logging_settings(
&self,
) -> (LevelFilter, Duration)
pub fn get_sqlx_slow_statements_logging_settings( &self, ) -> (LevelFilter, Duration)
Get the SQLx slow statements logging settings
Sourcepub fn sqlcipher_key<T>(&mut self, value: T) -> &mut Self
pub fn sqlcipher_key<T>(&mut self, value: T) -> &mut Self
set key for sqlcipher
Sourcepub fn set_schema_search_path<T>(&mut self, schema_search_path: T) -> &mut Self
pub fn set_schema_search_path<T>(&mut self, schema_search_path: T) -> &mut Self
Set schema search path (PostgreSQL only)
Sourcepub fn set_application_name<T>(&mut self, application_name: T) -> &mut Self
pub fn set_application_name<T>(&mut self, application_name: T) -> &mut Self
Set application name (PostgreSQL only)
Sourcepub fn statement_timeout(&mut self, value: Duration) -> &mut Self
pub fn statement_timeout(&mut self, value: Duration) -> &mut Self
Set the statement timeout (PostgreSQL only).
This sets the PostgreSQL statement_timeout parameter via the connection options,
causing the server to abort any statement that exceeds the specified duration.
The timeout is applied at connection time and does not require an extra roundtrip.
Has no effect on MySQL or SQLite connections.
Sourcepub fn get_statement_timeout(&self) -> Option<Duration>
pub fn get_statement_timeout(&self) -> Option<Duration>
Get the statement timeout, if set
Sourcepub fn test_before_acquire(&mut self, value: bool) -> &mut Self
pub fn test_before_acquire(&mut self, value: bool) -> &mut Self
If true, the connection will be pinged upon acquiring from the pool (default true).
See test_before_acquire_if_idle_for for the
cheaper โonly ping stale connectionsโ variant.
Sourcepub fn get_test_before_acquire(&self) -> bool
pub fn get_test_before_acquire(&self) -> bool
Get whether a pooled connection is pinged on every acquire (default true).
Sourcepub fn test_before_acquire_if_idle_for(&mut self, idle: Duration) -> &mut Self
pub fn test_before_acquire_if_idle_for(&mut self, idle: Duration) -> &mut Self
Ping a pooled connection before handing it out, but only once it has been idle for at
least idle.
test_before_acquire pings on every acquire, which adds
a round-trip to each checkout. This shorthand instead pings only connections that have
been idle long enough to plausibly have been dropped by the server, a proxy, or a
firewall โ the common failure mode โ while letting hot connections through untouched.
Calling this sets test_before_acquire to false (SQLx
runs the per-acquire ping and this hook, so leaving it enabled would ping on every
acquire regardless and defeat the threshold).
It composes with a per-backend map_sqlx_postgres_before_acquire callback (and its
MySQL / SQLite counterparts): the idle-ping runs first, then your callback.
ยงExpands to
With no per-backend callback set, this is exactly the following configuration on the
underlying sqlx::pool::PoolOptions:
pool_options
.test_before_acquire(false)
.before_acquire(move |conn, meta| {
Box::pin(async move {
if meta.idle_for >= idle {
conn.ping().await?;
}
Ok(true)
})
})Applies only to pools built through Database::connect. Pools adopted via
SqlxPostgresConnector::from_sqlx_postgres_pool (and the MySQL / SQLite equivalents)
bypass ConnectOptions entirely โ configure before_acquire on your own
sqlx::pool::PoolOptions in that case.
ยงExample
let mut opt = ConnectOptions::new("postgres://localhost/db");
opt.test_before_acquire_if_idle_for(Duration::from_secs(30));
assert_eq!(opt.get_test_before_acquire(), false);Sourcepub fn get_test_before_acquire_if_idle_for(&self) -> Option<Duration>
pub fn get_test_before_acquire_if_idle_for(&self) -> Option<Duration>
Get the idle threshold set by
test_before_acquire_if_idle_for, if any.
Sourcepub fn connect_lazy(&mut self, value: bool) -> &mut Self
pub fn connect_lazy(&mut self, value: bool) -> &mut Self
If set to true, the db connection pool will be created using SQLxโs
connect_lazy method.
Sourcepub fn get_connect_lazy(&self) -> bool
pub fn get_connect_lazy(&self) -> bool
Get whether DB connections will be established when the pool is created or only as needed.
Sourcepub fn after_connect<F>(&mut self, f: F) -> &mut Self
pub fn after_connect<F>(&mut self, f: F) -> &mut Self
Set a callback function that will be called after a new connection is established.
Sourcepub fn map_sqlx_mysql_opts<F>(&mut self, f: F) -> &mut Self
Available on crate feature sqlx-mysql only.
pub fn map_sqlx_mysql_opts<F>(&mut self, f: F) -> &mut Self
sqlx-mysql only.Apply a function to modify the underlying MySqlConnectOptions before
creating the connection pool.
Sourcepub fn map_sqlx_mysql_pool_opts<F>(&mut self, f: F) -> &mut Self
Available on crate feature sqlx-mysql only.
pub fn map_sqlx_mysql_pool_opts<F>(&mut self, f: F) -> &mut Self
sqlx-mysql only.Apply a function to modify the underlying sqlx::pool::PoolOptions<sqlx::MySql>
before creating the connection pool.
Sourcepub fn map_sqlx_postgres_opts<F>(&mut self, f: F) -> &mut Self
Available on crate feature sqlx-postgres only.
pub fn map_sqlx_postgres_opts<F>(&mut self, f: F) -> &mut Self
sqlx-postgres only.Apply a function to modify the underlying PgConnectOptions before
creating the connection pool.
Sourcepub fn map_sqlx_postgres_pool_opts<F>(&mut self, f: F) -> &mut Self
Available on crate feature sqlx-postgres only.
pub fn map_sqlx_postgres_pool_opts<F>(&mut self, f: F) -> &mut Self
sqlx-postgres only.Apply a function to modify the underlying sqlx::pool::PoolOptions<sqlx::Postgres>
before creating the connection pool.
Sourcepub fn map_sqlx_sqlite_opts<F>(&mut self, f: F) -> &mut Self
Available on crate feature sqlx-sqlite only.
pub fn map_sqlx_sqlite_opts<F>(&mut self, f: F) -> &mut Self
sqlx-sqlite only.Apply a function to modify the underlying SqliteConnectOptions before
creating the connection pool.
Sourcepub fn map_sqlx_sqlite_pool_opts<F>(&mut self, f: F) -> &mut Self
Available on crate feature sqlx-sqlite only.
pub fn map_sqlx_sqlite_pool_opts<F>(&mut self, f: F) -> &mut Self
sqlx-sqlite only.Apply a function to modify the underlying sqlx::pool::PoolOptions<sqlx::Sqlite>
before creating the connection pool.
Sourcepub fn map_sqlx_mysql_before_acquire<F>(&mut self, f: F) -> &mut Selfwhere
F: for<'c> Fn(&'c mut MySqlConnection, PoolConnectionMetadata) -> BoxFuture<'c, Result<bool, Error>> + Send + Sync + 'static,
Available on crate feature sqlx-mysql only.
pub fn map_sqlx_mysql_before_acquire<F>(&mut self, f: F) -> &mut Selfwhere
F: for<'c> Fn(&'c mut MySqlConnection, PoolConnectionMetadata) -> BoxFuture<'c, Result<bool, Error>> + Send + Sync + 'static,
sqlx-mysql only.Set a before_acquire callback run on an idle pooled MySQL connection before it is
handed out.
Return Ok(true) to use the connection, or Ok(false)/Err(_) to discard it and let
the pool try another (opening a fresh one if needed). Composes after the idle-ping
installed by
test_before_acquire_if_idle_for โ that runs
first, then this callback.
Applies only to pools built through Database::connect, not to pools adopted via
SqlxMySqlConnector::from_sqlx_mysql_pool.
Sourcepub fn map_sqlx_postgres_before_acquire<F>(&mut self, f: F) -> &mut Selfwhere
F: for<'c> Fn(&'c mut PgConnection, PoolConnectionMetadata) -> BoxFuture<'c, Result<bool, Error>> + Send + Sync + 'static,
Available on crate feature sqlx-postgres only.
pub fn map_sqlx_postgres_before_acquire<F>(&mut self, f: F) -> &mut Selfwhere
F: for<'c> Fn(&'c mut PgConnection, PoolConnectionMetadata) -> BoxFuture<'c, Result<bool, Error>> + Send + Sync + 'static,
sqlx-postgres only.Set a before_acquire callback run on an idle pooled PostgreSQL connection before it
is handed out.
Return Ok(true) to use the connection, or Ok(false)/Err(_) to discard it and let
the pool try another (opening a fresh one if needed). Composes after the idle-ping
installed by
test_before_acquire_if_idle_for โ that runs
first, then this callback.
Applies only to pools built through Database::connect, not to pools adopted via
SqlxPostgresConnector::from_sqlx_postgres_pool.
ยงExample
let mut opt = ConnectOptions::new("postgres://localhost/db");
opt.map_sqlx_postgres_before_acquire(|_conn, meta| {
Box::pin(async move {
// Discard (and transparently replace) connections older than 10 minutes,
// rather than pinging on every acquire.
Ok(meta.age < Duration::from_secs(600))
})
});Sourcepub fn map_sqlx_sqlite_before_acquire<F>(&mut self, f: F) -> &mut Selfwhere
F: for<'c> Fn(&'c mut SqliteConnection, PoolConnectionMetadata) -> BoxFuture<'c, Result<bool, Error>> + Send + Sync + 'static,
Available on crate feature sqlx-sqlite only.
pub fn map_sqlx_sqlite_before_acquire<F>(&mut self, f: F) -> &mut Selfwhere
F: for<'c> Fn(&'c mut SqliteConnection, PoolConnectionMetadata) -> BoxFuture<'c, Result<bool, Error>> + Send + Sync + 'static,
sqlx-sqlite only.Set a before_acquire callback run on an idle pooled SQLite connection before it is
handed out.
Return Ok(true) to use the connection, or Ok(false)/Err(_) to discard it and let
the pool try another (opening a fresh one if needed). Composes after the idle-ping
installed by
test_before_acquire_if_idle_for โ that runs
first, then this callback.
Applies only to pools built through Database::connect, not to pools adopted via
SqlxSqliteConnector::from_sqlx_sqlite_pool.
Sourceยงimpl ConnectOptions
impl ConnectOptions
Sourcepub fn sqlx_pool_options<DB>(self) -> PoolOptions<DB>where
DB: Database,
Available on crate feature sqlx-dep only.
pub fn sqlx_pool_options<DB>(self) -> PoolOptions<DB>where
DB: Database,
sqlx-dep only.Convert ConnectOptions into sqlx::pool::PoolOptions
Trait Implementationsยง
Sourceยงimpl Clone for ConnectOptions
impl Clone for ConnectOptions
Sourceยงfn clone(&self) -> ConnectOptions
fn clone(&self) -> ConnectOptions
1.0.0 (const: unstable) ยท Sourceยงfn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSourceยงimpl Debug for ConnectOptions
impl Debug for ConnectOptions
Sourceยงimpl<T> From<T> for ConnectOptions
impl<T> From<T> for ConnectOptions
Sourceยงfn from(s: T) -> ConnectOptions
fn from(s: T) -> ConnectOptions
Auto Trait Implementationsยง
impl !RefUnwindSafe for ConnectOptions
impl !UnwindSafe for ConnectOptions
impl Freeze for ConnectOptions
impl Send for ConnectOptions
impl Sync for ConnectOptions
impl Unpin for ConnectOptions
impl UnsafeUnpin for ConnectOptions
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Sourceยงimpl<T> Instrument for T
impl<T> Instrument for T
Sourceยงfn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Sourceยงfn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Sourceยงimpl<T> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more