Monday, 5 July 2021

How to properly kill MySQL/ MariaDB connections in Django using custom connectors

I am currently working on a project and I use the MariaDB connector to run the queries.

I can't use ORM so I have to use raw queries.

In general, the system works fine, as expected, but when I make a bit 'big' queries I get a Too many connections error message.

This has happened to me for both MySQL and MariaDB connectors, but I mainly use MariaDB.

Example of my code (truncated / simplified):

import mariadb

def get_cursor():
    conn = mariadb.connect(
    user="user",
    password="pass",
    host="localhost",
    database="db")
    return conn, conn.cursor(named_tuple=True)

def get_duplicated_variants():
    results = []
    conn_cursor  = get_cursor()
    cursor = conn_cursor[1]
    conn = conn_cursor[0]
    try:
        cursor.execute("SELECT * FROM `db`.s_data;")
        columns = [column[0] for column in cursor.description]
        results = []
        for row in cursor.fetchall():
            results.append(dict(zip(columns, row)))
        cursor.close()
        conn.close()
        return results
    except mariadb.Error as e:
        print(f"Error: {e}")

What I've tried:

show status like '%onn%';

enter image description here

And also: show variables like 'max_connections';

enter image description here

So the max_used_connections = 152 and I have 2503 Connections.

I also tried to execute the following query:

SELECT 
CONCAT('KILL ', id, ';') 
FROM INFORMATION_SCHEMA.PROCESSLIST 
WHERE `User` = 'user' 
AND `Host` = 'localhost'
AND `db` = 'db';

As seen in this question.

But the number of connections is the same after running the query, it does not work.

How could I close the connections properly?

I don't understand why the connections are still active since I use both cursor.close() to close the cursor and conn.close() to close the connection, but the connection is still active apparently.

I know I can increase max_connections with something like: set global max_connections = 500; but I would like to close the connections from the backend after the queries are done.

Any idea?



from How to properly kill MySQL/ MariaDB connections in Django using custom connectors

No comments:

Post a Comment