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。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(sql-file): pin one session for the whole SQL file execution (Fixes #12160) (#12725)
dienasoe9639e6919ca
diff --git a/crates/dbx-core/src/connection/mod.rs b/crates/dbx-core/src/connection/mod.rsindex 52eb9ef772..54789e0653 100644--- a/crates/dbx-core/src/connection/mod.rs+++ b/crates/dbx-core/src/connection/mod.rs@@ -1576,6 +1576,21 @@ impl AppState {update(&mut connections)}+ /// Test-only: the pool key a workload session uses. Tests that inject a+ /// pool instead of opening a real connection need the exact session key the+ /// executor will look up.+ #[cfg(test)]+ pub(crate) async fn session_pool_key_for_test(+ &self,+ connection_id: &str,+ database: Option<&str>,+ client_session_id: &str,+ ) -> String {+ let config = self.configs.read().await.get(connection_id).cloned();+ let base_pool_key = base_pool_key_for_config(config.as_ref(), connection_id, database, None, false);+ pool_key_for_session_role(config.as_ref(), base_pool_key, Some(client_session_id), AgentSessionRole::Workload)+ }+fn pool_routing_control(&self) -> PoolRoutingControl {PoolRoutingControl {connections: self.connections.clone(),diff --git a/crates/dbx-core/src/data/sql_file_import.rs b/crates/dbx-core/src/data/sql_file_import.rsindex 57d06914a2..228054cc94 100644--- a/crates/dbx-core/src/data/sql_file_import.rs+++ b/crates/dbx-core/src/data/sql_file_import.rs@@ -8,7 +8,7 @@ use mysql_async::prelude::Queryable;use tokio::io::{AsyncReadExt, BufReader};use tokio_util::sync::CancellationToken;-use crate::connection::{AppState, PoolKind};+use crate::connection::{AppState, ClientSessionPoolCleanupGuard, PoolKind};use crate::db;use crate::models::connection::DatabaseType;@@ -661,6 +661,72 @@ async fn with_sql_file_transaction<T>(result}+/// A session pinned for one SQL file execution.+///+/// Non-MySQL files execute each statement through the shared pool, so a file+/// that contains its own `BEGIN` / `COMMIT` can span physical connections and+/// leave an unfinished transaction `idle in transaction` in the pool (#12160).+/// MySQL already pins a connection in [`MySqlSqlFileExecutor`], and manual+/// transactions own their session, so both keep using their existing paths.+struct SqlFilePinnedSession {+ client_session_id: Option<String>,+ _cleanup: Option<ClientSessionPoolCleanupGuard>,+}++impl SqlFilePinnedSession {+ fn id(&self) -> Option<&str> {+ self.client_session_id.as_deref()+ }++ /// Close the pinned session before work that runs on another connection.+ ///+ /// Closing the session pool terminates its connection, which rolls back any+ /// transaction the file left open and releases its locks. The cleanup guard+ /// only drops at function exit, which is too late for the catalog-level+ /// constraint restore: that restore runs on the shared pool and would wait+ /// on the pinned session's locks until the query timeout.+ async fn release(&mut self, state: &AppState, request: &SqlFileRequest) {+ let Some(client_session_id) = self.client_session_id.take() else {+ return;+ };+ let database = request.database.trim();+ let database = (!database.is_empty()).then_some(database);+ match state.close_client_session_pool(&request.connection_id, database, &client_session_id).await {+ Ok(_) => {+ if let Some(cleanup) = self._cleanup.as_mut() {+ cleanup.disarm();+ }+ }+ Err(error) => {+ // Leave the guard armed so the pool is still removed at function+ // exit even if the synchronous close failed.+ log::warn!("[sql_file_import] failed to close pinned session {client_session_id}: {error}");+ }+ }+ }+}++fn should_pin_sql_file_session(request: &SqlFileRequest, mysql_executor_present: bool) -> bool {+ request.txn_session_id.is_none() && !mysql_executor_present+}++async fn pin_sql_file_session(+ state: &AppState,+ request: &SqlFileRequest,+ mysql_executor_present: bool,+) -> SqlFilePinnedSession {+ if !should_pin_sql_file_session(request, mysql_executor_present) {+ return SqlFilePinnedSession { client_session_id: None, _cleanup: None };+ }+ let client_session_id = format!("sql-file-{}", request.execution_id);+ let database = request.database.trim();+ let database = (!database.is_empty()).then_some(database);+ // The guard removes the session-scoped pool when the file finishes, errors+ // or is cancelled, which also rolls back a transaction the file left open.+ let cleanup = state.workload_session_pool_cleanup_guard(&request.connection_id, database, &client_session_id).await;+ SqlFilePinnedSession { client_session_id: Some(client_session_id), _cleanup: cleanup }+}+pub async fn execute_sql_file_content(state: &AppState,request: &SqlFileRequest,@@ -714,6 +780,9 @@ async fn execute_sql_file_content_inner(// MySQL-family imports need one pinned connection so `USE` and session// state survive across the whole file.let mut mysql_executor = MySqlSqlFileExecutor::build(state, request, import_target.as_ref()).await?;+ // Every other family also needs one connection for the whole file: a+ // file-level `BEGIN` / `COMMIT` must not span pooled sessions (#12160).+ let mut pinned_session = pin_sql_file_session(state, request, mysql_executor.is_some()).await;let bypass_kind = import_target.as_ref().and_then(relational_constraint_bypass_kind);let non_mysql_bypass = request.skip_relational_constraints&& mysql_executor.is_none()@@ -733,11 +802,17 @@ async fn execute_sql_file_content_inner(started_at,&planned_statements,mysql_executor.as_mut(),+ pinned_session.id(),&mut progress,&mut emit,).await;if non_mysql_bypass {+ // Release the pinned session first. A file that opened a transaction and+ // then failed or was cancelled can still hold locks on imported tables;+ // the catalog-level restore below runs on another connection and would+ // block on them until the query timeout.+ pinned_session.release(state, request).await;let kind = bypass_kind.expect("non_mysql_bypass implies bypass_kind is set");// Use a fresh token, not the (possibly already-cancelled) import token: the// restore is catalog-level and must run even after the user cancels, or FK@@ -883,6 +958,9 @@ async fn execute_sql_file_paths_inner(return Err(error);}};+ // Non-MySQL families need one connection for the whole batch so a file-level+ // `BEGIN` / `COMMIT` cannot span pooled sessions (#12160).+ let mut pinned_session = pin_sql_file_session(state, request, mysql_executor.is_some()).await;let constraints_disabled = request.skip_relational_constraints && mysql_executor.is_some();if constraints_disabled {let executor = mysql_executor.as_mut().expect("missing MySQL executor while disabling constraints");@@ -1037,6 +1115,7 @@ async fn execute_sql_file_paths_inner(max_insert_batch_statements,import_target.as_ref(),mysql_executor.as_mut(),+ pinned_session.id(),&mut progress,&mut emit,)@@ -1073,6 +1152,7 @@ async fn execute_sql_file_paths_inner(max_insert_batch_statements,import_target.as_ref(),mysql_executor.as_mut(),+ pinned_session.id(),&mut progress,&mut emit,)@@ -1123,6 +1203,10 @@ async fn execute_sql_file_paths_inner(}}if non_mysql_bypass {+ // Same ordering as `execute_sql_file_content`: close the pinned session+ // before the catalog-level restore so an unfinished transaction cannot+ // hold locks the restore would wait on.+ pinned_session.release(state, request).await;let kind = bypass_kind.expect("non_mysql_bypass implies bypass_kind is set");// Fresh token: see the matching comment in `execute_sql_file_content`. `token`// here may already be cancelled (user cancel or terminal error), which would@@ -2012,6 +2096,7 @@ async fn execute_sql_file_statement_batch(max_insert_batch_statements: Option<usize>,import_target: Option<&SqlFileImportTarget>,mysql_executor: Option<&mut MySqlSqlFileExecutor>,+ session_id: Option<&str>,progress: &mut SqlFileExecutionProgress,emit: &mut impl FnMut(SqlFileProgress),) -> Result<(), String> {@@ -2033,6 +2118,7 @@ async fn execute_sql_file_statement_batch(started_at,&planned_statements,mysql_executor,+ session_id,progress,emit,)@@ -2489,6 +2575,7 @@ async fn execute_planned_statements_with_progress(started_at: Instant,planned_statements: &[ControlledSqlFileImportStatement],mut mysql_executor: Option<&mut MySqlSqlFileExecutor>,+ session_id: Option<&str>,progress: &mut SqlFileExecutionProgress,emit: &mut impl FnMut(SqlFileProgress),) -> Result<(), String> {@@ -2509,6 +2596,7 @@ async fn execute_planned_statements_with_progress(&mut progress.failure_count,&mut progress.affected_rows,mysql_executor.as_deref_mut(),+ session_id,emit,).await?@@ -2532,6 +2620,7 @@ async fn execute_statement_with_progress(failure_count: &mut usize,affected_rows: &mut u64,mut mysql_executor: Option<&mut MySqlSqlFileExecutor>,+ session_id: Option<&str>,emit: &mut impl FnMut(SqlFileProgress),) -> Result<bool, String> {let statement = &controlled_statement.statement;@@ -2595,8 +2684,16 @@ async fn execute_statement_with_progress(let result = {let mysql_executor = mysql_executor.as_deref_mut();- execute_sql_file_statement_with_executor(state, request, &statement.sql, token, statement_index, mysql_executor)- .await+ execute_sql_file_statement_with_executor(+ state,+ request,+ &statement.sql,+ token,+ statement_index,+ mysql_executor,+ session_id,+ )+ .await};match result {@@ -2630,6 +2727,7 @@ async fn execute_statement_with_progress(failure_count,affected_rows,mysql_executor,+ session_id,emit,).await;@@ -2671,6 +2769,7 @@ async fn execute_merged_statement_fallback_with_progress(failure_count: &mut usize,affected_rows: &mut u64,mut mysql_executor: Option<&mut MySqlSqlFileExecutor>,+ session_id: Option<&str>,emit: &mut impl FnMut(SqlFileProgress),) -> Result<bool, String> {for (offset, source_sql) in statement.source_sqls.iter().enumerate() {@@ -2710,6 +2809,7 @@ async fn execute_merged_statement_fallback_with_progress(token,statement_index,mysql_executor.as_deref_mut(),+ session_id,).await{@@ -2764,11 +2864,12 @@ async fn execute_sql_file_statement_with_executor(token: &CancellationToken,statement_index: usize,mysql_executor: Option<&mut MySqlSqlFileExecutor>,+ pinned_session_id: Option<&str>,) -> Result<QueryResult, String> {if let Some(mysql_executor) = mysql_executor {mysql_executor.execute_statement(state, request, sql, token, statement_index).await} else {- execute_sql_file_statement(state, request, sql, token, statement_index).await+ execute_sql_file_statement(state, request, sql, token, statement_index, pinned_session_id).await}}@@ -2778,6 +2879,7 @@ async fn execute_sql_file_statement(sql: &str,token: &CancellationToken,statement_index: usize,+ pinned_session_id: Option<&str>,) -> Result<QueryResult, String> {if let Some(session_id) = request.txn_session_id.as_deref() {let schema = sql_file_transaction_schema(state, request).await?;@@ -2816,7 +2918,12 @@ async fn execute_sql_file_statement(sql,request.schema.as_deref(),Some(child_token),- QueryExecutionOptions { execution_id: Some(execution_id), timeout_secs, ..Default::default() },+ QueryExecutionOptions {+ execution_id: Some(execution_id),+ timeout_secs,+ client_session_id: pinned_session_id.map(str::to_string),+ ..Default::default()+ },).await;@@ -2920,6 +3027,31 @@ mod tests {assert_eq!(manual[1].statement.sql, statements[1].sql);}+ #[test]+ fn auto_commit_file_execution_pins_one_session_unless_mysql_or_manual() {+ let mut request = SqlFileRequest {+ execution_id: "pin-session".to_string(),+ connection_id: "connection".to_string(),+ database: String::new(),+ schema: None,+ file_path: String::new(),+ continue_on_error: false,+ txn_session_id: None,+ selected_tables: None,+ part_cooldown_ms: 0,+ skip_relational_constraints: false,+ failure_log_enabled: true,+ encoding: Default::default(),+ };+ // Non-MySQL auto-commit files must keep BEGIN/COMMIT on one connection.+ assert!(should_pin_sql_file_session(&request, false));+ // MySQL already pins its own connection in MySqlSqlFileExecutor.+ assert!(!should_pin_sql_file_session(&request, true));+ // Manual transactions already own a session.+ request.txn_session_id = Some("manual-transaction".to_string());+ assert!(!should_pin_sql_file_session(&request, false));+ }+#[tokio::test]async fn manual_file_missing_session_never_falls_back_to_ordinary_execution() {let directory = tempfile::tempdir().unwrap();@@ -3934,9 +4066,15 @@ mod tests {state.configs.write().await.insert(config.id.clone(), config);let pool = crate::db::sqlite::connect_path(":memory:").await.unwrap();+ // SQL file execution pins a session-scoped pool, so the injected pool+ // must exist under that key too; the base key keeps any non-pinned path+ // (metadata probes) working.+ let session_pool_key =+ state.session_pool_key_for_test("gauss-stream", None, "sql-file-gauss-stop-on-error").await;state.update_connection_pools(|connections| {connections.insert("gauss-stream".to_string(), crate::connection::PoolKind::Sqlite(pool.clone()));+ connections.insert(session_pool_key, crate::connection::PoolKind::Sqlite(pool.clone()));}).await;diff --git a/crates/dbx-core/tests/live_postgres_sql_file_transaction.rs b/crates/dbx-core/tests/live_postgres_sql_file_transaction.rsnew file mode 100644index 0000000000..ed1ddd1f11--- /dev/null+++ b/crates/dbx-core/tests/live_postgres_sql_file_transaction.rs@@ -0,0 +1,242 @@+//! Regression coverage for #12160: a SQL file that contains its own+//! `BEGIN` / `COMMIT` must run on one pinned session, and the session must be+//! released when the file finishes so an unfinished transaction cannot stay+//! `idle in transaction` in the shared pool and block later work.+//!+//! Run explicitly with a writable PostgreSQL:+//!+//! ```sh+//! DBX_LIVE_POSTGRES_HOST=127.0.0.1 \+//! DBX_LIVE_POSTGRES_PORT=55432 \+//! DBX_LIVE_POSTGRES_USER=postgres \+//! DBX_LIVE_POSTGRES_PASSWORD=postgres \+//! DBX_LIVE_POSTGRES_DATABASE=postgres \+//! cargo test -p dbx-core --test live_postgres_sql_file_transaction -- --ignored --nocapture+//! ```+use dbx_core::connection::AppState;+use dbx_core::data::sql_file_import::execute_sql_file_content;+use dbx_core::models::connection::{ConnectionConfig, DatabaseType};+use dbx_core::query::execute_sql_statement;+use dbx_core::sql::SqlFileRequest;+use std::time::{Duration, Instant};+use tokio_util::sync::CancellationToken;++fn env_or(name: &str, default: &str) -> String {+ std::env::var(name).unwrap_or_else(|_| default.to_string())+}++fn postgres_url(database: &str, application_name: &str) -> String {+ let host = env_or("DBX_LIVE_POSTGRES_HOST", "127.0.0.1");+ let port = env_or("DBX_LIVE_POSTGRES_PORT", "5432");+ let user = env_or("DBX_LIVE_POSTGRES_USER", "postgres");+ let password = env_or("DBX_LIVE_POSTGRES_PASSWORD", "");+ format!("postgres://{user}:{password}@{host}:{port}/{database}?application_name={application_name}")+}++struct TestContext {+ state: AppState,+ database: String,+ application_name: String,+ _directory: tempfile::TempDir,+}++async fn setup() -> TestContext {+ let directory = tempfile::tempdir().unwrap();+ let storage = dbx_core::persistence::test_storage::open(&directory.path().join("state.db")).await.unwrap();+ let state = AppState::new(storage);+ let database = env_or("DBX_LIVE_POSTGRES_DATABASE", "postgres");+ let application_name = format!("dbx_sql_file_{}", &uuid::Uuid::new_v4().simple().to_string()[..16]);+ let config: ConnectionConfig = serde_json::from_value(serde_json::json!({+ "id": "file-writer",+ "name": "SQL file transaction regression",+ "db_type": DatabaseType::Postgres,+ "host": env_or("DBX_LIVE_POSTGRES_HOST", "127.0.0.1"),+ "port": env_or("DBX_LIVE_POSTGRES_PORT", "5432").parse::<u16>().unwrap(),+ "username": env_or("DBX_LIVE_POSTGRES_USER", "postgres"),+ "password": env_or("DBX_LIVE_POSTGRES_PASSWORD", ""),+ "database": database,+ "url_params": format!("application_name={application_name}"),+ "connect_timeout_secs": 10,+ "query_timeout_secs": 30,+ "keepalive_interval_secs": 0,+ }))+ .unwrap();+ state.configs.write().await.insert(config.id.clone(), config);+ TestContext { state, database, application_name, _directory: directory }+}++fn request(database: &str, execution_id: &str) -> SqlFileRequest {+ SqlFileRequest {+ execution_id: execution_id.to_string(),+ connection_id: "file-writer".to_string(),+ database: database.to_string(),+ schema: None,+ file_path: String::new(),+ continue_on_error: false,+ txn_session_id: None,+ selected_tables: None,+ part_cooldown_ms: 0,+ skip_relational_constraints: false,+ failure_log_enabled: true,+ encoding: Default::default(),+ }+}++async fn query(state: &AppState, database: &str, sql: &str) -> Result<dbx_core::types::QueryResult, String> {+ execute_sql_statement(state, "file-writer", database, sql, None, None).await+}++/// A file that opens a transaction and never commits must not leave the pooled+/// connection `idle in transaction` after the execution returns. The check runs+/// on an independent connection: querying through the same pool can land on the+/// very connection that holds the transaction, where it would read `active`.+#[tokio::test]+#[ignore = "requires a writable PostgreSQL pointed at by DBX_LIVE_POSTGRES_*"]+async fn live_postgres_sql_file_releases_unfinished_transaction() {+ let context = setup().await;+ let request = request(&context.database, "unfinished-transaction");++ execute_sql_file_content(+ &context.state,+ &request,+ "BEGIN;\nSELECT 1;\n",+ CancellationToken::new(),+ Instant::now(),+ |_| {},+ )+ .await+ .expect("SQL file execution");++ let observer = dbx_core::db::postgres::connect(+ &postgres_url(&context.database, &format!("{}_observer", context.application_name)),+ Duration::from_secs(5),+ )+ .await+ .expect("connect observer");+ let idle = dbx_core::db::postgres::execute_query_with_schema(+ &observer,+ "public",+ &format!(+ "SELECT count(*)::text FROM pg_stat_activity \+ WHERE application_name = '{}' AND state = 'idle in transaction'",+ context.application_name+ ),+ )+ .await+ .expect("inspect pg_stat_activity");+ observer.close();++ assert_eq!(+ idle.rows.first().and_then(|row| row.first()).and_then(|value| value.as_str()),+ Some("0"),+ "SQL file execution left an idle-in-transaction connection in the pool"+ );++ context.state.shutdown(Duration::from_secs(10)).await;+}++/// The exact scenario from #12160: run the same file twice, dropping the table+/// between runs. A leftover transaction from the first run blocks the drop (or+/// the second run) until the query timeout.+#[tokio::test]+#[ignore = "requires a writable PostgreSQL pointed at by DBX_LIVE_POSTGRES_*"]+async fn live_postgres_sql_file_with_explicit_transaction_runs_twice() {+ let context = setup().await;+ let request = request(&context.database, "two-runs");+ let table = format!("dbx_file_txn_{}", &uuid::Uuid::new_v4().simple().to_string()[..16]);+ let file = format!(+ "BEGIN;\n\+ CREATE TABLE public.{table} (\n\+ id int8 GENERATED ALWAYS AS IDENTITY PRIMARY KEY,\n\+ item_code varchar(64) NOT NULL DEFAULT '',\n\+ title varchar(255) NOT NULL DEFAULT '',\n\+ content text NOT NULL DEFAULT ''\n\+ );\n\+ INSERT INTO public.{table} (item_code, title, content)\n\+ VALUES ('PROD-001', 'item', '<div>content</div>');\n\+ ALTER TABLE public.{table} ADD COLUMN IF NOT EXISTS extra_tag varchar(64) NOT NULL DEFAULT '';\n\+ ALTER TABLE public.{table} RENAME TO {table}_old;\n\+ CREATE TABLE public.{table} (\n\+ id int8 GENERATED ALWAYS AS IDENTITY PRIMARY KEY,\n\+ item_code varchar(64) NOT NULL DEFAULT '',\n\+ title varchar(255) NOT NULL DEFAULT '',\n\+ content text NOT NULL DEFAULT '',\n\+ extra_tag varchar(64) NOT NULL DEFAULT ''\n\+ );\n\+ INSERT INTO public.{table} (item_code, title, content, extra_tag)\n\+ SELECT item_code, title, content, extra_tag FROM public.{table}_old ORDER BY id;\n\+ DROP TABLE public.{table}_old;\n\+ COMMIT;\n"+ );++ execute_sql_file_content(&context.state, &request, &file, CancellationToken::new(), Instant::now(), |_| {})+ .await+ .expect("first SQL file execution");+ query(&context.state, &context.database, &format!("DROP TABLE public.{table}")).await.expect("drop between runs");++ let second = tokio::time::timeout(+ Duration::from_secs(20),+ execute_sql_file_content(&context.state, &request, &file, CancellationToken::new(), Instant::now(), |_| {}),+ )+ .await+ .expect("second SQL file execution must not hang");+ second.expect("second SQL file execution");+ query(&context.state, &context.database, &format!("DROP TABLE public.{table}")).await.expect("cleanup");++ context.state.shutdown(Duration::from_secs(10)).await;+}++/// `skip_relational_constraints` restores triggers on the shared pool after the+/// import. A file that leaves a transaction open (here: `BEGIN` + `LOCK TABLE`,+/// then a failing statement) must not keep the pinned session's lock while that+/// restore runs, or the restore blocks until the query timeout.+#[tokio::test]+#[ignore = "requires a writable PostgreSQL pointed at by DBX_LIVE_POSTGRES_*"]+async fn live_postgres_sql_file_releases_session_before_constraint_restore() {+ let context = setup().await;+ let table = format!("dbx_file_lock_{}", &uuid::Uuid::new_v4().simple().to_string()[..16]);+ query(+ &context.state,+ &context.database,+ &format!("CREATE TABLE public.{table} (id int PRIMARY KEY, parent_id int REFERENCES public.{table}(id))"),+ )+ .await+ .expect("create probe table");++ let mut request = request(&context.database, "constraint-restore");+ request.skip_relational_constraints = true;+ let file = format!(+ "BEGIN;\n\+ LOCK TABLE public.{table} IN ACCESS EXCLUSIVE MODE;\n\+ SELECT missing_column FROM public.{table};\n"+ );++ let started = Instant::now();+ let result = tokio::time::timeout(+ Duration::from_secs(15),+ execute_sql_file_content(&context.state, &request, &file, CancellationToken::new(), Instant::now(), |_| {}),+ )+ .await+ .expect("constraint restore must not block on the pinned session");+ assert!(result.is_err(), "the missing column must fail the import");+ assert!(started.elapsed() < Duration::from_secs(15), "constraint restore waited on the pinned session's lock");++ let disabled = query(+ &context.state,+ &context.database,+ &format!(+ "SELECT count(*)::text FROM pg_trigger \+ WHERE tgrelid = 'public.{table}'::regclass AND tgenabled = 'D'"+ ),+ )+ .await+ .expect("inspect trigger state");+ assert_eq!(+ disabled.rows.first().and_then(|row| row.first()).and_then(|value| value.as_str()),+ Some("0"),+ "constraint restore did not re-enable the probe table's triggers"+ );++ query(&context.state, &context.database, &format!("DROP TABLE public.{table}")).await.expect("cleanup");+ context.state.shutdown(Duration::from_secs(10)).await;+}