Failed inserts could cause "Commands out of sync" error, make sure we close the cursor and commit before relasing the lock