SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
7.37k stars 517 forks source link

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

Open kosayoda opened 2 months ago

kosayoda commented 2 months ago

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: https://github.com/SeaQL/sea-orm/blob/b68e770f973adea8651420c0865f697bca2f5b9b/src/driver/sqlx_common.rs#L54-L56 https://github.com/SeaQL/sea-orm/blob/b68e770f973adea8651420c0865f697bca2f5b9b/src/driver/sqlx_common.rs#L60-L62

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.

jcreekmore commented 1 month ago

@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.