4

I've recently implemented django in a tool I'm developing. While doing some tests I have been getting an django.db.utils.InterfaceError: (0, '') error. I have read that it might be a global cursor issue, however I am only doing queries through django, letting it handle the cursors.

Basically I have information about compounds which, I save to mySQL during several moments of the tool execution.

This particular piece of code will execute several times (which seems to work fine so far) and will then execute one final time in the end of the execution ( a global save). It is in this global save that I am getting the aforementioned error. This global save just iterates over all collected compounds and saves them.

Any ideas on what I can do to solve this error?

def save_to_SQL_db(self):
    #####COMPOUND#####
    if not self.sql_key:
        cpd_django=Compound_db(cpd_level=self.get_cpd_level(),\
                               chemical_formula=self.get_Chemical_formula(),\
                               smiles=self.get_SMILES())
    else:
        cpd_django=Compound_db.objects.get(pk=self.sql_key)
        cpd_django.cpd_level=self.get_cpd_level()
        cpd_django.chemical_formula=self.get_Chemical_formula()
        cpd_django.smiles=self.get_SMILES()
    cpd_django.save()

Error:

Traceback (most recent call last): File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute return self.cursor.execute(sql, params) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute return self.cursor.execute(query, args) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\cursors.py", line 170, in execute result = self._query(query) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\cursors.py", line 328, in _query conn.query(q) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\connections.py", line 515, in query self._execute_command(COMMAND.COM_QUERY, sql) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\connections.py", line 745, in _execute_command raise err.InterfaceError("(0, '')") pymysql.err.InterfaceError: (0, '')

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "MyProject/Query.py", line 1900, in File "MyProject/Query.py", line 1803, in multiple_query File "MyProject/Query.py", line 1836, in single_query File "MyProject/Query.py", line 1563, in query_all_databases self.database_saving_all() File "MyProject/Query.py", line 243, in database_saving_all self.database_saving_mets() File "MyProject/Query.py", line 519, in database_saving_mets met.save_to_SQL_db() File "MyProject\Compound.py", line 829, in save_to_SQL_db else: File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 393, in get num = len(clone) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 250, in len self._fetch_all() File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 1186, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\query.py", line 54, in iter results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\sql\compiler.py", line 1065, in execute_sql cursor.execute(sql, params) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 100, in execute return super().execute(sql, params) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 68, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers return executor(sql, params, many, context) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute return self.cursor.execute(sql, params) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\utils.py", line 89, in exit raise dj_exc_value.with_traceback(traceback) from exc_value File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\utils.py", line 85, in _execute return self.cursor.execute(sql, params) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute return self.cursor.execute(query, args) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\cursors.py", line 170, in execute result = self._query(query) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\cursors.py", line 328, in _query conn.query(q) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\connections.py", line 515, in query self._execute_command(COMMAND.COM_QUERY, sql) File "user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymysql\connections.py", line 745, in _execute_command raise err.InterfaceError("(0, '')") django.db.utils.InterfaceError: (0, '')

Pedro Queirós
  • 131
  • 1
  • 12

1 Answers1

0

I know, that this is not the optimal answer, but for me trying to close 3 times the connection, and then reconnecting again worked.

 try:
     connections.close_all()
 except:
     try:
         connections.close_all()
     except:
         try:
             connections.close_all()
         except:
             pass

Also tried to set CONN_MAX_AGE to None in settings.py, but that did not work.

sboda
  • 363
  • 4
  • 10