0
我尝试了一些新的东西,每当我有多个查询插入/更新时,我的一端。我为此使用数据库事务。多个查询的数据库事务
$this->transaction->beginTransaction();
try {
$trucker_user->update([
'username' => $request->getParam('username'),
'first_name' => $request->getParam('first_name'),
'middle_name' => $request->getParam('middle_name'),
'last_name' => $request->getParam('last_name'),
'contact_number' => $request->getParam('contact_number'),
'email' => $request->getParam('email'),
'status' => ($trucker_id) ? 1 : $request->getParam('status')
]);
if ($trucker_id === false) {
$trucker_user->userTrucker()->update([
'trucker_id' => $request->getParam('trucker_id')
]);
}
$this->transaction->commit();
} catch(\Exception $e) {
$this->transaction->rollBack();
throw $e;
}
因此问题是,是不是当我有一个插入/更新我应该使用数据库事务为它的多个查询一个好的做法呢?
我知道有一个事务是一个好主意,当事情发生不良时它会回滚。但是,每当我使用它总是在多个查询是不是有点矫枉过正?