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| {
({
if meta.idle_for >= idle {
conn.ping()?;
}
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.
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 !Send for ConnectOptions
impl !Sync for ConnectOptions
impl !UnwindSafe for ConnectOptions
impl Freeze 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