|
|
@@ -5,6 +5,7 @@ from datetime import datetime
|
|
|
from uuid import uuid1
|
|
|
import mysql.connector
|
|
|
from mysql.connector import pooling, Error
|
|
|
+import threading
|
|
|
from concurrent.futures import ThreadPoolExecutor, TimeoutError
|
|
|
from config import milvus_uri, mysql_config
|
|
|
|
|
|
@@ -197,19 +198,48 @@ class MysqlOperate:
|
|
|
从连接池中获取一个连接
|
|
|
:return: 数据库连接对象
|
|
|
"""
|
|
|
- try:
|
|
|
- with ThreadPoolExecutor() as executor:
|
|
|
- future = executor.submit(POOL.get_connection)
|
|
|
- connection = future.result(timeout=5.0) # 设置超时时间为5秒
|
|
|
-
|
|
|
- logger.info("成功从连接池获取连接")
|
|
|
+ # try:
|
|
|
+ # with ThreadPoolExecutor() as executor:
|
|
|
+ # future = executor.submit(POOL.get_connection)
|
|
|
+ # connection = future.result(timeout=5.0) # 设置超时时间为5秒
|
|
|
+ # logger.info("成功从连接池获取连接")
|
|
|
+ # return connection, "success"
|
|
|
+ # except TimeoutError:
|
|
|
+ # logger.error("获取mysql数据库连接池超时")
|
|
|
+ # return None, "mysql获取连接池超时"
|
|
|
+ # except errors.InterfaceError as e:
|
|
|
+ # logger.error(f"MySQL 接口异常:{e}")
|
|
|
+ # return None, "mysql接口异常"
|
|
|
+ # except errors.OperationalError as e:
|
|
|
+ # logger.error(f"MySQL 操作错误:{e}")
|
|
|
+ # return None, "mysql 操作错误"
|
|
|
+ # except Error as e:
|
|
|
+ # logger.error(f"无法从连接池获取连接: {e}")
|
|
|
+ # return None, str(e)
|
|
|
+ connection = None
|
|
|
+ event = threading.Event()
|
|
|
+
|
|
|
+ def target():
|
|
|
+ nonlocal connection
|
|
|
+ try:
|
|
|
+ connection = POOL.get_connection()
|
|
|
+ finally:
|
|
|
+ event.set()
|
|
|
+
|
|
|
+ thread = threading.Thread(target=target)
|
|
|
+ thread.start()
|
|
|
+ event.wait(timeout=5)
|
|
|
+
|
|
|
+ if thread.is_alive():
|
|
|
+ # 超时处理
|
|
|
+ logger.error("获取连接超时")
|
|
|
+ return None, "获取连接超时"
|
|
|
+ else:
|
|
|
+ if connection:
|
|
|
return connection, "success"
|
|
|
- except TimeoutError:
|
|
|
- logger.error("获取mysql数据库连接池超时")
|
|
|
- return None, "mysql获取连接池超时"
|
|
|
- except Error as e:
|
|
|
- logger.error(f"无法从连接池获取连接: {e}")
|
|
|
- return None, str(e)
|
|
|
+ else:
|
|
|
+ logger.error("获取连接失败")
|
|
|
+ return None, "获取连接失败"
|
|
|
|
|
|
def insert_to_slice(self, docs, knowledge_id, doc_id):
|
|
|
"""
|