diff --git a/src/database/db_connection.rs b/src/database/db_connection.rs index 7fd51633b..e9638777c 100644 --- a/src/database/db_connection.rs +++ b/src/database/db_connection.rs @@ -283,6 +283,26 @@ impl DatabaseConnection { _ => {} } } + + /// Explicitly close the database connection + pub async fn close(self) -> Result<(), DbErr> { + match self { + #[cfg(feature = "sqlx-mysql")] + DatabaseConnection::SqlxMySqlPoolConnection(conn) => conn.close().await, + #[cfg(feature = "sqlx-postgres")] + DatabaseConnection::SqlxPostgresPoolConnection(conn) => conn.close().await, + #[cfg(feature = "sqlx-sqlite")] + DatabaseConnection::SqlxSqlitePoolConnection(conn) => conn.close().await, + #[cfg(feature = "mock")] + DatabaseConnection::MockDatabaseConnection(_) => { + // Nothing to cleanup, we just consume the `DatabaseConnection` + Ok(()) + } + DatabaseConnection::Disconnected => { + Err(DbErr::Conn(RuntimeErr::Internal("Disconnected".to_owned()))) + } + } + } } impl DbBackend { diff --git a/src/driver/sqlx_mysql.rs b/src/driver/sqlx_mysql.rs index 73dfa59fd..fc734482f 100644 --- a/src/driver/sqlx_mysql.rs +++ b/src/driver/sqlx_mysql.rs @@ -185,6 +185,11 @@ impl SqlxMySqlPoolConnection { { self.metric_callback = Some(Arc::new(callback)); } + + /// Explicitly close the MySQL connection + pub async fn close(self) -> Result<(), DbErr> { + Ok(self.pool.close().await) + } } impl From for QueryResult { diff --git a/src/driver/sqlx_postgres.rs b/src/driver/sqlx_postgres.rs index 167b34b85..2b5816aaf 100644 --- a/src/driver/sqlx_postgres.rs +++ b/src/driver/sqlx_postgres.rs @@ -200,6 +200,11 @@ impl SqlxPostgresPoolConnection { { self.metric_callback = Some(Arc::new(callback)); } + + /// Explicitly close the Postgres connection + pub async fn close(self) -> Result<(), DbErr> { + Ok(self.pool.close().await) + } } impl From for QueryResult { diff --git a/src/driver/sqlx_sqlite.rs b/src/driver/sqlx_sqlite.rs index b09f1bd0d..13b857242 100644 --- a/src/driver/sqlx_sqlite.rs +++ b/src/driver/sqlx_sqlite.rs @@ -192,6 +192,11 @@ impl SqlxSqlitePoolConnection { { self.metric_callback = Some(Arc::new(callback)); } + + /// Explicitly close the SQLite connection + pub async fn close(self) -> Result<(), DbErr> { + Ok(self.pool.close().await) + } } impl From for QueryResult {