Skip to content

Commit

Permalink
clean database connection closing on shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
lovasoa committed Aug 28, 2024
1 parent e220742 commit b64a6d2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 7 additions & 0 deletions src/webserver/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion src/webserver/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ fn default_headers(app_state: &web::Data<AppState>) -> 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")]
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b64a6d2

Please sign in to comment.