Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(connection): retry metadata loads after idle disconnect

t8y2eaff976de7ea
diff --git a/crates/dbx-core/src/schema.rs b/crates/dbx-core/src/schema.rs
index 2c2ab39097..1a5bc0ae71 100644
--- a/crates/dbx-core/src/schema.rs
+++ b/crates/dbx-core/src/schema.rs
@@ -1,6 +1,7 @@
use crate::connection::{AppState, MysqlMode, PoolKind};
use crate::db;
use crate::models::connection::DatabaseType;
+use std::future::Future;
macro_rules! extract_pool {
($connections:expr, $key:expr, $variant:ident) => {
@@ -242,6 +243,10 @@ fn clickhouse_metadata_database<'a>(database: &'a str, schema: &'a str) -> &'a s
}
pub async fn list_databases_core(state: &AppState, connection_id: &str) -> Result<Vec<db::DatabaseInfo>, String> {
+ retry_metadata_connection(state, connection_id, None, || list_databases_once(state, connection_id)).await
+}
+
+async fn list_databases_once(state: &AppState, connection_id: &str) -> Result<Vec<db::DatabaseInfo>, String> {
log::info!("[list_databases] connection_id={connection_id}");
{
let connections = state.connections.read().await;
@@ -292,6 +297,13 @@ pub async fn list_databases_core(state: &AppState, connection_id: &str) -> Resul
}
pub async fn list_schemas_core(state: &AppState, connection_id: &str, database: &str) -> Result<Vec<String>, String> {
+ retry_metadata_connection(state, connection_id, Some(database), || {
+ list_schemas_once(state, connection_id, database)
+ })
+ .await
+}
+
+async fn list_schemas_once(state: &AppState, connection_id: &str, database: &str) -> Result<Vec<String>, String> {
let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?;
{
@@ -332,6 +344,20 @@ pub async fn list_tables_core(
schema: &str,
filter: Option<&str>,
limit: Option<usize>,
+) -> Result<Vec<db::TableInfo>, String> {
+ retry_metadata_connection(state, connection_id, Some(database), || {
+ list_tables_once(state, connection_id, database, schema, filter, limit)
+ })
+ .await
+}
+
+async fn list_tables_once(
+ state: &AppState,
+ connection_id: &str,
+ database: &str,
+ schema: &str,
+ filter: Option<&str>,
+ limit: Option<usize>,
) -> Result<Vec<db::TableInfo>, String> {
let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?;
let duckdb_attached_names = duckdb_attached_database_names(state, connection_id).await;
@@ -469,6 +495,18 @@ pub async fn list_objects_core(
connection_id: &str,
database: &str,
schema: &str,
+) -> Result<Vec<db::ObjectInfo>, String> {
+ retry_metadata_connection(state, connection_id, Some(database), || {
+ list_objects_once(state, connection_id, database, schema)
+ })
+ .await
+}
+
+async fn list_objects_once(
+ state: &AppState,
+ connection_id: &str,
+ database: &str,
+ schema: &str,
) -> Result<Vec<db::ObjectInfo>, String> {
let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?;
@@ -540,6 +578,26 @@ pub async fn list_objects_core(
}
}
+async fn retry_metadata_connection<T, F, Fut>(
+ state: &AppState,
+ connection_id: &str,
+ database: Option<&str>,
+ mut operation: F,
+) -> Result<T, String>
+where
+ F: FnMut() -> Fut,
+ Fut: Future<Output = Result<T, String>>,
+{
+ let result = operation().await;
+ match result {
+ Err(error) if crate::query::is_connection_error(&error) => {
+ state.reconnect_pool(connection_id, database).await?;
+ operation().await
+ }
+ _ => result,
+ }
+}
+
pub async fn get_columns_core(
state: &AppState,
connection_id: &str,