Skip to content

修复了sqlalchemy 在运行Fastapi后端服务一段时间后数据库连接超时的问题 #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

296711867
Copy link
Contributor

1、修改启动参数
把uvicorn.run("main:app", reload=False, port=5000)中的main修改为了__main__

2、修改对数据库连接的代码
打包后,sqlalchemy 在运行Fastapi后端服务一段时间后数据库连接超时的问题
修改为:

# 不使用连接池
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)

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.99%. Comparing base (187d08b) to head (8197423).

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master      #67   +/-   ##
=======================================
  Coverage   73.99%   73.99%           
=======================================
  Files           7        7           
  Lines         323      323           
=======================================
  Hits          239      239           
  Misses         84       84           
Flag Coverage Δ
smart-tests 73.99% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zy7y
Copy link
Owner

zy7y commented May 27, 2024

1、修改启动参数 把uvicorn.run("main:app", reload=False, port=5000)中的main修改为了__main__

2、修改对数据库连接的代码 打包后,sqlalchemy 在运行Fastapi后端服务一段时间后数据库连接超时的问题 修改为:

# 不使用连接池
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)

这两步看下来是非必须的,说的打包是打成桌面端?暂不合入

@zy7y zy7y closed this Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants