Skip to content

Commit

Permalink
Close connection and transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Nov 21, 2022
1 parent 9e77760 commit 474f1d4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/database/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pub trait ConnectionTrait: Sync {
fn is_mock_connection(&self) -> bool {
false
}

/// Explicitly close the database connection
async fn close(self) -> Result<(), DbErr>;
}

/// Stream query results
Expand Down
19 changes: 19 additions & 0 deletions src/database/db_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ impl ConnectionTrait for DatabaseConnection {
fn is_mock_connection(&self) -> bool {
matches!(self, DatabaseConnection::MockDatabaseConnection(_))
}

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(conn) => {
// Nothing to cleanup, we just consume the `DatabaseConnection`
Ok(())
},
DatabaseConnection::Disconnected => {
Err(DbErr::Conn(RuntimeErr::Internal("Disconnected".to_owned())))
}
}
}
}

#[async_trait::async_trait]
Expand Down
4 changes: 4 additions & 0 deletions src/database/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,10 @@ impl ConnectionTrait for DatabaseTransaction {
_ => unreachable!(),
}
}

async fn close(self) -> Result<(), DbErr> {
self.rollback().await
}
}

impl StreamTrait for DatabaseTransaction {
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

0 comments on commit 474f1d4

Please sign in to comment.