Skip to content

Commit a5e8ca2

Browse files
Update main.py
1 parent 3ed5ad1 commit a5e8ca2

File tree

1 file changed

+6
-5
lines changed
  • frameworks/Python/aiohttp/app

1 file changed

+6
-5
lines changed

frameworks/Python/aiohttp/app/main.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import asyncpg
55
from aiohttp import web
66
from sqlalchemy.engine.url import URL
7-
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
8-
from sqlalchemy.orm import sessionmaker
7+
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
98

109
from .views import (
1110
json,
@@ -48,15 +47,17 @@ async def db_ctx(app: web.Application):
4847
print(f'connection pool: min size: {min_size}, max size: {max_size}, orm: {CONNECTION_ORM}')
4948
if CONNECTION_ORM:
5049
dsn = pg_dsn('asyncpg')
51-
engine = create_async_engine(dsn, future=True, pool_size=max_size)
52-
app['db_session'] = sessionmaker(engine, class_=AsyncSession)
50+
engine = create_async_engine(dsn, pool_size=max_size)
51+
app['db_session'] = async_sessionmaker(engine)
5352
else:
5453
dsn = pg_dsn()
5554
app['pg'] = await asyncpg.create_pool(dsn=dsn, min_size=min_size, max_size=max_size, loop=app.loop)
5655

5756
yield
5857

59-
if not CONNECTION_ORM:
58+
if CONNECTION_ORM:
59+
await app['db_session'].dispose()
60+
else:
6061
await app['pg'].close()
6162

6263

0 commit comments

Comments
 (0)