diff --git a/config.py b/config.py index 99c25b5a..69ed087a 100644 --- a/config.py +++ b/config.py @@ -31,6 +31,12 @@ class Config: # MySQL SQLALCHEMY_DATABASE_URI = os.getenv('SQLALCHEMY_DATABASE_URI') SQLALCHEMY_TRACK_MODIFICATIONS = False + SQLALCHEMY_ENGINE_OPTIONS = { + 'pool_size': 10, + 'pool_recycle': 280, + 'pool_pre_ping': True, + 'pool_timeout': 30, + } # Auth0 SESSION_COOKIE_SECURE = True SESSION_COOKIE_HTTPONLY = True diff --git a/controllers/__pycache__/user.cpython-312.pyc b/controllers/__pycache__/user.cpython-312.pyc index 69b00a52..cd73e808 100644 Binary files a/controllers/__pycache__/user.cpython-312.pyc and b/controllers/__pycache__/user.cpython-312.pyc differ diff --git a/controllers/user.py b/controllers/user.py index 489f2b67..9144a3c4 100644 --- a/controllers/user.py +++ b/controllers/user.py @@ -21,6 +21,7 @@ import lib.argument_validation as av # external from flask import Flask, render_template, jsonify, request, render_template_string, send_from_directory, redirect, url_for, session, Blueprint, current_app from flask_sqlalchemy import SQLAlchemy +from sqlalchemy import exc from flask_wtf.csrf import generate_csrf from werkzeug.exceptions import BadRequest from extensions import oauth # db, @@ -34,6 +35,21 @@ db = SQLAlchemy() routes_user = Blueprint('routes_user', __name__) +def handle_db_disconnect(f): + @wraps(f) + def decorated_function(*args, **kwargs): + try: + return f(*args, **kwargs) + except exc.OperationalError as e: + if "MySQL server has gone away" in str(e): + # Close the session and create a new connection + db.session.remove() + db.session.rollback() + # Retry the operation + return f(*args, **kwargs) + raise + return decorated_function + # User authentication @routes_user.route("/login", methods=['POST', 'OPTIONS']) def login(): @@ -109,6 +125,7 @@ def login(): @routes_user.route("/login_callback") # / +@handle_db_disconnect def login_callback(): try: error_state = request.args.get(Model_View_User.FLAG_ERROR_OAUTH) @@ -185,6 +202,7 @@ def logout(): return redirect(url_logout) @routes_user.route("/logout_callback") # / +@handle_db_disconnect def logout_callback(): return redirect(url_for('routes_core.home')) try: