我使用的是CodeIgniter和tansactions,但有些东西我无法理解。
我设法将问题缩小到交易本身,所以我会发布不同的结果。
情景1:
Mycontroller.php
$call_1 = a_check($data1); // a_check is located in a Helper
$call_2 = a_check($data2);
mycustomhelper.php
function a_check($data){
$ci->db->trans_start();
// If some conditions, run Query 1 below
$ci->db->set('progress', $progress);
$ci->db->where('id_player' , $UserID);
$ci->db->update('user_a');
// If some other conditions, run Query 2 below
$sql = "UPDATE achievements SET count = count+1 WHERE id_a = $a_id;
$ci->db->query($sql);
$ci->db->trans_complete();
}
结果:如果执行
$call_2
期间的查询1,则仅在第二次调用($call_1
)期间执行查询1。
场景2:(删除了交易)
Mycontroller.php
$call_1 = a_check($data1); // a_check is located in a Helper
$call_2 = a_check($data2);
mycustomhelper.php
function a_check($data){
// If some conditions, run Query 1 below
$ci->db->set('progress', $progress);
$ci->db->where('id_player' , $UserID);
$ci->db->update('user_a');
// If some other conditions, run Query 2 below
$sql = "UPDATE achievements SET count = count+1 WHERE id_a = $a_id;
$ci->db->query($sql);
}
结果:查询1将在
$call_1
和$call_2
期间执行(正如预期的那样)。
场景3:(未运行$ call_1)
Mycontroller.php
// $call_1 = a_check($data1); // a_check is located in a Helper
$call_2 = a_check($data2);
mycustomhelper.php
function a_check($data){
$ci->db->trans_start();
// If some conditions, run Query 1 below
$ci->db->set('progress', $progress);
$ci->db->where('id_player' , $UserID);
$ci->db->update('user_a');
// If some other conditions, run Query 2 below
$sql = "UPDATE achievements SET count = count+1 WHERE id_a = $a_id;
$ci->db->query($sql);
$ci->db->trans_complete();
}
结果:查询1将在
$call_2
期间执行。
我使用事务进行错误处理(从此代码示例中删除)但我无法弄清楚我做错了什么。