|
9 | 9 | SQLALCHEMY_APPLICATION_NAME = os.getenv("SQLALCHEMY_APPLICATION_NAME", "db") |
10 | 10 | SQLALCHEMY_APPLICATION_VERSION = os.getenv("SQLALCHEMY_APPLICATION_VERSION", "-") |
11 | 11 |
|
12 | | - |
13 | | -engine = create_engine( |
14 | | - os.environ.get('SQLALCHEMY_URL', 'postgresql://postgres:123456@localhost'), |
| 12 | +_create_engine_kwargs = dict( |
15 | 13 | future=True, |
16 | 14 | connect_args={ |
17 | 15 | "options": "-c timezone=utc", |
18 | 16 | "application_name": f'{SQLALCHEMY_APPLICATION_NAME} {SQLALCHEMY_APPLICATION_VERSION}'[:64], |
19 | 17 | }, |
20 | 18 | echo=bool(os.environ.get('SQLALCHEMY_ECHO')), |
21 | | - pool_size=int(os.environ.get('SQLALCHEMY_POOL_SIZE', 10)), |
22 | | - max_overflow=int(os.environ.get('SQLALCHEMY_MAX_OVERFLOW', 20)), |
23 | | - pool_timeout=int(os.environ.get('SQLALCHEMY_POOL_TIMEOUT', 30)), |
| 19 | +) |
| 20 | +if os.getenv("SQLALCHEMY_POOLCLASS_NULLPOOL") == "yes": |
| 21 | + from sqlalchemy.pool import NullPool |
| 22 | + _create_engine_kwargs.update( |
| 23 | + poolclass=NullPool, |
| 24 | + pool_pre_ping=True, |
| 25 | + ) |
| 26 | +else: |
| 27 | + _create_engine_kwargs.update( |
| 28 | + dict( |
| 29 | + pool_size=int(os.environ.get('SQLALCHEMY_POOL_SIZE', 10)), |
| 30 | + max_overflow=int(os.environ.get('SQLALCHEMY_MAX_OVERFLOW', 20)), |
| 31 | + pool_timeout=int(os.environ.get('SQLALCHEMY_POOL_TIMEOUT', 30)), |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | +engine = create_engine( |
| 36 | + os.environ.get('SQLALCHEMY_URL', 'postgresql://postgres:123456@localhost'), |
| 37 | + **_create_engine_kwargs, |
24 | 38 | ) |
25 | 39 | _sessionmaker = sessionmaker(bind=engine, future=True, autoflush=False, autocommit=False) |
26 | 40 |
|
|
0 commit comments