I have a method that kill a character and if this character is married to another character, it also kill them.
public function kill(string $reason): void
{
$this->update(array(
'character_status' => "Dead",
'character_death_reason' => $reason
));
// To ensure it doesn't make an infinite loop.
if($this->isMarried() && $reason != "wedding") {
$husband = $this->getHusband();
$husband->kill('wedding');
}
}
This method is called on a code, 300 lines after the transaction started, but can be resumed precisely like that :
// For context, every Character() belong to a City(), and I'm doing a transaction per City.
$city->beginTransaction();
[...]
foreach($characterAlive as $character) {
$character->kill('because_i_want_to');
}
[...]
$city->commitTransaction();
It does work for every character alive. But if a character have a husband, the transaction goes on a “locked table” issue:
ERROR: SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction.
All my concerned tables are in InnoDB storage config and I’m using a Singleton. As I’m acting on different rows (character_id being my PRIMARY KEY A_I), I don’t understand why it does work on the foreach, but not if a Character class instantiates another Character class.
I need this dependence as there is more than 30 ways to reach the kill method.