employeed Sqlalchemy - pool pre ping to help with db connect errors.

This commit is contained in:
Bryan Van Deusen
2025-08-03 22:46:22 -04:00
parent c480a176c2
commit 68cff4e028
3 changed files with 7 additions and 16 deletions
+2 -15
View File
@@ -13,26 +13,13 @@ def create_app(config_class='config.Config'):
app = Flask(__name__)
app.config.from_object(config_class)
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = config_class.SQLALCHEMY_ENGINE_OPTIONS
db.init_app(app)
migrate.init_app(app, db)
login_manager.init_app(app)
login_manager.login_view = 'auth.login'
with app.app_context():
# Enable connection ping only for PostgreSQL
if app.config['SQLALCHEMY_DATABASE_URI'].startswith("postgresql"):
from sqlalchemy import event, text
from sqlalchemy.exc import DisconnectionError
@event.listens_for(db.engine, "engine_connect")
def ping_connection(connection, branch):
if branch:
return
try:
connection.execution_options(isolation_level="AUTOCOMMIT").scalar(text("SELECT 1"))
except:
raise DisconnectionError()
from app.main import main
from app.auth import auth
+2 -1
View File
@@ -21,7 +21,8 @@ def index():
background_url = None
if image:
filename = image.filepath.replace('/images/', '')
path = image.thumb_path or image.filepath
filename = path.replace('/images/', '')
background_url = url_for('main.serve_image', filename=filename)
return render_template('index.html', background_url=background_url)
+3
View File
@@ -28,3 +28,6 @@ class Config:
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_ENGINE_OPTIONS = {
"pool_pre_ping": True
}