diff --git a/apps/desktop/src-tauri/src/app_state.rs b/apps/desktop/src-tauri/src/app_state.rs index de18022..ca5fd92 100644 --- a/apps/desktop/src-tauri/src/app_state.rs +++ b/apps/desktop/src-tauri/src/app_state.rs @@ -197,6 +197,7 @@ impl AppState { model_dynamic_repository: Mutex::new(None), video_generation_repository: Mutex::new(None), conversation_repository: Mutex::new(None), + outfit_photo_generation_repository: Mutex::new(None), performance_monitor: Mutex::new(PerformanceMonitor::new()), event_bus_manager: Arc::new(EventBusManager::new()), }; diff --git a/apps/desktop/src-tauri/src/business/services/tests/mod.rs b/apps/desktop/src-tauri/src/business/services/tests/mod.rs index 6fa1266..b7e97d4 100644 --- a/apps/desktop/src-tauri/src/business/services/tests/mod.rs +++ b/apps/desktop/src-tauri/src/business/services/tests/mod.rs @@ -2,7 +2,6 @@ pub mod draft_parser_tests; pub mod cloud_upload_service_tests; pub mod watermark_tests; pub mod template_service_tests; -pub mod template_foreign_key_test; // 测试工具函数 pub mod test_utils { diff --git a/apps/desktop/src-tauri/src/infrastructure/connection_pool.rs b/apps/desktop/src-tauri/src/infrastructure/connection_pool.rs index c97cb82..426a8e6 100644 --- a/apps/desktop/src-tauri/src/infrastructure/connection_pool.rs +++ b/apps/desktop/src-tauri/src/infrastructure/connection_pool.rs @@ -231,18 +231,23 @@ impl ConnectionPool { fn try_acquire_from_pool(&self) -> Result> { let mut connections = self.connections.lock().map_err(|e| anyhow!("获取连接池锁失败: {}", e))?; - // 查找可用的连接 - for pooled_conn in connections.iter_mut() { + // 清理过期连接 + connections.retain(|conn| !conn.is_expired(self.config.idle_timeout)); + + // 查找可用的连接并移除它 + let mut found_index = None; + for (i, pooled_conn) in connections.iter().enumerate() { if !pooled_conn.is_busy && !pooled_conn.is_expired(self.config.idle_timeout) { - pooled_conn.mark_used(); - // 这里我们需要移除连接,但由于借用检查器的限制,我们需要重新设计 - // 暂时返回 None,后续优化 + found_index = Some(i); break; } } - // 清理过期连接 - connections.retain(|conn| !conn.is_expired(self.config.idle_timeout)); + if let Some(index) = found_index { + let pooled_conn = connections.remove(index).unwrap(); + println!("从连接池获取现有连接,剩余连接数: {}", connections.len()); + return Ok(Some(pooled_conn.connection)); + } // 如果没有可用连接且未达到最大连接数,创建新连接 if connections.len() < self.config.max_connections { @@ -259,6 +264,7 @@ impl ConnectionPool { } } + println!("连接池已满且无可用连接,当前连接数: {}", connections.len()); Ok(None) } @@ -271,6 +277,8 @@ impl ConnectionPool { stats.total_returned += 1; stats.active_connections = stats.active_connections.saturating_sub(1); stats.idle_connections += 1; + + println!("连接已归还到池中,当前连接数: {}", connections.len()); } /// 获取连接池统计信息 diff --git a/apps/desktop/src-tauri/src/infrastructure/database.rs b/apps/desktop/src-tauri/src/infrastructure/database.rs index ce2740d..600a3a1 100644 --- a/apps/desktop/src-tauri/src/infrastructure/database.rs +++ b/apps/desktop/src-tauri/src/infrastructure/database.rs @@ -213,10 +213,10 @@ impl Database { } else { println!("🚨 强制启用默认连接池配置以避免死锁"); let default_config = ConnectionPoolConfig { - max_connections: 10, // 适中的最大连接数 - min_connections: 3, // 保持最小连接数 + max_connections: 5, // 减少最大连接数,SQLite不适合太多并发 + min_connections: 2, // 减少最小连接数 connection_timeout: Duration::from_secs(30), // 连接超时 - acquire_timeout: Duration::from_secs(30), // 增加获取超时 + acquire_timeout: Duration::from_secs(10), // 减少获取超时,快速失败 idle_timeout: Duration::from_secs(300), // 5分钟空闲超时 };