public function testHasActiveTransactionReturnsFalse() { $this->assertFalse($this->db->hasActiveTransaction()); $this->db->beginTransaction(); $this->db->commit(); $this->assertFalse($this->db->hasActiveTransaction()); $this->db->beginTransaction(); $this->db->rollBack(); $this->assertFalse($this->db->hasActiveTransaction()); }
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; }
<strong>Warning!</strong> Enter the valid number plece. </div>')); die($output); } $current_stock = $db->query("SELECT currentStock FROM Product WHERE Product_id = :pid ", array("pid" => $product_id)); //check there is stock to remove that no of items if ($current_stock[0]["currentStock"] < $amount) { $output = json_encode(array('type' => 'error', 'text' => '<div class="alert alert-danger"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <strong>Warning!</strong> Sorry there is no that amount of items. </div>')); die($output); } $db->beginTransaction(); $result = $db->query("INSERT INTO Stock (OutStock,product_id) VALUES (:OutStock,:product_id)", array("OutStock" => $amount, "product_id" => $product_id)); if ($result == 1) { $result = $db->query("UPDATE Product SET currentStock = currentStock - :OutStock WHERE Product_id = :product_id", array("OutStock" => $amount, "product_id" => $product_id)); } $db->commit(); if ($result == 1) { $output = json_encode(array('type' => 'text', 'text' => '<div class="alert alert-success"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <strong>Success!</strong> </div>')); } else { $output = json_encode(array('type' => 'error', 'text' => '<div class="alert alert-danger"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <strong>Warning!</strong> </div>')); } die($output);
/** * test_ExecAndTransaction_ExecWithInTrans() * * exec()のテスト(トランザクションあり) */ public function test_ExecAndTransaction_ExecWithInTrans() { $params = ["driver" => $GLOBALS['DB_DRIVER'], "user" => $GLOBALS['DB_USER'], "pass" => $GLOBALS['DB_PASSWORD'], "dbname" => $GLOBALS['DB_DBNAME'], "host" => $GLOBALS['DB_HOST'], "persistent" => false]; $instance = new Db(); $instance->connect($params); // commit pattern $this->assertTrue($instance->beginTransaction()); $this->assertTrue($instance->inTransaction()); $this->assertEquals(1, $instance->exec('INSERT INTO risoluto_db_test(id, column1, column2) values ("10", "TEST_A", "TEST_B");')); $this->assertEquals(2, $this->getconnection()->getRowCount('risoluto_db_test')); $this->assertEquals(10, $instance->lastInsertId()); $this->assertEquals(10, $instance->lastInsertId('id')); $this->assertTrue($instance->commit()); $this->assertFalse($instance->inTransaction()); $this->assertEquals(3, $this->getconnection()->getRowCount('risoluto_db_test')); // Rollback pattern $before_val = $this->getconnection()->createQueryTable('risoluto_db_test', 'SELECT id, column1, column2 FROM risoluto_db_test WHERE id="10";'); $this->assertTrue($instance->beginTransaction()); $this->assertTrue($instance->inTransaction()); $this->assertEquals(1, $instance->exec('UPDATE risoluto_db_test SET column1="TEST_C", column2="TEST_C" WHERE id="10";')); $this->assertEquals(3, $this->getconnection()->getRowCount('risoluto_db_test')); $this->assertEquals(1, $instance->exec('DELETE FROM risoluto_db_test WHERE id="10";')); $this->assertEquals(3, $this->getconnection()->getRowCount('risoluto_db_test')); $this->assertTrue($instance->rollBack()); $this->assertFalse($instance->inTransaction()); $after_val = $this->getconnection()->createQueryTable('risoluto_db_test', 'SELECT id, column1, column2 FROM risoluto_db_test WHERE id="10";'); $this->assertEquals(3, $this->getconnection()->getRowCount('risoluto_db_test')); $this->assertTablesEqual($before_val, $after_val); // Cleaning $this->assertEquals(1, $instance->exec('DELETE FROM risoluto_db_test WHERE id="10";')); $this->assertEquals(2, $this->getconnection()->getRowCount('risoluto_db_test')); $instance->disConnect(); unset($instance); }
/** * 提交事务 * * @param string $host */ public static function commit($host = 'master') { Db::commit($host); }
/** * 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; }
public function commit() { return $this->db->commit(); }
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; }