Karan Verma
Karan Verma

Reputation: 61

Getting intermittent InterfaceError(0, '') in Django3.2 with MySQL5.7

I am using an EC2 server to run a listener cron in python using Django 3.2 and connecting to MySQL 5.7 using a global connection. I am facing intermittent InterfaceError(0, '') in the logs and this is creating data loss for me.

Setting.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': db_conf.DB_CONF[app_env.APP_ENV]["rds"]["db_name"],
        'USER': DB["username"],
        'PASSWORD': DB["password"],
        'HOST': DB["host"],
        'PORT': '3306',
        'CONN_MAX_AGE': 1, # in seconds (5 minutes)
        'CONN_HEALTH_CHECKS': True,
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        }
    },
    'write_db': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': db_conf.DB_CONF[app_env.APP_ENV]["write_rds"]["db_name"],
        'USER': DB["write_rds_username"],
        'PASSWORD': DB["write_rds_password"],
        'HOST': DB["write_rds_host"],
        'PORT': db_conf.DB_CONF[app_env.APP_ENV]["write_rds"]["port"],
        'CONN_MAX_AGE': 1, # in seconds (5 minutes)
        'CONN_HEALTH_CHECKS': True,
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
        }
    }
}

My db function looks like below:

    @staticmethod
    def increment_count(user_id, domain, log_dict={}):
        try:
            with transaction.atomic():
                my_model.MyTable.objects.filter(user_id=user_id, domain=domain).update(count=F('count') + 1)
            return True
        except Exception as e:
            log_dict["message"] = f"Failed to increment Count for user {user_id}, domain: {domain}"
            log_dict["exception"] = e
            logger.error(log_dict)
            return False

Upvotes: 0

Views: 20

Answers (0)

Related Questions