fix: disable autoconnect and set pool timeout on PooledPostgresqlDatabase (#80) #87
@ -23,8 +23,8 @@ if DATABASE_TYPE.lower() == 'postgresql':
|
|||||||
port=int(os.environ.get('POSTGRES_PORT', '5432')),
|
port=int(os.environ.get('POSTGRES_PORT', '5432')),
|
||||||
max_connections=20,
|
max_connections=20,
|
||||||
stale_timeout=300, # 5 minutes
|
stale_timeout=300, # 5 minutes
|
||||||
timeout=0,
|
timeout=5,
|
||||||
autoconnect=True,
|
autoconnect=False,
|
||||||
autorollback=True # Automatically rollback failed transactions
|
autorollback=True # Automatically rollback failed transactions
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
|||||||
13
app/main.py
13
app/main.py
@ -7,6 +7,8 @@ from fastapi import Depends, FastAPI, Request
|
|||||||
from fastapi.openapi.docs import get_swagger_ui_html
|
from fastapi.openapi.docs import get_swagger_ui_html
|
||||||
from fastapi.openapi.utils import get_openapi
|
from fastapi.openapi.utils import get_openapi
|
||||||
|
|
||||||
|
from .db_engine import db
|
||||||
|
|
||||||
# from fastapi.openapi.docs import get_swagger_ui_html
|
# from fastapi.openapi.docs import get_swagger_ui_html
|
||||||
# from fastapi.openapi.utils import get_openapi
|
# from fastapi.openapi.utils import get_openapi
|
||||||
|
|
||||||
@ -67,6 +69,17 @@ app = FastAPI(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def db_connection_middleware(request: Request, call_next):
|
||||||
|
db.connect(reuse_if_open=True)
|
||||||
|
try:
|
||||||
|
response = await call_next(request)
|
||||||
|
finally:
|
||||||
|
if not db.is_closed():
|
||||||
|
db.close()
|
||||||
|
return response
|
||||||
|
|
||||||
|
|
||||||
logger.info(f"Starting up now...")
|
logger.info(f"Starting up now...")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user