Close connection explicitly (#1236)

* Close connection and transaction

* Close connection only
This commit is contained in:
Billy Chan 2022-11-22 13:55:03 +08:00 committed by GitHub
parent f65c931751
commit 115e19a95e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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