Add ConnectOptions::test_before_acquire #2066

This commit is contained in:
Chris Tsang 2024-01-20 22:05:16 +00:00
parent c6e1a84a12
commit 382ffa3054

View File

@ -62,6 +62,7 @@ pub struct ConnectOptions {
pub(crate) sqlcipher_key: Option<Cow<'static, str>>, pub(crate) sqlcipher_key: Option<Cow<'static, str>>,
/// Schema search path (PostgreSQL only) /// Schema search path (PostgreSQL only)
pub(crate) schema_search_path: Option<String>, pub(crate) schema_search_path: Option<String>,
pub(crate) test_before_acquire: bool,
} }
impl Database { impl Database {
@ -155,6 +156,7 @@ impl ConnectOptions {
sqlx_slow_statements_logging_threshold: Duration::from_secs(1), sqlx_slow_statements_logging_threshold: Duration::from_secs(1),
sqlcipher_key: None, sqlcipher_key: None,
schema_search_path: None, schema_search_path: None,
test_before_acquire: true,
} }
} }
@ -183,6 +185,7 @@ impl ConnectOptions {
if let Some(max_lifetime) = self.max_lifetime { if let Some(max_lifetime) = self.max_lifetime {
opt = opt.max_lifetime(Some(max_lifetime)); opt = opt.max_lifetime(Some(max_lifetime));
} }
opt = opt.test_before_acquire(self.test_before_acquire);
opt opt
} }
@ -268,14 +271,14 @@ impl ConnectOptions {
self.sqlx_logging self.sqlx_logging
} }
/// Set SQLx statement logging level (default INFO) /// Set SQLx statement logging level (default INFO).
/// (ignored if `sqlx_logging` is `false`) /// (ignored if `sqlx_logging` is `false`)
pub fn sqlx_logging_level(&mut self, level: log::LevelFilter) -> &mut Self { pub fn sqlx_logging_level(&mut self, level: log::LevelFilter) -> &mut Self {
self.sqlx_logging_level = level; self.sqlx_logging_level = level;
self self
} }
/// Set SQLx slow statements logging level and duration threshold /// Set SQLx slow statements logging level and duration threshold (default `LevelFilter::Off`).
/// (ignored if `sqlx_logging` is `false`) /// (ignored if `sqlx_logging` is `false`)
pub fn sqlx_slow_statements_logging_settings( pub fn sqlx_slow_statements_logging_settings(
&mut self, &mut self,
@ -317,4 +320,10 @@ impl ConnectOptions {
self.schema_search_path = Some(schema_search_path.into()); self.schema_search_path = Some(schema_search_path.into());
self self
} }
/// If true, the connection will be pinged after acquiring from the pool (default true).
pub fn test_before_acquire(&mut self, value: bool) -> &mut Self {
self.test_before_acquire = value;
self
}
} }