diff --git a/frameworks/Python/aiohttp/app/main.py b/frameworks/Python/aiohttp/app/main.py index 6ed65ed19d7..ef2b2fd4f8b 100644 --- a/frameworks/Python/aiohttp/app/main.py +++ b/frameworks/Python/aiohttp/app/main.py @@ -37,6 +37,11 @@ def pg_dsn(dialect=None) -> str: ) return url.render_as_string(hide_password=False) +class NoResetConnection(asyncpg.Connection): + __slots__ = () + + def get_reset_query(self): + return '' async def db_ctx(app: web.Application): # number of gunicorn workers = multiprocessing.cpu_count() as per gunicorn_conf.py @@ -52,7 +57,7 @@ async def db_ctx(app: web.Application): app['db_session'] = async_sessionmaker(engine) else: dsn = pg_dsn() - app['pg'] = await asyncpg.create_pool(dsn=dsn, min_size=min_size, max_size=max_size, loop=app.loop) + app['pg'] = await asyncpg.create_pool(dsn=dsn, min_size=min_size, max_size=max_size, loop=app.loop, connection_class=NoResetConnection) yield diff --git a/frameworks/Python/aiohttp/app/views.py b/frameworks/Python/aiohttp/app/views.py index c5e76da736f..aa60ad4164d 100644 --- a/frameworks/Python/aiohttp/app/views.py +++ b/frameworks/Python/aiohttp/app/views.py @@ -66,7 +66,7 @@ async def single_database_query_raw(request): id_ = randint(1, 10000) async with request.app['pg'].acquire() as conn: - r = await conn.fetchval('SELECT id,randomnumber FROM world WHERE id = $1', id_) + r = await conn.fetchval(READ_ROW_SQL, id_) return json_response({'id': id_, 'randomNumber': r}) @@ -96,11 +96,10 @@ async def multiple_database_queries_raw(request): result = [] async with request.app['pg'].acquire() as conn: - stmt = await conn.prepare(READ_ROW_SQL) for id_ in ids: result.append({ 'id': id_, - 'randomNumber': await stmt.fetchval(id_), + 'randomNumber': await conn.fetchval(READ_ROW_SQL, id_), }) return json_response(result) @@ -160,10 +159,9 @@ async def updates_raw(request): worlds = [{'id': row_id, 'randomNumber': number} for row_id, number in updates] async with request.app['pg'].acquire() as conn: - stmt = await conn.prepare(READ_ROW_SQL) for id_, _ in updates: # the result of this is the int previous random number which we don't actually use - await stmt.fetchval(id_) + await conn.fetchval(READ_ROW_SQL, id_) await conn.executemany(WRITE_ROW_SQL, updates) return json_response(worlds)