aio-libs / aiomysql

aiomysql is a library for accessing a MySQL database from the asyncio
https://aiomysql.rtfd.io
MIT License
1.75k stars 255 forks source link

Pool does not reset connections after they're returned to the pool #462

Open cole-dda opened 4 years ago

cole-dda commented 4 years ago
async with aiomysql.create_pool(autocommit=False) as pool:
           async with pool.connect() as conn:
                       conn.autocommit(True) 

When next resue connection,the connection maybe autocommit=True,but pool settings is autocommit=False。

fix:

def release(conn):
       conn.autocommit= init_autocommit
MateuszCzubak commented 4 years ago

I'm not really sure if this is a bug. If you manipulate with the connection state, shouldn't it be your responsibility to change the state back the original one?

Nothing4You commented 2 years ago

As mentioned in #679, this could be dealt with by resetting the connection when it's returned to the pool.

MySQL supports this since version 5.7.3, which means all currently supported versions include this.

https://dev.mysql.com/doc/internals/en/com-reset-connection.html

MariaDB supports this since 10.2.4: https://mariadb.com/kb/en/com_reset_connection/, which means all currently supported versions include this.

Note that at least MariaDB explicitly notes that the Database will NOT be reset to initial value. MySQL does not explicitly mention resetting the DB, so it probably doesn't reset it either: https://dev.mysql.com/doc/c-api/8.0/en/mysql-reset-connection.html

It looks like aiopg currently deals with this by checking transaction state and closing connections when they're returned with active transactions: https://github.com/aio-libs/aiopg/blob/7f40980694085bb176d9ef2a5529f026941183db/aiopg/pool.py#L390-L398

Nothing4You commented 2 years ago

see also