diff --git a/stock-html/routes/admin.py b/stock-html/routes/admin.py index 2e84ff3..9545cb5 100644 --- a/stock-html/routes/admin.py +++ b/stock-html/routes/admin.py @@ -782,7 +782,6 @@ def kline_sync_status(): total_stocks = cur.fetchone()[0] cur.close() - put_db(conn) is_running = _is_kline_sync_running() @@ -858,6 +857,8 @@ def kline_sync_status(): }) except Exception as e: return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) # ========== 定时任务监控 ========== @@ -1301,7 +1302,6 @@ def admin_scan_status(): triggered = cur.fetchone()[0] cur.close() - put_db(conn) is_running = _is_scan_running() @@ -1350,3 +1350,5 @@ def admin_scan_status(): }) except Exception as e: return jsonify({'success': False, 'error': str(e)}), 500 + finally: + put_db(conn) diff --git a/stock-html/routes/market.py b/stock-html/routes/market.py index 4144f29..f96e5a5 100644 --- a/stock-html/routes/market.py +++ b/stock-html/routes/market.py @@ -305,7 +305,7 @@ def get_fund_flow_rank(): return jsonify({'success': True, 'data': flow_data, 'total': len(flow_data), 'source': 'cache'}) finally: - _put_db(conn) + put_db(_conn) return jsonify({'success': True, 'data': [], 'total': 0}) except Exception as e: @@ -324,6 +324,7 @@ def get_fundamental(stock_code): realtime_data = None signal_data = None + conn = None try: conn = get_db() if conn: @@ -388,10 +389,10 @@ def get_fundamental(stock_code): 'indicators': ind, 'triggered_count': sig_row[2] or 0, } - - put_db(conn) except Exception as e: print(f"获取数据失败: {e}") + finally: + put_db(conn) # 优先从数据库获取当日缓存 cached = db_get_fundamental(stock_code)