So I have two methods. Method one adds a row to my database. Method two selects and updates the latest 10 rows of the table.
If method one adds a new row after method two selects the data but before it updates it will method two update the wrong 10 rows? Will transactions help here or do i need to lock tables?
function one(){
insert row
}
function two(){
select latest 10 rows
update latest 10 rows
}
// EDIT /////////////////////////////////////////////////////////////////////////////
I was reluctant to throw in my actual methods (they are codeIgniter methods) but here they are
function one(){
insert row
}
function two(){
$this->db->where('status', '1');
$this->db->limit(10);
$query = $this->db->get('rrsf_users');
$this->db->where('status', '1');
$this->db->limit(10);
$this->db->update('rrsf_users', array('status' => '2'));
}
So it looks like two separate queries hence the “current last 10 rows” problem. Not sure the best fix for this. Lock tables or cycle though each index selected from the query and add this to the where query?
It is dependend on how you select those 10 rows. If you have a unique index on them and you know which 10 rows to update (and what you update is exactly those 10 rows previously selected) then you do not have a problem. Of course if you write the update statement in a way that you update also the just inserted row you have that problem. Locking as a matter of fact does not help you much here because you only can lock what is already there.
In short: If you know what you update you have no problem. If you update the “current last 10 rows” you have a problem.