Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Has the way auto reconnect when connection timeout or database server restart ? #561

Closed
xiuno opened this issue Jul 26, 2020 · 1 comment · Fixed by #1901
Closed

Has the way auto reconnect when connection timeout or database server restart ? #561

xiuno opened this issue Jul 26, 2020 · 1 comment · Fixed by #1901

Comments

@xiuno
Copy link

xiuno commented Jul 26, 2020

Code:

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.

@abonander
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants