diff --git a/airtime_mvc/application/models/Preference.php b/airtime_mvc/application/models/Preference.php index c6e6fff49..1dd9124a0 100644 --- a/airtime_mvc/application/models/Preference.php +++ b/airtime_mvc/application/models/Preference.php @@ -28,7 +28,9 @@ class Application_Model_Preference { $cache = new Cache(); $con = Propel::getConnection(CcPrefPeer::DATABASE_NAME); - $con->beginTransaction(); + + //We are using row-level locking in Postgres via "FOR UPDATE" instead of a transaction here + //because sometimes this function needs to be called while a transaction is already started. try { /* Comment this out while we reevaluate it in favor of a unique constraint @@ -40,7 +42,7 @@ class Application_Model_Preference } //Check if key already exists - $sql = "SELECT COUNT(*) FROM cc_pref" + $sql = "SELECT valstr FROM cc_pref" ." WHERE keystr = :key"; $paramMap = array(); @@ -50,13 +52,16 @@ class Application_Model_Preference if ($isUserValue) { $sql .= " AND subjid = :id"; $paramMap[':id'] = $userId; - } + } + + $sql .= " FOR UPDATE"; $result = Application_Common_Database::prepareAndExecute($sql, $paramMap, Application_Common_Database::COLUMN, PDO::FETCH_ASSOC, $con); + $result = count($result); $paramMap = array(); if ($result > 1) { @@ -103,9 +108,7 @@ class Application_Model_Preference PDO::FETCH_ASSOC, $con); - $con->commit(); } catch (Exception $e) { - $con->rollback(); header('HTTP/1.0 503 Service Unavailable'); Logging::info("Database error: ".$e->getMessage()); exit; diff --git a/airtime_mvc/application/models/Show.php b/airtime_mvc/application/models/Show.php index 8fcb64cda..832186d80 100644 --- a/airtime_mvc/application/models/Show.php +++ b/airtime_mvc/application/models/Show.php @@ -847,14 +847,24 @@ SQL; */ public static function createAndFillShowInstancesPastPopulatedUntilDate($needScheduleUntil) { - //UTC DateTime object - $showsPopUntil = Application_Model_Preference::GetShowsPopulatedUntil(); - //if application is requesting shows past our previous populated until date, generate shows up until this point. - if (is_null($showsPopUntil) || $showsPopUntil->getTimestamp() < $needScheduleUntil->getTimestamp()) { - $service_show = new Application_Service_ShowService(); - $ccShow = $service_show->delegateInstanceCreation(null, $needScheduleUntil, true); - Application_Model_Preference::SetShowsPopulatedUntil($needScheduleUntil); + $con = Propel::getConnection(CcPrefPeer::DATABASE_NAME); + try { + $con->beginTransaction(); + + //UTC DateTime object + $showsPopUntil = Application_Model_Preference::GetShowsPopulatedUntil(); + //if application is requesting shows past our previous populated until date, generate shows up until this point. + if (is_null($showsPopUntil) || $showsPopUntil->getTimestamp() < $needScheduleUntil->getTimestamp()) { + $service_show = new Application_Service_ShowService(); + $ccShow = $service_show->delegateInstanceCreation(null, $needScheduleUntil, true); + Application_Model_Preference::SetShowsPopulatedUntil($needScheduleUntil); + } + $con->commit(); + } catch (Exception $e) { + $con->rollBack(); + throw $e; } + } /**