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

Close connection explicitly #1236

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/database/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/driver/sqlx_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MySqlRow> for QueryResult {
Expand Down
5 changes: 5 additions & 0 deletions src/driver/sqlx_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PgRow> for QueryResult {
Expand Down
5 changes: 5 additions & 0 deletions src/driver/sqlx_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SqliteRow> for QueryResult {
Expand Down