public function createSession($user) { if (empty($user)) { return; } try { Db::begin(); $user->last_login = Db::now(); $user->store(); $session = Orm::collection('Session')->load(); $session->user = $user; $session->token = App::hash(uniqid(rand(), true)); $session->store(); Db::commit(); } catch (Exception $e) { Db::rollback(); throw $e; } return $session->token; }
/** * 回滚事务 * * @param string $host */ public static function rollback($host = 'master') { Db::rollback($host); }
print "Test '{$test_name}' (FALLÓ) con mensaje: ({$e->getMessage()})"; print "</div>"; } if ($test) { $end_benckmark = microtime(true) - $start_benchmark; print "<div style='background:#CCFF99;border:1px solid green'>"; print "Test '{$test_name}' (OK) con tiempo: ({$end_benckmark})"; print "</div>"; } $test = true; $test_name = "INSTRUCCIONES TRANSACCIONALES"; $start_benchmark = microtime(true); try { $db->begin(); $db->query("delete from kumbia_test"); $db->rollback(); $db->begin(); $db->query("delete from kumbia_test"); $db->commit(); } catch (Exception $e) { print "<div style='background:#FFBBBB;border:1px solid red'>"; print "Test '{$test_name}' (FALLÓ) con mensaje: ({$e->getMessage()})"; print "</div>"; } if ($test) { $end_benckmark = microtime(true) - $start_benchmark; print "<div style='background:#CCFF99;border:1px solid green'>"; print "Test '{$test_name}' (OK) con tiempo: ({$end_benckmark})"; print "</div>"; } $test = true;
function undo_checkout(){ ajx_current("empty"); $file = ProjectFiles::findById(get_id()); if(!$file instanceof ProjectFile) { flash_error(lang('file dnx')); return; } // if if(!$file->canCheckin(logged_user())) { flash_error(lang('no access permissions')); return; } // if try { $file->cancelCheckOut(); flash_success(lang("success undo checkout file")); if (array_var($_GET, 'back', false)) { ajx_current("back"); } else { ajx_current("reload"); } } catch (Exception $e){ Db::rollback(); flash_error($e->getMessage()); } }
/** * Refresh single user data * @param int $userId * @param bool $updateSummary * @return bool * @throws RateLimitException */ protected function updateSingleUser($userId, $updateSummary = true) { $companyId = false; try { if (!$userId || !ctype_digit((string) $userId)) { throw new Exception('Invalid user ID.'); } // fetch from TLMS $user = TalentLMS_User::retrieve($userId); $courseData = $certData = array(); foreach ($user['courses'] as $oneCourse) { // skip non-learners and courses that we are not tracking if ($oneCourse['role'] != 'learner' || !isset($this->courses[$oneCourse['id']])) { continue; } $courseStatus = TalentLMS_Course::getUserStatus(array('user_id' => $userId, 'course_id' => $oneCourse['id'])); $exams = array('taken' => 0, 'passed' => 0); if (isset($courseStatus['units'][0])) { foreach ($courseStatus['units'] as $oneUnit) { // skip non-test and not attempted tests if ($oneUnit['type'] != 'Test' || $oneUnit['completion_status'] == 'Not attempted') { continue; } ++$exams['taken']; if ($oneUnit['completion_status'] == 'Completed') { ++$exams['passed']; } } } $courseData[$oneCourse['id']] = $exams; } foreach ($this->courses as $oneCourse => $oneName) { $certData[$oneCourse] = array('certified' => 0, 'issued' => Db::NULL, 'expires' => Db::NULL); } foreach ($user['certifications'] as $oneCert) { // skip certs we are not tracking if (!isset($certData[$oneCert['course_id']])) { continue; } $issued = DateTime::createFromFormat('Y/m/d', $oneCert['issued_date']); if ($issued) { $issued = $issued->format('Y-m-d'); } else { $issued = Db::NULL; } $expires = DateTime::createFromFormat('Y/m/d', $oneCert['expiration_date']); if ($expires) { $expires = $expires->format('Y-m-d'); } else { $expires = Db::NULL; } $certData[$oneCert['course_id']] = array('certified' => 1, 'issued' => $issued, 'expires' => $expires); // if no exams data, create empty if (!isset($courseData[$oneCert['course_id']])) { $courseData[$oneCert['course_id']] = array('taken' => 0, 'passed' => 0); } } // save to DB $numRetry = 0; while ($numRetry <= 10) { try { $this->db->startTransaction(); $companyId = $this->getCompanyId($user['custom_field_1']); $rez = $this->runSql(' insert into tlms_user (id, companyId, firstName, lastName, fullName) values (::p0::, ::p1::, ::p2::, ::p3::, ::p4::) on duplicate key update companyId = ::p1::, firstName = ::p2::, lastName = ::p3::, fullName = ::p4::', array($userId, $companyId, $user['first_name'], $user['last_name'], $user['first_name'] . ' ' . $user['last_name'])); if (!$rez) { $this->db->rollback(); throw new Exception('User insert failed.'); } // only way to handle deleted stuff is to delete all and insert newly found data $this->runSql('delete from tlms_course_user where userId = ::p0::', array($userId)); foreach ($courseData as $courseId => $exams) { $rez = $this->runSql('insert into tlms_course_user (courseId, userId, examsTaken, examsPassed, certified, certIssuedDate, certExpirationDate, lastUpdated) VALUES (::p0::, ::p1::, ::p2::, ::p3::, ::p4::, ::p5::, ::p6::, now())', array($courseId, $userId, $exams['taken'], $exams['passed'], $certData[$courseId]['certified'], $certData[$courseId]['issued'], $certData[$courseId]['expires'])); if (!$rez) { $this->db->rollback(); throw new Exception('Course data insert failed.'); } } $this->db->commit(); break; } catch (TransactionDeadlockException $e) { // if nested transaction forward exception to initial level/transaction if ($this->db->getTransactionDepthLevel()) { $this->db->rollback(); throw $e; } if (++$numRetry > 9) { throw $e; } } } } catch (Exception $e) { $msg = $e->getMessage(); $this->logError($msg); if (stripos($msg, 'rate limit') !== false) { throw new RateLimitException(); } } if ($updateSummary && $companyId) { $this->updateSummary($companyId); } return true; }
function delete($ctx) { $id = intval($_GET['id']); try { Db::begin(); WxReplyItem::delete($id); WxReplyKeyword::deleteByWhere("item_id='{$id}'"); Db::commit(); } catch (Exception $e) { Db::rollback(); } _redirect($this->_list_url()); return; }
/** * * @param Db $db * @param int $foodid */ function checkExist(&$db, $foodid) { $sql = "select food_id from canteen_food where food_id={$foodid};"; $res = $db->query($sql); if ($res !== false) { if (!empty($res)) { echo getJsonResponse(3, "食物已经存在", null); $db->rollback(); //回滚 $db->close(); exit; } $sql2 = "select user_id from recommendedfood where user_id={$_POST['user_id']} and \n\t\t canteen_id={$_POST['canteen_id']} and food_id={$foodid};"; $res2 = $db->query($sql2); if ($res2 !== false) { if (!empty($res2)) { echo getJsonResponse(4, "食物已经推荐给食堂", null); $db->rollback(); //回滚 $db->close(); exit; } } else { echo getJsonResponse(1, $db->error, null); Log::error_log('database error:' . $db->error . ' in ' . basename(__FILE__)); $db->rollback(); $db->close(); exit; } } else { echo getJsonResponse(1, $db->error, null); Log::error_log('database error:' . $db->error . ' in ' . basename(__FILE__)); $db->rollback(); $db->close(); exit; } }