Skip to content

Commit

Permalink
修复了sqlalchemy 在运行Fastapi后端服务一段时间后数据库连接超时的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
296711867 committed May 26, 2024
1 parent 187d08b commit 8197423
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions dfs_generate/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,12 @@ def delete_${router_name}_by_id(id: int) -> schema.Result[schema.$table]:
from sqlmodel import create_engine
db_uri = "{uri}"
engine = create_engine(db_uri)
# 不使用连接池
engine = create_engine(db_uri, pool_pre_ping=True, pool_recycle=-1)
# 设置sqlalchemy 回收链接的时间为10分钟
# engine = create_engine(db_uri, pool_recycle=600)
# 在每次使用连接前都会进行ping操作,并且连接的最大空闲时间为30分钟(1800秒)后才会被回收
# engine = create_engine(db_uri, pool_pre_ping=True,pool_recycle=1800)
"""

SQLMODEL_MAIN = (
Expand All @@ -153,7 +157,7 @@ def delete_${router_name}_by_id(id: int) -> schema.Result[schema.$table]:
if __name__ == '__main__':
import uvicorn
uvicorn.run("main:app", reload=True, port=5000)
uvicorn.run("__main__:app", reload=False, port=5000)
"""
% DOC_DESC
)
Expand Down

0 comments on commit 8197423

Please sign in to comment.