开发者

How can I test against a query executing without errors within a transaction?

开发者 https://www.devze.com 2023-02-09 02:25 出处:网络
I\'m running two queries within a ColdFusion transaction (ColdFusion 开发者_如何转开发9, MySQL 5):

I'm running two queries within a ColdFusion transaction (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;
    }
}

I would like to know the following: What should I replace both updates where successful in the if() with to test if the queries executed correctly?


This solution swallows the errors, and leaves it to the calling function to examine and rethrow them. As such, I changed your return value on you. It's a struct with a boolean and an error array.

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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号