I have a method deleteUser(..) that executes a mysql query and calls another method logAction(..) that calls another mysql query.
What is the best way to handle transactions inside such methods?
Example pseudo code:
function deleteUser($userId) {
$db->execute('BEGIN TRANSACTION');
$userCount = $db->execute("DELETE FROM users WHERE id=$userId");
logAction('USER DELETED');
$db->execute('INSERT INTO ... another action');
$db->execute('COMMIT');
}
function logAction($action) {
$db->execute('BEGIN TRANSACTION');
$db->execute('INSERT INTO logtable ... ACTION');
if (error) {
$db->execute('ROLLBACK');
}
$db->execute('COMMIT');
}
As you can see, I'm calling logAction from deleteUser() method, and encountered 'BEGIN TRANSACTION' twice. Method logAction() calls 'COMMIT' prematurely before deleteUser() is done.
Is there a way to manage transactions inside such nested methods?