【问题标题】:laravel nested db transaction doesnt commitlaravel 嵌套数据库事务未提交
【发布时间】:2020-03-29 03:37:26
【问题描述】:

我有一个嵌套事务,用于提交几个不同的表。我只想在嵌套中提交一个表,在外部提交中提交另外两个表。

beginTransaction();

error_message.=function A();
error_message.=function B();

if (empty error_message){
    commit(); //commit table B and C
} else {
    rollback();
}
function A(){
    beginTransaction();
    update table A //wants to commit table A immediately
    commit();
    //do some stuff
    update table B
    if(error) {
        return error_message
    } else {
        return null
    }
}

function B(){
    beginTransaction();
    update table A //wants to commit table A immediately
    commit();
    //do some stuff
    update table C
    if(error) {
        return error_message
    } else {
        return null
    }
}

【问题讨论】:

    标签: mysql database laravel transactions database-connection


    【解决方案1】:

    当你调用 beginTransaction();在函数之前,会有两个beginTransaction();,第一个在函数外,第二个在函数内。

    beginTransaction();
    error_message.=function A();
    error_message.=function B();
    

    把你的 beginTransaction();在函数调用下方。

    error =0;
    
    error_message = function A();
    if(empty(error_message)){
       error =1;
    }
    
    error_message = function B();
    if(empty(error_message)){
       error =1;
    }
    
    beginTransaction();
    if (error){
        commit(); //commit table B and C
    } else {
        rollback();
    }
    

    【讨论】:

    • 这仍然会更新表 B 和 C 而不检查 error_message 是否为空
    • 你确定你检查你的错误这种类型。 if(empty(error_message))
    • 是的。每个函数都会更新表并返回error_message,无论它是否为空
    • 这是我的问题。我不希望我的表格在每个函数中都立即更新。我想等到没有错误才更新
    • 我有 3 个表要更新,只有一个表要立即更新,而另外 2 个表要等到没有错误才更新
    【解决方案2】:

    我认为你在写函数 a() 或 b() 时错过了 rollback()。

    rollback()
    

    当您跳过 3 个中的 1 个方法时,dbtransaction 未运行

    1 beginTransaction()//start transection
    2 commit()//commit your transaction
    3 rollback()//rollback your transaction 
    

    没有回滚 beginTransaction 不起作用。

    我认为你在空后想念 ()

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      相关资源
      最近更新 更多