if I use transactions, will it lock the tables and prevent it from making any changes by other users?
pseudo code:
begin transaction
issue a select query
issue a update query
end transaction
so, in between those two queries, is it possible to make some changes by another update statement or something which was issued from another page ?
or since the beginning of the transaction, will the tables used be locked ?
What is the difference between transaction and lock table? will a transaction implicitly lock the table ?
Edit:
This is what i want to do:
{
// Check Table2 (posted messages)
// If is_approved is FALSE for a given msg_id
{
// Set is_approved to TRUE
// Update Table1 (member details) post_count_month++
// and post_count_lifetime++
}
// Else
{
// NOOP
}
}
Above updation can be made by several users at the same time. Also, a single user(Admin) could delete a message(both accepted and not accepted message). So for deleting, the countmonth and countlifetime of the user who had posted that message(the one to be deleted) should be incremented and then message is deleted.
This is the two situation I am facing.
Transactions and table locking are different features that solve different problems. In your case you only seem to perform one write operation into the database, thus using a transaction will not provide any benefit unless you want to able to undo the update.
Have a look at the SELECT … FOR UPDATE statement.
Update #1:
Alright, here you are some code:
(I’m not sure about how helpful it’ll be, considering that I’ve invented all the queries. Understanding the code is always better than copying and pasting.)
Update #2:
I’ve edited the sample code to surround the queries in a transaction. I said that transactions and table locking are different features and it’s still true. However, the
SELECT ... FOR UPDATEworks inside a transaction. It’s just an implementation detail.