Implement database connection pooling with context manager pattern
- Added DBUtils PooledDB for intelligent connection pooling - Created db_pool.py with lazy-initialized connection pool (max 20 connections) - Added db_connection_context() context manager for safe connection handling - Refactored all 19 database operations to use context manager pattern - Ensures proper connection cleanup and exception handling - Prevents connection exhaustion on POST requests - Added logging configuration for debugging Changes: - py_app/app/db_pool.py: New connection pool manager - py_app/app/logging_config.py: Centralized logging - py_app/app/__init__.py: Updated to use connection pool - py_app/app/routes.py: Refactored all DB operations to use context manager - py_app/app/settings.py: Updated settings handlers - py_app/requirements.txt: Added DBUtils dependency This solves the connection timeout issues experienced with the fgscan page.
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
from flask import Flask
|
||||
from datetime import datetime
|
||||
import os
|
||||
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
app.config['SECRET_KEY'] = 'your_secret_key'
|
||||
|
||||
# Initialize logging first
|
||||
from app.logging_config import setup_logging
|
||||
log_dir = os.path.join(app.instance_path, '..', 'logs')
|
||||
logger = setup_logging(app=app, log_dir=log_dir)
|
||||
logger.info("Flask app initialization started")
|
||||
|
||||
# Configure session persistence
|
||||
from datetime import timedelta
|
||||
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=7)
|
||||
@@ -15,14 +22,21 @@ def create_app():
|
||||
# Set max upload size to 10GB for large database backups
|
||||
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 * 1024 # 10GB
|
||||
|
||||
# Note: Database connection pool is lazily initialized on first use
|
||||
# This is to avoid trying to read configuration before it's created
|
||||
# during application startup. See app.db_pool.get_db_pool() for details.
|
||||
logger.info("Database connection pool will be lazily initialized on first use")
|
||||
|
||||
# Application uses direct MariaDB connections via external_server.conf
|
||||
# No SQLAlchemy ORM needed - all database operations use raw SQL
|
||||
# Connection pooling via DBUtils prevents connection exhaustion
|
||||
|
||||
logger.info("Registering Flask blueprints...")
|
||||
from app.routes import bp as main_bp, warehouse_bp
|
||||
from app.daily_mirror import daily_mirror_bp
|
||||
app.register_blueprint(main_bp, url_prefix='/')
|
||||
app.register_blueprint(warehouse_bp, url_prefix='/warehouse')
|
||||
app.register_blueprint(daily_mirror_bp)
|
||||
logger.info("Blueprints registered successfully")
|
||||
|
||||
# Add 'now' function to Jinja2 globals
|
||||
app.jinja_env.globals['now'] = datetime.now
|
||||
|
||||
Reference in New Issue
Block a user