【发布时间】:2014-05-19 08:48:32
【问题描述】:
我们都使用DB::transaction() 进行多个插入查询。这样做时,应该将try...catch 放在里面还是包裹起来?如果出现问题,事务会自动失败,是否还需要包含try...catch?
try...catch 包装事务的示例:
// try...catch
try {
// Transaction
$exception = DB::transaction(function() {
// Do your SQL here
});
if(is_null($exception)) {
return true;
} else {
throw new Exception;
}
}
catch(Exception $e) {
return false;
}
相反,DB::transaction() 包装了一个 try...catch:
// Transaction
$exception = DB::transaction(function() {
// try...catch
try {
// Do your SQL here
}
catch(Exception $e) {
return $e;
}
});
return is_null($exception) ? true : false;
或者只是一个没有 try...catch 的事务
// Transaction only
$exception = DB::transaction(function() {
// Do your SQL here
});
return is_null($exception) ? true : false;
【问题讨论】:
标签: php laravel transactions