Search code examples
pythonmysql

Python MySQLdb won't release table lock


I have a program that reads a file and inserts data into a Database. I am writing a test harness for that program in python. The python harness connects to the database to get a few configuration parameters before generating the test data. It then wait 10 seconds to allow the program being tested to process the file and modify the database. After the delay if I try to read the values from the database they are not present. If I open up a second terminal and check via the command line they aren't there. As soon as I terminate my Python test harness the data is inserted into the database. It would appear that the MySQLdb is locking the tables are not allowing me to insert data. How do I make MySQLdb release its lock on the tables

My workflow looks like this:

connection = MySQLdb.connect(host="localhost")
cursor = connection.cursor()
cursor.execute(".....")
cursor.close()
connection.commit()
connection.close()
cursor = None
connection = None

sleep(10)

repeat the above to check for the data my other program should have inserted


Solution

  • You need to do connection.commit() before cursor.close().

    Note that MySQL does not actually support cursors-- the Python wrapper emulates them. This can cause your code to not work in unexpected ways, like in this example.