You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
let pool = sqlx::pool::PoolOptions::<sqlx::MySql>::new()
.min_connections(1)
.max_connections(5)
.connect_timeout(std::time::Duration::from_secs(5))
.max_lifetime(std::time::Duration::from_secs(5)) // short connection session lifetime
.idle_timeout(std::time::Duration::from_secs(10))
.test_before_acquire(true)
.connect(&std::env::var("DATABASE_URL").unwrap())
.await.unwrap();
I find the comment:
/// Set the maximum lifetime of individual connections.
///
/// Any connection with a lifetime greater than this will be closed.
///
/// When set to `None`, all connections live until either reaped by [`idle_timeout`]
/// or explicitly disconnected.
///
/// Infinite connections are not recommended due to the unfortunate reality of memory/resource
/// leaks on the database-side. It is better to retire connections periodically
/// (even if only once daily) to allow the database the opportunity to clean up data structures
/// (parse trees, query metadata caches, thread-local storage, etc.) that are associated with a
/// session.
///
/// [`idle_timeout`]: #method.idle_timeout
pub fn max_lifetime(mut self, lifetime: impl Into<Option<Duration>>) -> Self {
self.max_lifetime = lifetime.into();
self
}
short lifetime seems to improve database resource situation, but it will got Error when connection timeout:
sqlx::query_as("SELECT ...").fetch_one(pool).await?; // after timeout (testing with 5 seconds), it will unwrap().
has the anyway make the sqlx reconnect database when I use the closed handle? just like:
let pool = sqlx::pool::PoolOptions::<sqlx::MySql>::new()
.min_connections(1)
.max_connections(5)
.auto_reconnect(true)
That will greatly simplify the error handling code.
The text was updated successfully, but these errors were encountered:
I'm renaming connect_timeout to acquire_timeout in #1901 as that more clearly represents what's actually timing out. It retries connecting internally in a backoff loop until the timeout expires.
Code:
I find the comment:
short lifetime seems to improve database resource situation, but it will got Error when connection timeout:
has the anyway make the sqlx reconnect database when I use the closed handle? just like:
That will greatly simplify the error handling code.
The text was updated successfully, but these errors were encountered: