如何针对在事务中无错误执行的查询进行测试?

时间:2011-02-08 17:55:34

标签: coldfusion transactions coldfusion-9

我在ColdFusion事务中运行了两个查询(ColdFusion 9,MySQL 5):

private boolean function updates()
{
    transaction
    {
        local.foo = new Query();
        local.foo.setSql("UPDATE foo SET bar = foo");
        local.foo.execute();

        local.bar = new Query();
        local.bar.setSql("UPDATE bar SET foo = bar");
        local.bar.execute();
    }

    if (both updates were successful)
    {
        return true;
    }
    else
    {
        return false;
    }
}

我想知道以下内容:我应该在both updates where successful中替换if()以测试查询是否正确执行?

1 个答案:

答案 0 :(得分:4)

此解决方案吞下错误,并将其留给调用函数来检查并重新抛出它们。因此,我改变了你的回报价值。它是一个带有布尔值和错误数组的结构。

local.result.bool = false;
local.result.error = ArrayNew(1);
TRANSACTION action="begin" 
{
 try{
    local.foo = new Query();
    local.foo.setSql("UPDATE foo SET bar = foo");
    local.foo.execute();
    local.bar = new Query();
    local.bar.setSql("UPDATE bar SET foo = bar");
    local.bar.execute();
 }
 catch(any excpt){
    ArrayAppend(local.result.error, excpt);
 }finally{
   if(ArrayLen(local.result.error) gt 0){
      transactionRollback();
    }else{
      local.result.bool = true; 
    }
 } 
}
return local.result;