Spaces:
Sleeping
Sleeping
fix: add pool_pre_ping and pool_recycle to prevent stale Neon connection errors
Browse files- app/services/database.py +5 -1
app/services/database.py
CHANGED
|
@@ -17,7 +17,11 @@ if "localhost" not in DATABASE_URL and "sslmode" not in DATABASE_URL:
|
|
| 17 |
else:
|
| 18 |
DATABASE_URL += "?sslmode=require"
|
| 19 |
|
| 20 |
-
engine= create_engine(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
SessionLocal= sessionmaker(autocommit= False, autoflush= False, bind= engine)
|
| 22 |
Base= declarative_base()
|
| 23 |
|
|
|
|
| 17 |
else:
|
| 18 |
DATABASE_URL += "?sslmode=require"
|
| 19 |
|
| 20 |
+
engine = create_engine(
|
| 21 |
+
DATABASE_URL,
|
| 22 |
+
pool_pre_ping=True, # Test connection health before use; silently replaces dead connections
|
| 23 |
+
pool_recycle=300, # Recycle connections every 5 min, before Neon's idle timeout closes them
|
| 24 |
+
)
|
| 25 |
SessionLocal= sessionmaker(autocommit= False, autoflush= False, bind= engine)
|
| 26 |
Base= declarative_base()
|
| 27 |
|