Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single MySQL session per python\django process

If at the start of a python\django program I load a module that executes a raw custom sql command such as

from django.db import connection
cursor = connection.cursor()
cursor.execute("SET SESSION wait_timeout=2147483")

thus changing a session variable, will it hold for all the rest of the program run? i.e. is the module in the same MySQL session as the rest of the code running in the same python\django process?

like image 415
Jonathan Livni Avatar asked Feb 22 '23 08:02

Jonathan Livni


1 Answers

Not reliably.

The correct way to do this is to use init_command within the OPTIONS dictionary of the db settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        ...
        'OPTIONS': {
            'init_command': '"SET SESSION wait_timeout=2147483',
        },
    }
}
like image 177
Daniel Roseman Avatar answered Mar 03 '23 19:03

Daniel Roseman