Skip to content

Commit ddb8a7a

Browse files
committed
Print close warning at most once
1 parent 4a21918 commit ddb8a7a

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/database.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
This module defines the main API wrapper.
33
*/
44

5+
use std::sync::Arc;
6+
57
use log::{debug, warn, LevelFilter};
68
use rorm_declaration::config::DatabaseDriver;
79
use rorm_sql::delete::Delete;
@@ -96,14 +98,17 @@ Main API wrapper.
9698
All operations can be started with methods of this struct.
9799
*/
98100
#[derive(Clone)]
99-
pub struct Database(pub(crate) internal::database::Impl);
101+
pub struct Database(pub(crate) internal::database::Impl, Arc<()>);
100102

101103
impl Database {
102104
/**
103105
Connect to the database using `configuration`.
104106
*/
105107
pub async fn connect(configuration: DatabaseConfiguration) -> Result<Self, Error> {
106-
internal::database::connect(configuration).await
108+
Ok(Self(
109+
internal::database::connect(configuration).await?,
110+
Arc::new(()),
111+
))
107112
}
108113

109114
/**
@@ -146,7 +151,10 @@ impl Database {
146151

147152
impl Drop for Database {
148153
fn drop(&mut self) {
149-
if !internal::database::is_closed(self) {
154+
// The use of strong_count should be correct:
155+
// - the arc is private and we don't create WeakRefs
156+
// => when observing a strong_count of 1, there can't be any remaining refs
157+
if Arc::strong_count(&self.1) == 1 && !internal::database::is_closed(self) {
150158
warn!("Database has been dropped without calling close. This might case the last queries to not being flushed properly");
151159
}
152160
}

src/dummy_impl/database.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::transaction::Transaction;
99
pub(crate) type Impl = NotInstantiable;
1010

1111
/// Implementation of [Database::connect]
12-
pub(crate) async fn connect(_configuration: DatabaseConfiguration) -> Result<Database, Error> {
12+
pub(crate) async fn connect(_configuration: DatabaseConfiguration) -> Result<Impl, Error> {
1313
no_sqlx();
1414
}
1515

src/sqlx_impl/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub(crate) type Impl = AnyPool;
2020
const SLOW_STATEMENTS: Duration = Duration::from_millis(300);
2121

2222
/// Implementation of [Database::connect]
23-
pub(crate) async fn connect(configuration: DatabaseConfiguration) -> Result<Database, Error> {
23+
pub(crate) async fn connect(configuration: DatabaseConfiguration) -> Result<Impl, Error> {
2424
if configuration.max_connections < configuration.min_connections {
2525
return Err(Error::ConfigurationError(String::from(
2626
"max_connections must not be less than min_connections",
@@ -139,7 +139,7 @@ pub(crate) async fn connect(configuration: DatabaseConfiguration) -> Result<Data
139139
}
140140
};
141141

142-
Ok(Database(pool))
142+
Ok(pool)
143143
}
144144

145145
/// Implementation of [Database::raw_sql]

0 commit comments

Comments
 (0)