How can I test against a query executing without errors within a transaction?
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;
精彩评论