diff --git a/CHANGELOG.md b/CHANGELOG.md index 99c6f378..f6ff12d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Support postgres String Constants with Unicode Escapes . Fixes https://github.com/lovasoa/SQLpage/discussions/511 - 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. - Fixed small display inconsistencies in the shell component with the new sidebar feature. +- 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. ## 0.27.0 (2024-08-17) diff --git a/src/main.rs b/src/main.rs index 5f740e46..4337b23e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,7 +19,9 @@ async fn start() -> anyhow::Result<()> { webserver::database::migrations::apply(&app_config, &db).await?; let state = AppState::init_with_db(&app_config, db).await?; log::debug!("Starting server..."); - webserver::http::run_server(&app_config, state).await + webserver::http::run_server(&app_config, state).await?; + log::info!("Server stopped gracefully. Goodbye!"); + Ok(()) } fn init_logging() { diff --git a/src/webserver/database/mod.rs b/src/webserver/database/mod.rs index a9e61348..339d3ded 100644 --- a/src/webserver/database/mod.rs +++ b/src/webserver/database/mod.rs @@ -14,6 +14,13 @@ pub use sql::{make_placeholder, ParsedSqlFile}; pub struct Database { pub(crate) connection: sqlx::AnyPool, } +impl Database { + pub async fn close(&self) -> anyhow::Result<()> { + log::info!("Closing all database connections..."); + self.connection.close().await; + Ok(()) + } +} #[derive(Debug)] pub enum DbItem { diff --git a/src/webserver/http.rs b/src/webserver/http.rs index e612db01..8f40c413 100644 --- a/src/webserver/http.rs +++ b/src/webserver/http.rs @@ -635,6 +635,7 @@ fn default_headers(app_state: &web::Data) -> middleware::DefaultHeader pub async fn run_server(config: &AppConfig, state: AppState) -> anyhow::Result<()> { let listen_on = config.listen_on(); let state = web::Data::new(state); + let final_state = web::Data::clone(&state); let factory = move || create_app(web::Data::clone(&state)); #[cfg(feature = "lambda-web")] @@ -679,7 +680,11 @@ pub async fn run_server(config: &AppConfig, state: AppState) -> anyhow::Result<( server .run() .await - .with_context(|| "Unable to start the application") + .with_context(|| "Unable to start the application")?; + + // We are done, we can close the database connection + final_state.db.close().await?; + Ok(()) } fn log_welcome_message(config: &AppConfig) {