i'm running a SELECT query in a loop.
Once in a while, the database table is updated (by another program).
The first SELECT retrieves the correct data, but further calls in the loop return the first values.
How can I retrieve up-to-date data?
The only workaround I found so far, is reconnect to the DB on each iteration! In my example, uncommenting BOTH comment #1# and #2#. Uncommenting only #2# is not enough (i.e., cursor is recreated), the results are still cached.
Here's a working sample that gives the error.
import MySQLdb
from time import sleep
class DB:
def __init__(self):
self.connection = MySQLdb.connect(mysql_host, mysql_user, mysql_pass, mysql_db)
self.cursor = self.connection.cursor()
def get(self):
sql = ''' SELECT id, message FROM mps_messages
WHERE topic=%s ORDER BY id LIMIT 1 '''
#1# self.connect()
#2# self.cursor = self.connection.cursor()
self.cursor.execute(sql, ("topic",) )
rec = self.cursor.fetchone()
print rec
def loop(self):
while True:
self.get()
sleep(4)
db=DB()
db.loop()
Hibernate also implements a cache for query resultsets that integrates closely with the second-level cache. This is an optional feature and requires two additional physical cache regions that hold the cached query results and the timestamps when a table was last updated.
Query cache is a prominent MySQL feature that speeds up data retrieval from a database. It achieves this by storing MySQL SELECT statements together with the retrieved record set in memory, then if a client requests identical queries it can serve the data faster without executing commands again from the database.
Query to disable the cache From the MySQL command line, a application like phpMyAdmin, or from a script, run the following SQL command to disable the query cache: SET GLOBAL query_cache_size = 0; This will take effect immediately. You will also need to modify the MySQL configuration file to keep it disabled on reboot.
i had to add
connection.autocommit(True)
Adding SQL_NO_CACHE had no effect on the presented case, apparently because there was no caching involved.
I still don't understand why a SELECT needs COMMIT.
I'll open a new question about it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With