Twisted adbapi: runInteraction last_insert_id ()

class MySQL(object): def __init__(self): self.dbpool = adbapi.ConnectionPool( 'MySQLdb', db='dummy', user='root', passwd='', host = 'localhost', cp_reconnect = True, cursorclass=MySQLdb.cursors.DictCursor, charset='utf8', use_unicode=True ) def process(self, item): query = self.dbpool.runInteraction(self.conditionalInsert, item).addErrback(self.handle_error) return item def conditionalInsert(self, tx, item): tx.execute("INSERT INTO User (user_name) VALUES (%s)",(name)) tx.execute("SELECT LAST_INSERT_ID()") lastID = getID(tx.fetchone()) # DO SOMETHING USING lasID ... ... def handle_error(self, e): log.err(e) 

The last identifier of the second row below corresponds to the insertion in the first row? or can it be from any of the runInteraction threads?

  tx.execute("INSERT INTO User (user_name) VALUES (%s)",(name)) tx.execute("SELECT LAST_INSERT_ID()") 
+4
source share
1 answer

The last identifier will be the last inserted row identifier in the same transaction.

I have a test that uses the following operations:

  • start a transaction and insert a row using the runInteraction (...) function

  • get the last insert id, for example. it's 18

  • sleep 30 seconds in the function in which the transaction is executed

  • Insert a row into the same table using mysql client or phpMyAdmin

  • get the last insert id from step 4, for example. it's 19

  • the return function returns and requests the last insert identifier using the same Transaction object, the last insert identifier is another 18

+1
source

Source: https://habr.com/ru/post/1440520/


All Articles