Skip to content

Commit b64a6d2

Browse files
committed
clean database connection closing on shutdown
1 parent e220742 commit b64a6d2

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Support postgres String Constants with Unicode Escapes . Fixes https://github.com/lovasoa/SQLpage/discussions/511
88
- New [big_number](https://sql.datapage.app/documentation.sql?component=big_number#component) component to display key statistics and indicators in a large, easy-to-read format. Useful for displaying KPIs, metrics, and other important numbers in dashboards and reports.
99
- Fixed small display inconsistencies in the shell component with the new sidebar feature.
10+
- Cleanly close all opened database connections when shutting down sqlpage. Previously, when shutting down SQLPage, database connections that were opened during the session were not explicitly closed. These connections could remain open until the database closes it. Now, SQLPage ensures that all opened database connections are cleanly closed during shutdown. This guarantees that resources are freed immediately, ensuring more reliable operation, particularly in environments with limited database connections.
1011

1112
## 0.27.0 (2024-08-17)
1213

src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ async fn start() -> anyhow::Result<()> {
1919
webserver::database::migrations::apply(&app_config, &db).await?;
2020
let state = AppState::init_with_db(&app_config, db).await?;
2121
log::debug!("Starting server...");
22-
webserver::http::run_server(&app_config, state).await
22+
webserver::http::run_server(&app_config, state).await?;
23+
log::info!("Server stopped gracefully. Goodbye!");
24+
Ok(())
2325
}
2426

2527
fn init_logging() {

src/webserver/database/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ pub use sql::{make_placeholder, ParsedSqlFile};
1414
pub struct Database {
1515
pub(crate) connection: sqlx::AnyPool,
1616
}
17+
impl Database {
18+
pub async fn close(&self) -> anyhow::Result<()> {
19+
log::info!("Closing all database connections...");
20+
self.connection.close().await;
21+
Ok(())
22+
}
23+
}
1724

1825
#[derive(Debug)]
1926
pub enum DbItem {

src/webserver/http.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ fn default_headers(app_state: &web::Data<AppState>) -> middleware::DefaultHeader
635635
pub async fn run_server(config: &AppConfig, state: AppState) -> anyhow::Result<()> {
636636
let listen_on = config.listen_on();
637637
let state = web::Data::new(state);
638+
let final_state = web::Data::clone(&state);
638639
let factory = move || create_app(web::Data::clone(&state));
639640

640641
#[cfg(feature = "lambda-web")]
@@ -679,7 +680,11 @@ pub async fn run_server(config: &AppConfig, state: AppState) -> anyhow::Result<(
679680
server
680681
.run()
681682
.await
682-
.with_context(|| "Unable to start the application")
683+
.with_context(|| "Unable to start the application")?;
684+
685+
// We are done, we can close the database connection
686+
final_state.db.close().await?;
687+
Ok(())
683688
}
684689

685690
fn log_welcome_message(config: &AppConfig) {

0 commit comments

Comments
 (0)