title: Django on MySQL: how to enable autocommit?
body: <p>I have something like the following code that runs in a background process:</p>
  
  <pre><code>def run()
      while True:
          objs = MyModel.objects.filter(last_updated &lt; time.time() - FREQUENCY)
          print objs
  
  def update()
      while True:
         # some processing code
         mymodel.last_updated = time.time()
         mymodel.save()
  </code></pre>
  
  <p>The above functions run in two separate threads: update() updates all the models in turn, while run() selects the models that need updating. This all runs against MySQL and MyModel lives in an InnoDB table.</p>
  
  <p>The problem is that run() always sees the same value for last_updated. The reason is that it is inside of a transaction and selects a consistent snapshot of the data. Naturally, I want it to select the latest data instead. It works if I do the following:</p>
  
  <pre><code>def run()
      from django.db import connection
      while True:
          connection.connection.execute('SET autocommit = 1')
          objs = MyModel.objects.filter(last_updated &lt; time.time() - FREQUENCY)
          print objs
  </code></pre>
  
  <p>But that means that I'll be executing an additional query every single time. On top of that, if the connection gets closes between where I set autocommit = 1 and the following select, it will not work.</p>
  
  <p>Postgres happily supports this: <a href="http://docs.djangoproject.com/en/dev/ref/databases/#autocommit-mode" rel="nofollow">http://docs.djangoproject.com/en/dev/ref/databases/#autocommit-mode</a> (at least according to the docs), but is there a way to enable autocommit for MySQL?</p>
  
  <p>Also, since this is running as a background process, no requests are processed, and no middleware is involved.</p>