File tree 3 files changed +14
-6
lines changed 3 files changed +14
-6
lines changed Original file line number Diff line number Diff line change 2
2
This module defines the main API wrapper.
3
3
*/
4
4
5
+ use std:: sync:: Arc ;
6
+
5
7
use log:: { debug, warn, LevelFilter } ;
6
8
use rorm_declaration:: config:: DatabaseDriver ;
7
9
use rorm_sql:: delete:: Delete ;
@@ -96,14 +98,17 @@ Main API wrapper.
96
98
All operations can be started with methods of this struct.
97
99
*/
98
100
#[ derive( Clone ) ]
99
- pub struct Database ( pub ( crate ) internal:: database:: Impl ) ;
101
+ pub struct Database ( pub ( crate ) internal:: database:: Impl , Arc < ( ) > ) ;
100
102
101
103
impl Database {
102
104
/**
103
105
Connect to the database using `configuration`.
104
106
*/
105
107
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
+ ) )
107
112
}
108
113
109
114
/**
@@ -146,7 +151,10 @@ impl Database {
146
151
147
152
impl Drop for Database {
148
153
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 ) {
150
158
warn ! ( "Database has been dropped without calling close. This might case the last queries to not being flushed properly" ) ;
151
159
}
152
160
}
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ use crate::transaction::Transaction;
9
9
pub ( crate ) type Impl = NotInstantiable ;
10
10
11
11
/// 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 > {
13
13
no_sqlx ( ) ;
14
14
}
15
15
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ pub(crate) type Impl = AnyPool;
20
20
const SLOW_STATEMENTS : Duration = Duration :: from_millis ( 300 ) ;
21
21
22
22
/// 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 > {
24
24
if configuration. max_connections < configuration. min_connections {
25
25
return Err ( Error :: ConfigurationError ( String :: from (
26
26
"max_connections must not be less than min_connections" ,
@@ -139,7 +139,7 @@ pub(crate) async fn connect(configuration: DatabaseConfiguration) -> Result<Data
139
139
}
140
140
} ;
141
141
142
- Ok ( Database ( pool) )
142
+ Ok ( pool)
143
143
}
144
144
145
145
/// Implementation of [Database::raw_sql]
You can’t perform that action at this time.
0 commit comments