I want to change my python program from a normal connection to a Connection Pool so that the database connection doesn't get lost when no queries are sent for some hours, as well as the database not being overwhelmed with a bunch of queries at once at peak usage.
I'm using Python 3.7.4, mysql-connector-python 8.0.17, and MariaDB 10.4.7.
It works fine when I use a normal connection, but MariaDB apparently doesn't support the pool_reset_session
setting of mysql.connector.pooling.MySQLConnectionPool
At the start of my code, it tries to create the database if it doesn't yet exist, and that is causing the errors I get.
import mysql.connector as mariadb
from mysql.connector import errorcode
from mysql.connector import pooling
cnx = mariadb.pooling.MySQLConnectionPool(user='root', password='password', host='localhost',
pool_name='connectionpool', pool_size=10, pool_reset_session=True)
try:
db = cnx.get_connection()
cursor = db.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS tests")
print("Created database")
except mariadb.Error as err:
print(f"Failed creating database: {err}")
finally:
print("Finally (create)")
db.close()
I expected that this snippet would just create the database tests
but instead I got the following two errors:
mysql.connector.errors.NotSupportedError: MySQL version 5.7.2 and earlier does not support COM_RESET_CONNECTION.
as well as
mysql.connector.errors.OperationalError: 1047 (08S01): Unknown command
From the traceback logs, it looks like this gets caused by trying to execute db.close()
in line 17.
Full output with traceback: https://pastebin.com/H3SAvA9N
I am asking what I can do to fix this and if it's possible at all to use this sort of connection pooling with MariaDB 10.4.7 (I get confused because it says that MySQL <= 5.7.2 doesn't support this reset of connections after use even though I use MariaDB 10.4.7)
I also found out that MariaDB Connector/J does provide such an option, called useResetConnection
but I don't want to learn Java just for this feature.
I was facing the same issue with mysql-connector-python for mariaDB, and I downgraded mysql-connector-python version to 8.0.12 and it worked for me