[Flask] Getting old value from DB

Cameron Simpson cs at cskk.id.au
Sat Jun 22 19:18:59 EDT 2019


On 22Jun2019 08:48, shanmugavel Subramani <srshanmugavel at gmail.com> wrote:
>I have below DB setup in my code. There are few APIs in my code and few
>controllers which will serve templates and corresponding form POST
>handlers. The problem is when i call /details?stu_id=abcd the handler
>decorator gets invoked and I have a db query to check if the given stu_id
>is present in my table(Student). I am randomly getting the passed id is not
>exists in my table error. But when i checked manually its present. after
>few refreshes its working fine. I dont get this problem in the APIs
>
>Sometimes it happens in the later stages of the pages. I update status in
>each page movement. When i read status from the same table using above
>StuHelper methods it gives me the old value but when i checked in db
>manually it has the correct value. after a refresh, it returns me some
>other value which i updated in sequence and after few refreshes, it gives
>me the right value from db.
[...]
>I should be getting stu_id from the table when I query from ORM and 
>correct
>status value in subsequent pages query. I feel like flask maintains a stack
>which gives me random behavior here. Can someone help me solving this
>problem?

Is it possible that the new stu_id row is created by a transaction which 
has not been committed? I don't see any explicit transaction stuff in 
the code you quoted, but since flask is multithreaded it is possible for 
the creating transaction to be still open (uncommitted) when your query 
is made. Depending on what is going on.

Have you tried having your stu_id creation code use an explicit 
transaction:

  with session->begin():
    ... insert student data

so that the transaction is definitiely committed when you leave the 
"with" above? Otherwise you may be operating inside a longer lived 
transaction.

I'm just guessing here as its not clear from the code, but (a) 
transactions do conceal their sid effects until closed/committed and (b) 
transactions are often implicitly used if you don't manage them yourself 
and (c) they will produce effects like this as a consequence.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Flask mailing list