Skip to content

Commit 3961309

Browse files
committed
feat: 配置文件新增数据库连接池相关配置
1 parent ebf18e7 commit 3961309

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

dash-fastapi-backend/.env.dev

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ DB_PASSWORD = 'mysqlroot'
4242
DB_DATABASE = 'dash-fastapi'
4343
# 是否开启sqlalchemy日志
4444
DB_ECHO = true
45+
# 允许溢出连接池大小的最大连接数
46+
DB_MAX_OVERFLOW = 10
47+
# 连接池大小,0表示连接数无限制
48+
DB_POOL_SIZE = 50
49+
# 连接回收时间(单位:秒)
50+
DB_POOL_RECYCLE = 3600
51+
# 连接池中没有线程可用时,最多等待的时间(单位:秒)
52+
DB_POOL_TIMEOUT = 30
4553

4654
# -------- Redis配置 --------
4755
# Redis主机

dash-fastapi-backend/.env.prod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ DB_PASSWORD = 'mysqlroot'
4242
DB_DATABASE = 'dash-fastapi'
4343
# 是否开启sqlalchemy日志
4444
DB_ECHO = true
45+
# 允许溢出连接池大小的最大连接数
46+
DB_MAX_OVERFLOW = 10
47+
# 连接池大小,0表示连接数无限制
48+
DB_POOL_SIZE = 50
49+
# 连接回收时间(单位:秒)
50+
DB_POOL_RECYCLE = 3600
51+
# 连接池中没有线程可用时,最多等待的时间(单位:秒)
52+
DB_POOL_TIMEOUT = 30
4553

4654
# -------- Redis配置 --------
4755
# Redis主机

dash-fastapi-backend/config/database.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
f"{DataBaseConfig.db_host}:{DataBaseConfig.db_port}/{DataBaseConfig.db_database}"
99

1010
engine = create_engine(
11-
SQLALCHEMY_DATABASE_URL, echo=DataBaseConfig.db_echo
11+
SQLALCHEMY_DATABASE_URL,
12+
echo=DataBaseConfig.db_echo,
13+
max_overflow=DataBaseConfig.db_max_overflow,
14+
pool_size=DataBaseConfig.db_pool_size,
15+
pool_recycle=DataBaseConfig.db_pool_recycle,
16+
pool_timeout=DataBaseConfig.db_pool_timeout
1217
)
1318
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
1419
Base = declarative_base()

dash-fastapi-backend/config/env.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class DataBaseSettings(BaseSettings):
4141
db_password: str = 'mysqlroot'
4242
db_database: str = 'dash-fastapi'
4343
db_echo: bool = True
44+
db_max_overflow: int = 10
45+
db_pool_size: int = 50
46+
db_pool_recycle: int = 3600
47+
db_pool_timeout: int = 30
4448

4549

4650
class RedisSettings(BaseSettings):

0 commit comments

Comments
 (0)