Expose RawConnection::close on Connection (and rename from finalize)

This commit is contained in:
Ryan Leckey 2019-09-02 17:09:23 -07:00
parent fe697fee1d
commit 9d7eff2ca7
2 changed files with 24 additions and 10 deletions

View File

@ -37,7 +37,9 @@ pub trait RawConnection: Send {
///
/// This method is not required to be called. A database server will eventually notice
/// and clean up not fully closed connections.
fn finalize<'c>(&'c mut self) -> BoxFuture<'c, Result<(), Error>>;
///
/// It is safe to close an already closed connection.
fn close<'c>(&'c mut self) -> BoxFuture<'c, Result<(), Error>>;
/// Verifies a connection to the database is still alive.
fn ping<'c>(&'c mut self) -> BoxFuture<'c, Result<(), Error>> {
@ -92,13 +94,25 @@ where
Ok(Self(Arc::new(shared)))
}
async fn get(&self) -> ConnectionFairy<'_, DB> {
#[inline]
async fn acquire(&self) -> ConnectionFairy<'_, DB> {
ConnectionFairy::new(&self.0, self.0.acquire().await)
}
/// Release resources for this database connection immediately.
///
/// This method is not required to be called. A database server will eventually notice
/// and clean up not fully closed connections.
///
/// It is safe to close an already closed connection.
pub async fn close(&self) -> crate::Result<()> {
let mut conn = self.acquire().await;
conn.close().await
}
/// Verifies a connection to the database is still alive.
pub async fn ping(&mut self) -> crate::Result<()> {
let mut conn = self.get().await;
pub async fn ping(&self) -> crate::Result<()> {
let mut conn = self.acquire().await;
conn.ping().await
}
}
@ -118,7 +132,7 @@ where
A: IntoQueryParameters<Self::Backend> + Send,
{
Box::pin(async move {
let mut conn = self.get().await;
let mut conn = self.acquire().await;
conn.execute(query, params.into()).await
})
}
@ -133,7 +147,7 @@ where
T: FromSqlRow<Self::Backend> + Send + Unpin,
{
Box::pin(async_stream::try_stream! {
let mut conn = self.get().await;
let mut conn = self.acquire().await;
let mut s = conn.fetch(query, params.into());
while let Some(row) = s.next().await.transpose()? {
@ -152,7 +166,7 @@ where
T: FromSqlRow<Self::Backend>,
{
Box::pin(async move {
let mut conn = self.get().await;
let mut conn = self.acquire().await;
let row = conn.fetch_optional(query, params.into()).await?;
Ok(row.map(T::from_row))

View File

@ -134,7 +134,7 @@ impl PostgresRawConnection {
Ok(conn)
}
async fn finalize(&mut self) -> crate::Result<()> {
async fn close(&mut self) -> crate::Result<()> {
self.write(Terminate);
self.stream.flush().await?;
self.stream.close().await?;
@ -307,8 +307,8 @@ impl RawConnection for PostgresRawConnection {
}
#[inline]
fn finalize<'c>(&'c mut self) -> BoxFuture<'c, crate::Result<()>> {
Box::pin(self.finalize())
fn close<'c>(&'c mut self) -> BoxFuture<'c, crate::Result<()>> {
Box::pin(self.close())
}
fn execute<'c>(