diff --git a/includes/commerce.controller.inc b/includes/commerce.controller.inc index 1cdd605..fce6a88 100644 --- a/includes/commerce.controller.inc +++ b/includes/commerce.controller.inc @@ -7,7 +7,46 @@ * A full fork of Entity API's controller, with support for revisions. */ -class DrupalCommerceEntityController extends DrupalDefaultEntityController implements EntityAPIControllerInterface { +interface DrupalCommerceEntityControllerInterface extends EntityAPIControllerInterface { + + /** + * Determines whether the provided entity is locked. + * + * @param $entity + * The entity to check. + * + * @return + * True if the entity is locked, false otherwise. + */ + public function isLocked($entity); + + /** + * Determines whether the provided entity is cached. + * + * @param $entity + * The entity to check. + * + * @return + * True if the entity is cached, false otherwise. + */ + public function isCached($entity); + + /** + * Indicate that pessimistic locking should be skipped in this request. + * + * @param $skip_locking + * The boolean indicating whether locking should be skipped. + */ + public function skipLocking(); + + /** + * releases locking on all entities, use with caution + */ + public function releaseLocking(); + +} + +class DrupalCommerceEntityController extends DrupalDefaultEntityController implements DrupalCommerceEntityControllerInterface { /** * Stores our transaction object, necessary for pessimistic locking to work. @@ -21,6 +60,90 @@ class DrupalCommerceEntityController extends DrupalDefaultEntityController imple protected $lockedEntities = array(); /** + * Stores the flag if a condition has been passed for locking + */ + protected $requestLocking = TRUE; + + /** + * Stores whether pessimistic locking should be skipped in this request. + */ + protected $skipLocking = FALSE; + + /** + * Implements DrupalCommerceEntityControllerInterface::isLocked(). + */ + public function isLocked($entity) { + return isset($this->lockedEntities[$entity->{$this->idKey}]); + } + + /** + * Implements DrupalCommerceEntityControllerInterface::isLocked(). + */ + public function isCached($entity) { + return isset($this->entityCache[$entity->{$this->idKey}]); + } + + /** + * Implements DrupalCommerceEntityControllerInterface::skipLocking(). + */ + public function skipLocking($skip_locking = TRUE) { + $this->skipLocking = $skip_locking; + + return $this; + } + + /** + * Determines whether the current entity type requires pessimistic locking. + * + * @return + * True if locking is required, false otherwise. + */ + protected function requiresLocking() { + $enabled = isset($this->entityInfo['locking mode']) && $this->entityInfo['locking mode'] == 'pessimistic'; + $not_skipped = empty($this->skipLocking); + + return $enabled && $not_skipped && $this->requestLocking; + } + + /** + * Checks the list of tracked locked entities, and if it's empty, commits + * the transaction in order to remove the acquired locks. + * + * The transaction is not necessarily committed immediately. Drupal will + * commit it as soon as possible given the state of the transaction stack. + */ + protected function releaseLock() { + if ($this->requiresLocking() && empty($this->lockedEntities)) { + unset($this->controllerTransaction); + } + } + + public function releaseLocking() { + unset($this->controllerTransaction); + $this->lockedEntities = array(); + } + + /** + * Overrides DrupalDefaultEntityController::load(). + */ + public function load($ids = array(), $conditions = array()) { + // Lock by default if the caller didn't indicate a preference. + $conditions += array('_lock' => TRUE); + $this->requestLocking = $conditions["_lock"]; + unset($conditions['_lock']); + + // If locking has been required, then bypass the internal cache for any + // entities that are not already locked. + if ($this->requiresLocking()) { + foreach (array_diff_key(array_flip($ids), $this->lockedEntities) as $id => $value) { + unset($this->entityCache[$id]); + } + } + + return parent::load($ids, $conditions); + } + + /** * Override of DrupalDefaultEntityController::buildQuery(). * * Handle pessimistic locking. @@ -28,7 +151,7 @@ class DrupalCommerceEntityController extends DrupalDefaultEntityController imple protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) { $query = parent::buildQuery($ids, $conditions, $revision_id); - if (isset($this->entityInfo['locking mode']) && $this->entityInfo['locking mode'] == 'pessimistic') { + if ($this->requiresLocking()) { // In pessimistic locking mode, we issue the load query with a FOR UPDATE // clause. This will block all other load queries to the loaded objects // but requires us to start a transaction. @@ -48,41 +171,6 @@ class DrupalCommerceEntityController extends DrupalDefaultEntityController imple return $query; } - public function resetCache(array $ids = NULL) { - parent::resetCache($ids); - - // Maintain the list of locked entities, so that the releaseLock() method - // can know when it's time to commit the transaction. - if (!empty($this->lockedEntities)) { - if (isset($ids)) { - foreach ($ids as $id) { - unset($this->lockedEntities[$id]); - } - } - else { - $this->lockedEntities = array(); - } - } - - // Try to release the lock, if possible. - $this->releaseLock(); - } - - /** - * Checks the list of tracked locked entities, and if it's empty, commits - * the transaction in order to remove the acquired locks. - * - * The transaction is not necessarily committed immediately. Drupal will - * commit it as soon as possible given the state of the transaction stack. - */ - protected function releaseLock() { - if (isset($this->entityInfo['locking mode']) && $this->entityInfo['locking mode'] == 'pessimistic') { - if (empty($this->lockedEntities)) { - unset($this->controllerTransaction); - } - } - } - /** * (Internal use) Invokes a hook on behalf of the entity. * @@ -228,9 +316,9 @@ class DrupalCommerceEntityController extends DrupalDefaultEntityController imple if (!empty($update_base_table)) { // Go back to the base table and update the pointer to the revision ID. db_update($this->entityInfo['base table']) - ->fields(array($this->revisionKey => $entity->{$this->revisionKey})) - ->condition($this->idKey, $entity->{$this->idKey}) - ->execute(); + ->fields(array($this->revisionKey => $entity->{$this->revisionKey})) + ->condition($this->idKey, $entity->{$this->idKey}) + ->execute(); } // Update the static cache so that the next entity_load() will return this diff --git a/modules/order/commerce_order.module b/modules/order/commerce_order.module index 786e013..f713e6d 100644 --- a/modules/order/commerce_order.module +++ b/modules/order/commerce_order.module @@ -746,17 +746,27 @@ function commerce_order_save($order) { /** * Loads an order by ID. + * + * @param $order_id + * The order id. + * @param $lock + * Whether to lock the loaded order. */ -function commerce_order_load($order_id) { - $orders = commerce_order_load_multiple(array($order_id), array()); +function commerce_order_load($order_id, $lock = TRUE) { + $orders = commerce_order_load_multiple(array($order_id), array('_lock' => $lock), FALSE); return $orders ? reset($orders) : FALSE; } /** * Loads an order by number. + * + * @param $order_number + * The order number. + * @param $lock + * Whether to lock the loaded order. */ -function commerce_order_load_by_number($order_number) { - $orders = commerce_order_load_multiple(array(), array('order_number' => $order_number)); +function commerce_order_load_by_number($order_number, $lock = TRUE) { + $orders = commerce_order_load_multiple(array(), array('order_number' => $order_number, '_lock' => $lock), FALSE); return $orders ? reset($orders) : FALSE; } @@ -774,6 +784,8 @@ function commerce_order_load_by_number($order_number) { * the $order_ids array assuming the revision_id is valid for the order_id. * @param $reset * Whether to reset the internal order loading cache. + * @param $lock + * Whether to lock the loaded order. * * @return * An array of order objects indexed by order_id. @@ -782,6 +794,32 @@ function commerce_order_load_multiple($order_ids = array(), $conditions = array( return entity_load('commerce_order', $order_ids, $conditions, $reset); } + /** + * Determines whether or not the given order object is locked. + * + * @param $order + * A fully loaded order object. + * + * @return + * Boolean indicating whether or not the order object is locked. + */ +function commerce_order_is_locked($order) { + return entity_get_controller('commerce_order')->isLocked($order); +} + +/** + * Determines whether or not the given order object is cached. + * + * @param $order + * A fully loaded order object. + * + * @return + * Boolean indicating whether or not the order object is cached. + */ +function commerce_order_is_cached($order) { + return entity_get_controller('commerce_order')->isCached($order); +} + /** * Determines whether or not the given order object represents the latest * revision of the order. @@ -1472,6 +1510,34 @@ function commerce_order_entity_query_alter($query) { } /** + * Implements hook_views_pre_execute(). + * + * Ensures that order views don't lock the loaded orders if not necessary. + */ +function commerce_order_views_pre_execute(&$view) { + if ($view->base_table != 'commerce_order') { + return; + } + + $previous_result = &drupal_static(__FUNCTION__, NULL); + if ($previous_result === FALSE) { + // The previous order view needed locking, which means that locking can't + // be skipped in this request. No need to evaluate other views. + return; + } + + // Locking can't be skipped if the view has form elements, except VBO. + // VBO modifies orders in a multi-step process, so locking is only needed + // after that process is initiated by the user submitting the VBO selection. + $has_form_elements = views_view_has_form_elements($view); + $has_vbo_field = isset($view->field['views_bulk_operations']); + $submitted = isset($_POST['operation']); + $skip_locking = (!$has_form_elements || ($has_vbo_field && !$submitted)); + entity_get_controller('commerce_order')->skipLocking($skip_locking); + $previous_result = $skip_locking; +} + +/** * Implements hook_preprocess_views_view(). * * When the line item summary and order total area handlers are present on Views diff --git a/modules/order/commerce_order_ui.module b/modules/order/commerce_order_ui.module index 8dc2333..be40d8a 100644 --- a/modules/order/commerce_order_ui.module +++ b/modules/order/commerce_order_ui.module @@ -35,6 +35,8 @@ function commerce_order_ui_menu() { ); $items['admin/commerce/orders/%commerce_order'] = array( + // Load the order unlocked, the view page won't modify it. + 'load arguments' => array(FALSE), 'title callback' => 'commerce_order_ui_order_title', 'title arguments' => array(3), 'page callback' => 'commerce_order_ui_order_view', @@ -43,12 +45,14 @@ function commerce_order_ui_menu() { 'access arguments' => array(3), ); $items['admin/commerce/orders/%commerce_order/view'] = array( + 'load arguments' => array(FALSE), 'title' => 'View', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10, 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE, ); $items['admin/commerce/orders/%commerce_order/edit'] = array( + 'load arguments' => array(TRUE), 'title' => 'Edit', 'page callback' => 'commerce_order_ui_order_form_wrapper', 'page arguments' => array(3), @@ -60,6 +64,7 @@ function commerce_order_ui_menu() { 'file' => 'includes/commerce_order_ui.orders.inc', ); $items['admin/commerce/orders/%commerce_order/delete'] = array( + 'load arguments' => array(TRUE), 'title' => 'Delete', 'page callback' => 'commerce_order_ui_order_delete_form_wrapper', 'page arguments' => array(3), @@ -86,6 +91,8 @@ function commerce_order_ui_menu() { ); $items['user/%user/orders/%commerce_order'] = array( + // Load the order unlocked, since it won't be modified. + 'load arguments' => array(FALSE), 'title callback' => 'commerce_order_ui_order_title', 'title arguments' => array(3), 'page callback' => 'commerce_order_ui_order_view', diff --git a/modules/order/includes/commerce_order.controller.inc b/modules/order/includes/commerce_order.controller.inc index ba345a8..b418ad5 100644 --- a/modules/order/includes/commerce_order.controller.inc +++ b/modules/order/includes/commerce_order.controller.inc @@ -54,8 +54,14 @@ class CommerceOrderEntityController extends DrupalCommerceEntityController { * SAVED_NEW or SAVED_UPDATED depending on the operation performed. */ public function save($order, DatabaseTransaction $transaction = NULL) { - if (!isset($transaction)) { - $transaction = db_transaction(); + //we don't really ever want to have 2 transactions, should we really even accept passed in transactions? or at least flush our internal one immediately? + if (isset($transaction)) { + $this->controllerTransaction = $transaction; + } + + //check if we already have a transaction, else create one + if(!isset($this->controllerTransaction)){ + $this->controllerTransaction = db_transaction(); $started_transaction = TRUE; } diff --git a/modules/order/tests/commerce_order.test b/modules/order/tests/commerce_order.test index 1164634..02f0409 100644 --- a/modules/order/tests/commerce_order.test +++ b/modules/order/tests/commerce_order.test @@ -97,6 +97,53 @@ class CommerceOrderCRUDTestCase extends CommerceBaseTestCase { } /** + * Test order locking. + */ + function testCommerceOrderLocking() { + $created_order = $this->createDummyOrder(); + entity_get_controller('commerce_order')->resetCache(); + + // Ensure that loading locked and unlocked orders works. + $unlocked_order = commerce_order_load($created_order->order_id, FALSE); + $this->assertFalse(commerce_order_is_locked($unlocked_order), 'commerce_order_load() returned an unlocked order.'); + $this->assertTrue(commerce_order_is_cached($unlocked_order), 'commerce_order_load() cached the order.'); + $locked_order = commerce_order_load($created_order->order_id); + $this->assertTrue(commerce_order_is_locked($locked_order), 'commerce_order_load() returned a locked order.'); + $this->assertTrue(commerce_order_is_cached($locked_order), 'commerce_order_load() cached the order.'); + entity_get_controller('commerce_order')->releaseLocking(); + $this->assertTrue(commerce_order_is_cached($locked_order), 'commerce_order_load() cached the order.'); + + // Ensure that skipLocking() works. + entity_get_controller('commerce_order')->releaseLocking(); + entity_get_controller('commerce_order')->skipLocking(); + $unlocked_order = commerce_order_load($created_order->order_id); + $this->assertFalse(commerce_order_is_locked($unlocked_order), 'commerce_order_load() returned an unlocked order.'); + + // Simulate an order view that has a non-VBO form field. + $fake_view = new stdClass(); + $fake_view->base_table = 'commerce_order'; + $fake_view->field = array(); + $fake_view->field['test'] = new StdClass(); + $fake_view->field['test']->views_form_callback = 'test_callback'; + commerce_order_views_pre_execute($fake_view); + $result = drupal_static('commerce_order_views_pre_execute', NULL); + $this->assertFalse($result, 'commerce_order_views_pre_execute() did not skip locking.'); + + // Simulate a VBO order view. Since the previous view required locking, + // locking should still not be skipped. + $fake_view->field['views_bulk_operations'] = $fake_view->field['test']; + commerce_order_views_pre_execute($fake_view); + $result = drupal_static('commerce_order_views_pre_execute', NULL); + $this->assertFalse($result, 'commerce_order_views_pre_execute() did not skip locking.'); + + // Start from scratch, test just the VBO order view. + drupal_static_reset('commerce_order_views_pre_execute'); + commerce_order_views_pre_execute($fake_view); + $result = drupal_static('commerce_order_views_pre_execute', NULL); + $this->assertTrue($result, 'commerce_order_views_pre_execute() skipped locking.'); + } + + /** * Test order Token replacement. */ function testCommerceOrderTokens() { diff --git a/modules/payment/commerce_payment_ui.module b/modules/payment/commerce_payment_ui.module index bd728ae..db45ef8 100644 --- a/modules/payment/commerce_payment_ui.module +++ b/modules/payment/commerce_payment_ui.module @@ -13,6 +13,7 @@ function commerce_payment_ui_menu() { // Payment tab on orders. $items['admin/commerce/orders/%commerce_order/payment'] = array( + 'load arguments' => array(TRUE), 'title' => 'Payment', 'page callback' => 'commerce_payment_ui_order_tab', 'page arguments' => array(3),