Suppose we have following pseudo python code to deal with Mysql:
with connection.cursor() as cursor:
sql = "SELECT `count` FROM `sometable` WHERE `name`=%s"
cursor.execute(sql, ('somename',))
result = cursor.fetchone()
if result['count'] == 1:
sql = "UPDATE `sometable` SET `count`=%d WHERE `name`=%s"
cursor.execute(sql, (10, 'somename'))
connection.commit()
I know transaction will provide an "all-or-nothing" proposition, but don't know how it works if mixed with application code. How can database transaction deal with appliction code like: if result['count'] == 1
.
In my understanding, when committed, all sqls will be handled to Database Server to execute, and the server will make sure sqls execute "all-or-nothing". But the code above should only execute the UPDATE
sql when count
equals 1
. What would happen if all the sqls have already handled to the database server and some other transaction just change the count
?