Chronic stale results using MySQLdb in Python

前端 未结 3 545

My Python program queries a set of tables in a MySQL DB, sleeps for 30 seconds, then queries them again, etc. The tables in question are continuously updated by a third-par

3条回答
  •  有刺的猬
    2020-12-24 07:48

    You may want to check the transaction isolation level of your database. The behavior you describe is what you may expect if it is set to REPEATABLE-READ. You may want to change it to READ-COMMITTED.

    Since the original poster of the problem mentions that he is merely querying the database, it cannot be a commit that was forgotten. Inserting a commit seems to be a workaround though since it causes a new transaction to begin; and a new snapshot may need to be established. Still, having to insert a commit before every select doesn't sound like good programming practices to me.

    There is no python code to show since the solution lies in correctly configuring the database.

    DO check MySQL documentation at http://dev.mysql.com/doc/refman/5.5/en/set-transaction.html.

    REPEATABLE READ
    This is the default isolation level for InnoDB. For consistent reads, there is an important difference from the READ COMMITTED isolation level: All consistent reads within the same transaction read the snapshot established by the first read. This convention means that if you issue several plain (nonlocking) SELECT statements within the same transaction, these SELECT statements are consistent also with respect to each other. See Section 14.3.9.2, “Consistent Nonlocking Reads”.

    READ COMMITTED
    A somewhat Oracle-like isolation level with respect to consistent (nonlocking) reads: Each consistent read, even within the same transaction, sets and reads its own fresh snapshot. See Section 14.3.9.2, “Consistent Nonlocking Reads”.

    Checking the configured isolation level:

    >mysql > SELECT @@GLOBAL.tx_isolation, @@tx_isolation;
    +-----------------------+-----------------+
    | @@GLOBAL.tx_isolation | @@tx_isolation  |
    +-----------------------+-----------------+
    | REPEATABLE-READ       | REPEATABLE-READ |
    +-----------------------+-----------------+
    1 row in set (0.01 sec)
    

    Setting the transaction isolation level to READ-COMMITTED

    mysql> SET GLOBAL tx_isolation='READ-COMMITTED';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> SET SESSION tx_isolation='READ-COMMITTED';
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> SELECT @@GLOBAL.tx_isolation, @@tx_isolation;
    +-----------------------+----------------+
    | @@GLOBAL.tx_isolation | @@tx_isolation |
    +-----------------------+----------------+
    | READ-COMMITTED        | READ-COMMITTED |
    +-----------------------+----------------+
    1 row in set (0.01 sec)
    
    mysql>
    

    And run the application again …

提交回复
热议问题