Should i be using transactions?

#1

I’ve been reading about php sessions and someone said that sessions can lock if a race condition is created, like a double form submission. I don’t know what the guy is babbling about but i do wonder if i should be using transactions when i am interacting with a database. For example, my login script. I’ve changed the pdo to work within a transaction. The code works and i am able to log in but is this correct for this situation? i know that we should use a transaction to handle any corruption or error so that we can implement a rollback. I am just wondering if there could be a problem with sessions that could be solved by using a transaction for the db?

#2

Transaction are used with a purpose, and I don’t think what you are doing qualifies.

So, an example would be a banking transaction. I send you money.

try
    start transaction
    remove money from account A
    add money to account B
    commit transaction
catch
   rollback transaction

The purpose it to revert changes if something goes wrong. This is a very simplified example. So, with what I do, Machines complete work and update multiple systems. If out of 9 system updates, #8 fails, that means all the records need to be reverted. Otherwise, data is corrupt. (And yes there are ways to do transactions across systems)

1 Like
#3

Thank you @astonecipher, i have a better understanding of the matter. Very nice example too.

I removed the transaction code and i also followed the advice of phdr and removed the try catch block from my login. I think that now, with your guidance and the guidance of Jim and phdr, that my login script is becoming better.

Thank you :slight_smile: