Repository navigation
Replies: 1 comment
|
A For SeaORM 2, start with the pool's built-in checks: use sea_orm::{ConnectOptions, Database};
use std::time::Duration;
let mut opt = ConnectOptions::new(url);
opt.connect_lazy(true)
.connect_timeout(Duration::from_secs(5))
.acquire_timeout(Duration::from_secs(10))
.idle_timeout(Duration::from_secs(5 * 60))
.max_lifetime(Duration::from_secs(30 * 60))
.test_before_acquire(true);
let db = Database::connect(opt).await?;
If the extra round trip on every checkout matters, SeaORM 2 also has You still need to handle the query that observes the outage. At the worker/request boundary, classify the Normally you should not close and rebuild the pool for a server reboot or stale idle socket. SQLx is designed to replace failed pooled connections. If it still only recovers after a process restart with the default acquire check enabled, log the exact |
Uh oh!
There was an error while loading. Please reload this page.
I've been working with SeaORM 1.x for a while in a project, and am about to rework that project under SeaORM 2. One of the things I wanted to look at while I was reworking the code was more robust handling of lost connections, and I'm unclear on how this is handled inside SeaORM.
The issue I'm concerned with is that the project is an application that runs more or less 24/7 and is requesting data from several different databases constantly. It's using a DatabaseConnection established at application launch for each of these databases, but sometimes networking issues or server reboots cause a period of lost connections. The application ought to be able to handle that - it simply loses access to the data stream from that particular server for a time. But sometimes I find I need to restart the application to recover the lost connection. Unfortunately, it's really unclear what conditions cause this.
As part of the rework, I'd like to revise my connection handling to better resolve this kind of situation and to also handle the case where one or more of those databases are unavailable at application launch time. I was thinking along the lines of wrapping the DatabaseConnection with a small structure that also kept an atomic known state of the connection, and then if it's in a failed state, after a period of time it would close and release the old DatabaseConnection and attempt to create a new DatabaseConnection. All database requests would check the atomic known state first before attempting to use the connection, and if they fail with a connection error, would put the connection into it's failed state.
But as I was researching whether this was a good approach, I realized I'm not even sure how the connection is handled inside SeaORM itself and was wondering if there's a better solution. Anyone have feedback on this? Is there a best practice for dealing with lost connections?
All reactions