Hi,

that's 2 days I'm fighting this issue, and I desesperate!

I've seen this issue http://drupal.org/node/1060002, which seems to be related, but error is still there even if the patch has been commited as you can read.

Here is the code which leads to the error :

<?php

/**
 * Implemente hook_menu().
 */
function mymodule_menu() {
    $menu=array();
  
    $menu['mymodule_test'] = array(
    'title' => 'Test',
    'page callback' => 'mymodule_test',
    'access callback' => TRUE,
    'weight' => 7,
    );
       
    return $menu;
}
function mymodule_test(){
	global $user;

    // we load the first product	
    $product=commerce_product_load(1);
    // then we create a line item
    $line_item = commerce_product_line_item_new($product,2);
    // we save it
    commerce_line_item_save($line_item);
    // and we add it to the cart
    commerce_cart_product_add($user->uid, $line_item,TRUE);

    // we go for another product
    $product2=commerce_product_load(2);	
    $line_item2 = commerce_product_line_item_new($product2,2);
    commerce_line_item_save($line_item2);
    commerce_cart_product_add($user->uid, $line_item2,TRUE);	
	
        // and we remove this lines
	$order = commerce_cart_order_load($user->uid);
	$order=commerce_cart_order_product_line_item_delete($order, $line_item->line_item_id,TRUE);
	$order=commerce_cart_order_product_line_item_delete($order,$line_item2->line_item_id,TRUE);
	
        // at the end, we save (tries with saving each time gave the same error)
	commerce_order_save($order);
	
	return "ok";
}
?>

Note that if I deal with only one line (create then remove), all is fine. The error only occurs when I try to deal with two lines, and occurs on the second line deletion.

Removing the rollback when catching the exception shows that the line is indeed removed, but somewhere commerce tries to load the line item.

Anybody seeing what I've done wrong?

Regards,
TahitiClic

Comments

tahiticlic’s picture

Issue summary: View changes

Typo correction

tahiticlic’s picture

In commerce.controller.inc, maybe the

<?php	  foreach ($entities as $id => $entity) {
        $this->invoke('delete', $entity);
      }
?>

block should come prior to the db_delete.

I did this modification and it works well now, I guess that's a deletion priority between references taht must be deleted first and entity itself that must be deleted last.

j0rd’s picture

I ran into the same issue as you.

http://drupal.org/node/1228502#comment-4792490

We need a more clear way to clear the cart.

At the very least commerce_cart_order_product_line_item_delete() should not leave the database in an inconsistent state.

tahiticlic’s picture

Yes, that's embedded in the commerce_cart_order_product_line_item_delete call. The problem is deeper, in commerce controller (even if I can't see why this works if you remove only one line).

rszrama’s picture

Status: Active » Needs review

Any reason you can't use commerce_cart_order_empty()? Also, I think the error may have been resolved by this recent commit if you could pull and try again.

See: http://drupalcode.org/project/commerce.git/commitdiff/b3b9bdf (this removes the entity metadata wrapper from commerce_entity_reference_delete())

j0rd’s picture

I wasn't aware of that function which is why I wasn't calling it. I needed to remove some line items, so I was using that when grepping the code.

With that said, running any function hopefully shouldn't leave the database in a broken state. It should either keep things working or not do anything.

I'll test this out next week.

rszrama’s picture

Yeah, the problem is primarily with entity_metadata_wrapper(), which tries to load a referenced entity when it wraps an entity with a reference property / field. I agree that it shouldn't cause a site to go down to have a stale reference. Apparently I opened #1030140: Do not throw exceptions when wrapping an entity with a stale reference a while ago to address this but never got back with fago; I've updated the issue and would appreciate it if you could chime in there with your experience as well.

fwiw, http://api.drupalcommerce.org may help with finding API functions. I think it needs some more love (I don't like the URLs it generates atm, for example), but all of our APIs are documented and our hooks are all documented through there or through the specification handbook on dc.org itself.

rszrama’s picture

Status: Needs review » Fixed

In trying to duplicate this issue, I can no longer generate the error. I think this is fixed, though #1243306: Implement pessimistic entity locking and enable it for orders seems to be turning up different issues with a new approach to transactions. The safe thing to do will still be to use the empty function, and I won't commit that other patch until I'm sure it accommodates this example.

rszrama’s picture

And it turns out the error in there is a bug in Drupal 7.7 that has already been fixed in dev. I'm going to go ahead with that patch, and if you run into an issue with transaction collision, you just need to update to dev until Drupal 7.8+ comes out (early September at the latest).

tahiticlic’s picture

Well thanks for the solution, I was looking for a transaction problem not beeing able to trace it deeply though.

I can't upgrade Commerce now, the project is (lately) delivered and API changes may break our modules. But I'll test it during the next whole upgrade.

zeta1600’s picture

subscribing.

still getting this error on Drupal 7.x-dev.

I have a rule to remove product if >1 quantity.

rszrama’s picture

@zeta1600 - I'm going to need you to post your Rule up in here. And can you specify the exact error message?

zeta1600’s picture

Thanks for reply rszrama.

My rule is as follows:
Rule:
Event: After adding a product to the cart

Conditions:
User has role(s)
Parameter: User: [site:current-user], Roles: Level 1
Total product quantity comparison
Parameter: Order: [commerce_order], Operator: >

Action:
Remove all products from an order
Parameter: Order to empty: [site:current-cart-order]

I was getting this error:
EntityMetadataWrapperException: Unable to load the commerce_line_item with the id 64. in EntityListWrapper->value() (line 934 of /Users/..../sites/all/modules/entity/includes/entity.wrapper.inc).

But then, I realized you didn't want D7 updated, you wanted DC to dev version. So, I did and cleared cache. Now, I get this error:
PDOException: There is already an active transaction in db_transaction() (line 2479 of /Users/.../includes/database/database.inc).

I have reverted back to D7.7 and DC 7.x-1.0-rc1

Hope you can help.

zeta1600’s picture

Interestingly enough, after reverted and then re-uploaded DC dev again and NOT clear D7 cache, it works fine... hmmm.

zeta1600’s picture

No... I was wrong, I am still getting this error:
PDOException: There is already an active transaction in db_transaction() (line 2479 of /Users/.../includes/database/database.inc).

rszrama’s picture

If you're using Commerce dev (soon to be RC2) and you're getting this error, you need to use Drupal 7.x-dev, not Drupal 7.7.

zeta1600’s picture

Yup, DC dev and D7 dev worked fine. Thanks.

rszrama’s picture

Awesome, glad to hear it. : )

arbel’s picture

don't want to hijack this queue but I'm getting this error with the latest d7 dev and DC-rc2.

I've got the commerce coupon module installed (and commerce shipping).

when I enter an invalid coupon code on the checkout pane, I get a validation error message as expected that the coupon code is invalid. all good up to this point.

now if I try to submit the checkout pane again, with no coupon code, correct coupon code or invalid coupon code, I get:
EntityMetadataWrapperException: Unable to load the commerce_line_item with the id

I'm not really sure what the cause is or how I should go about debuggin this.

Idan

rszrama’s picture

Perhaps the Coupon module is using a line item for coupons and not deleting them properly? I haven't peeked into the Coupon code, so I'm not really sure where you should begin researching it.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Correction on code