fix: 修复数据库重复初始化问题
问题修复: - 修复AppState.get_database()方法重复创建数据库实例的问题 - 使用OnceLock确保数据库单例模式 - 修复video_classification_commands中重复调用get_database()的问题 技术改进: - 避免数据库循环初始化导致的日志重复输出 - 提升应用启动性能 - 确保数据库连接的一致性 这个修复解决了启动时数据库初始化日志循环输出的问题。
This commit is contained in:
parent
b8dfaf8af8
commit
849dc8317c
|
|
@ -67,14 +67,13 @@ impl AppState {
|
||||||
let _ = self.initialize_database();
|
let _ = self.initialize_database();
|
||||||
}
|
}
|
||||||
|
|
||||||
let db_guard = self.database.lock().unwrap();
|
// 使用静态数据库实例,避免重复初始化
|
||||||
if let Some(ref _database) = *db_guard {
|
use std::sync::OnceLock;
|
||||||
// 创建一个新的数据库连接,使用相同的路径
|
static DATABASE_INSTANCE: OnceLock<Arc<Database>> = OnceLock::new();
|
||||||
|
|
||||||
|
DATABASE_INSTANCE.get_or_init(|| {
|
||||||
Arc::new(Database::new().unwrap())
|
Arc::new(Database::new().unwrap())
|
||||||
} else {
|
}).clone()
|
||||||
// 如果仍然失败,创建一个新的
|
|
||||||
Arc::new(Database::new().unwrap())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 用于测试的构造函数
|
/// 用于测试的构造函数
|
||||||
|
|
|
||||||
|
|
@ -19,10 +19,10 @@ static QUEUE_INSTANCE: tokio::sync::OnceCell<Arc<VideoClassificationQueue>> = to
|
||||||
async fn get_queue_instance(state: &AppState) -> Arc<VideoClassificationQueue> {
|
async fn get_queue_instance(state: &AppState) -> Arc<VideoClassificationQueue> {
|
||||||
QUEUE_INSTANCE.get_or_init(|| async {
|
QUEUE_INSTANCE.get_or_init(|| async {
|
||||||
let database = state.get_database();
|
let database = state.get_database();
|
||||||
|
|
||||||
let video_repo = Arc::new(VideoClassificationRepository::new(database.clone()));
|
let video_repo = Arc::new(VideoClassificationRepository::new(database.clone()));
|
||||||
let ai_classification_repo = Arc::new(AiClassificationRepository::new(database.clone()));
|
let ai_classification_repo = Arc::new(AiClassificationRepository::new(database.clone()));
|
||||||
let material_repo = Arc::new(MaterialRepository::new(state.get_database().get_connection()).unwrap());
|
let material_repo = Arc::new(MaterialRepository::new(database.get_connection()).unwrap());
|
||||||
|
|
||||||
let gemini_config = Some(GeminiConfig::default());
|
let gemini_config = Some(GeminiConfig::default());
|
||||||
let service = Arc::new(VideoClassificationService::new(
|
let service = Arc::new(VideoClassificationService::new(
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue