数据库-SQLAlchemy 重要参数详解

sqlalchemy 连接参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 'pool_timeout' is the maximum number of seconds to wait when retrieving a
# new connection from the pool. After the specified amount of time, an
# exception will be thrown.
pool_timeout=30, # 30 seconds
# 'pool_recycle' is the maximum number of seconds a connection can persist.
# Connections that live longer than the specified amount of time will be
# re-established
pool_recycle=1800, # 30 minutes
# Pool size is the maximum number of permanent connections to keep.
pool_size=5,
# Temporarily exceeds the set pool_size if no connections are available.
max_overflow=2,
# The total number of concurrent connections for your application will be
# a total of pool_size and max_overflow.

如上所知如下:

  1. 应用程序最大db并发量 = pool_size + max_overflow

  2. 池子中的空闲conn 最大生存时间 1800s,超过的将被回收重建,此时client会和db主动断连

  3. 应用程序如果超过30s未从池子中取出可用conn,程序将触发exception