# Lost connection to Mysql during query

**URL:** <https://forum.djangoproject.com/t/lost-connection-to-mysql-during-query/26445>\
**Category:** Using the ORM\
**Created:** [December 27, 2023, 8:02am UTC](https://forum.djangoproject.com/t/lost-connection-to-mysql-during-query/26445 "2023-12-27T08:02:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kessero](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kessero/32/10089_2.png) [@kessero](https://forum.djangoproject.com/u/kessero)\
**Post date:** [December 27, 2023, 8:02am UTC](https://forum.djangoproject.com/t/lost-connection-to-mysql-during-query/26445/1 "2023-12-27T08:02:11Z")

</div>

My script is running as service

> #!/usr/bin/env python3  
> import os  
> import django
> 
> os.environ.setdefault(‘DJANGO\_SETTINGS\_MODULE’, ‘LPS.settings’)  
> django.setup()  
> from abonament.models import Abonament\_info  
> from datetime import datetime, timedelta  
> import schedule  
> import time  
> from django.utils import timezone  
> def change\_day():  
> now = timezone.now()  
> abo\_to\_change = Abonament\_info.objects.filter(user\_id\_\_notatka\_\_startswith=“@”,  
> end\_\_gt=now,  
> begin\_\_lt=now)  
> for item in abo\_to\_change:  
> new\_date = now.replace(hour=item.begin.hour, minute=item.begin.minute, second=0)  
> item.begin = new\_date  
> item.save()
> 
> #change\_day()
> 
> schedule.every().day.at(“08:01”).do(change\_day)
> 
> while 1:  
> schedule.run\_pending()  
> time.sleep(1)

One day after running hi works ok next day I found in logs

> Mysqldb.OperationalError: (2013, ‘Lost connection to MySQL sever durning query’)

Manual activation works ok.  
There are 4-5 rows in the table to change the day and there are 120 rows, so there is not that much data to manage.  
Maybe there is some command in ORM to keep the connection in this particular situation?  
I found some CONN\_MAX\_AGE setting, but is this the right way? Won’t this lock up the database for other operations? This is a setting for the entire project.  
I don’t have this problem in other parts of the project, e.g. with forms on the website

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 27, 2023, 1:17pm UTC](https://forum.djangoproject.com/t/lost-connection-to-mysql-during-query/26445/2 "2023-12-27T13:17:28Z")

</div>

Set this up as a cron job to run once/day rather than keeping this process persistent as a service. That’s going to be easier than just about any other method to keep the connection active.

Trying to keep the connection active also isn’t going to help if there’s any _other_ cause for the connection being lost, such as the database engine being restarted.

This means that you’d want to establish a new connection every day anyway, which then means you’re just as well off running a new instance of the process each day.

---

<div class="post-metadata">

**Author:** ![kessero](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kessero/32/10089_2.png) [@kessero](https://forum.djangoproject.com/u/kessero)\
**Post date:** [December 27, 2023, 3:17pm UTC](https://forum.djangoproject.com/t/lost-connection-to-mysql-during-query/26445/3 "2023-12-27T15:17:56Z")

</div>

I’don’t like cron 🙂 If I run this as a service it’s easier to manage in my opinion.  
If I want to stick with my solution, should I assume that it is a matter of time performing the operation? I don’t see any disturbing issues related to mySQL in the logs. For example, will replacing the ORM with a plugin and SQL be a solution or is it better to check the execution and retry the task?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 27, 2023, 3:21pm UTC](https://forum.djangoproject.com/t/lost-connection-to-mysql-during-query/26445/4 "2023-12-27T15:21:42Z")

</div>

This really isn’t an issue of “like” or “dislike”. It’s a question of what’s the most appropriate tool for a task.

Yes, you can build your task manager, and you can create your service to do exactly what cron is doing.

Or you can use cron, which is well-tested and known to be a reliable mechanism for initiating tasks on a periodic basis.

Or, if you’re already using Celery & Beat, you could configure this as a Celery task.

---

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [December 27, 2023, 5:27pm UTC](https://forum.djangoproject.com/t/lost-connection-to-mysql-during-query/26445/5 "2023-12-27T17:27:25Z")

</div>

@kessero

The only part missing here is that you should call `django.db.close_old_connections` first thing first in `change_day`.

The alternative would be to explicitly close the connection used in the function at the end of it.

There’s a long standing issue about documenting `close_old_connections` and [when it should be called in long running script/management commands](https://code.djangoproject.com/ticket/14845).

---

<div class="post-metadata">

**Author:** ![kessero](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kessero/32/10089_2.png) [@kessero](https://forum.djangoproject.com/u/kessero)\
**Post date:** [December 28, 2023, 8:51am UTC](https://forum.djangoproject.com/t/lost-connection-to-mysql-during-query/26445/6 "2023-12-28T08:51:59Z")

</div>

I’am not familiar with mysql on this level.  
It looks like this is the problem. Yesterday script crash i’don’t do anything and today at 8:01 it executed OK. So tomorrow should crash again.  
close\_old\_connections will close all connections (django website, scripts), all connections this specific user or only connection from that script?

---

<div class="post-metadata">

**Author:** ![charettes](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/charettes/32/27_2.png) [@charettes](https://forum.djangoproject.com/u/charettes)\
**Post date:** [December 28, 2023, 3:52pm UTC](https://forum.djangoproject.com/t/lost-connection-to-mysql-during-query/26445/7 "2023-12-28T15:52:23Z")

</div>

only connection from that script
