SeaQL/sea-orm

No way to set `max_lifetime` and `idle_timeout` to None for database connections

kosayoda opened this issue · 1 comments

Description

When ConnectOptions is converted to sqlx::pool::PoolOptions, the max_lifetime and idle_timeout fields are set only if the value provided is not None:

if let Some(idle_timeout) = self.idle_timeout {
opt = opt.idle_timeout(Some(idle_timeout));
}

if let Some(max_lifetime) = self.max_lifetime {
opt = opt.max_lifetime(Some(max_lifetime));
}

However, None is a valid field for sqlx to enable infinite connection lifetime and no idle timeout. By default, the values are set to 30 mins and 10 mins respectively.

Use Case

I'm using an SQLite in-memory database and the data will be wiped if connections to it are closed. Therefore I want to keep a connection to the database indefinitely.

@kosayoda while it doesn't directly address the issue you created, there is a workaround. I had to do something similar and used the sqlx::sqlite::SqliteConnectionOptions and sqlx::sqlite::SqlitePoolOptions directly to configure and create the pool and then used the sea_orm::SqlxSqliteConnector to build the sea_orm::DatabaseConnection.

As an example (I am winging this, so it might not compile, but it is based on what I did):

let mut conn_opts = sqlx::sqlite::SqliteConnectOptions::from_str(connstr)?;
conn_opts = conn_opts.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);

let pool_opts = sqlx::sqlite::SqlitePoolOptions::new()
    .idle_timeout(None)
    .max_lifetime(None);

let pool = pool_opts.connect_with(conn_opts).await?;
let connection = sea_orm::SqlxSqliteConnector::from_sqlx_sqlite_pool(pool);

I specifically needed this so that I could hook in an after_connect hook into the pool and couldn't come up with another way to do it.