Hi, and thank you for your tremendous module.
Describe your bug or feature request.
I'm getting the following notice in dblog after a new order is saved:
Drupal\commerce_order\Exception\OrderVersionMismatchException: Attempted to save order 21 with version 3. Current version is 5. in Drupal\commerce_order\Entity\Order->preSave() (line 660 of /home/mysite/public_html/modules/contrib/commerce/modules/order/src/Entity/Order.php).
If a bug, provide steps to reproduce it from a clean install.
Now it is true, I have been deleting orders manually at /admin/commerce/orders, so now there seems to be a mismatch in the database.
Do I have to complete re-install Commerce or can I easily go into phpmyadmin and remove entries?
If yes for phpmyadmin, which entries can I clear please?
Most appreciated 🌷
Comments
Comment #2
liliplanet commentedSo started again with a fresh install, when a recurring order is created, the order is successfully placed, but ..
Drupal\commerce_order\Exception\OrderVersionMismatchException: Attempted to save order 4 with version 6. Current version is 8. in Drupal\commerce_order\Entity\Order->preSave() (line 660 of /home/mysite/public_html/modules/contrib/commerce/modules/order/src/Entity/Order.php).What could possibly be causing this please?
Comment #3
liliplanet commentedComment #4
jsacksick commentedCould you provide the backtrace of the exception please?
You could in the meantime, allow the order save by changing the setting to log the exception instead of throwing it from admin/commerce/config/orders/settings.
Comment #5
liliplanet commentedOh thank you @jsacksick, I checked and seems correctly selected
Sorry, how do I do a backtrace?
Comment #6
liliplanet commentedThe numbers seem to change when a new order is created:
Drupal\commerce_order\Exception\OrderVersionMismatchException: Attempted to save order 9 with version 5. Current version is 7. in Drupal\commerce_order\Entity\Order->preSave() (line 660 of /home/mysite/public_html/modules/contrib/commerce/modules/order/src/Entity/Order.php).This is a fresh install with Commerce Version: 8.x-2.26
and Commerce Recurring Version: 8.x-1.0-beta6
Comment #7
liliplanet commentedJust to let you know the error went away when I downgraded Commerce module to Version: 8.x-2.24
Yes, it worked before, but once I upgraded to Version: 8.x-2.26
OrderVersionMismatchExceptioncame into play.Comment #8
jsacksick commentedOf course the error went away since this was previously not logged... So there's no need to downgrade.
I invite you to read the "Order locking" section from https://www.centarro.io/blog/drupal-commerce-core-225-expands-payment-co....
@Lilliplanet: You're saying the error is constantly reproducible? Every time you place an order that creates a subscription?
Comment #9
liliplanet commented@jsacksick thank you for following up.
This works: With a fresh install of Commerce version 8.x-2.24 and now there is no more OrderVersionMismatchException.
Error: With a fresh install of Commerce version 8.x-2.26, every time you place an order with a recurring subscription, get the error Drupal\commerce_order\Exception\OrderVersionMismatchException.
So, something in 8.x-2.26 causes the mismatch error 😅
PS. Payment processor is Stripe off-session.
Comment #10
jsacksick commentedI'm not sure what you mean, 2.28 doesn't exist yet? You meant 2.24 and 2.26? The error wasn't logged prior to Commerce 2.25, so that is expected...
In other words, the error has always been there, it was just "transparent" previously.
Comment #11
liliplanet commentedSorry, yes I meant 2.26 error mismatched error appears ☺️ 2.24 works fine.
Re transparent, I been checking the database on 2.24, and the order matches the correct version.
2.26 seems to want to match orders with version numbers that don't exist, then creates one. (I think).
Comment #12
liliplanet commented2.24 matches the order and version. 2.26 seems to have order 1 match to an un sequential number.
Comment #13
jsacksick commentedI'm not sure what the screenshot is supposed to show?
Comment #14
liliplanet commentedThat the numbers correspond in 2.24, not in 2.26 😌
Comment #15
jsacksick commentedYour screenshot shows records from the
commerce_order__order_itemsDB table, which doesn't prove anything? This table holds the order items referenced by an order.Once again, prior to Commerce 2.25, no error was logged. So there's a high chance the error was already present, it was simply not logged anywhere.
In any event, it could be a bug in the commerce_recurring module, not commerce itself...
Comment #16
liliplanet commentedThank you for explaining @jsaksick 🌷 I guess I will upgrade to 2.26 and let the errors in dblog continue till a solution is found.
Comment #17
liliplanet commentedI just noticed this is an issue with Stripe gateway
https://www.drupal.org/project/commerce/issues/2656818#comment-14158439
Comment #18
jsacksick commentedfwiw, tried setting up commerce + commerce recurring and stripe.
Placed an order, and didn't get this error in the logs.
I'd be great if you could provide a list of reproducible steps we can reproduce.
Comment #19
liliplanet commentedHi! Thank you so much for you following up @jsacksick 🌷
So fresh install, Commerce 2.26, Commerce Recurring 8.x-1.0-beta6 and Stripe.
The orders go through perfectly, but mismatch error in dblog still there.
Drupal\commerce_order\Exception\OrderVersionMismatchException: Attempted to save order 7 with version 6. Current version is 8. in Drupal\commerce_order\Entity\Order->preSave() (line 660 of /home/mysite/public_html/modules/contrib/commerce/modules/order/src/Entity/Order.php).I'm just so happy that orders are created and can move forward, most appreciated.
Comment #20
sah62 commentedI'm seeing the same error with Commerce Core 8.x-2.26, but I'm not using Commerce Recurring or Stripe. The logged message doesn't include stack trace. It doesn't happen with every order, so I don't know how to reproduce it.
I've also noticed that orders associated with this error have two instances of Shipment #1. One of the shipments changes state when I change the state of the order; the other doesn't. I can delete the shipment that doesn't change state and everything seems fine after the duplicate shipment is deleted.
Comment #21
cchoe1 commentedI'm sorry but this version thing seems half-baked and not ready at all for production. I am getting these errors after updating my Commerce setup and the ironic part is that attempting to save the order and then running into this error is causing the order to get permanently deleted in some cases--the whole point of this feature being preventing data loss. Why was the default setting the one that could potentially cause catastrophic results?
I just checked the past 3 version release notes and do not see a SINGLE mention of this feature being added. I honestly have no idea where the feature even came from. Why was this not documented, considering it's potential to cause devastating results?
Comment #22
cchoe1 commentedpost got duplicated
Comment #23
jsacksick commentedhttps://www.centarro.io/blog/drupal-commerce-core-225-expands-payment-co...
The default setting should log the exception (instead of throwing it), I can't think of a reason why this would cause your order to be deleted TBH...
We've updated Commerce on several on our projects and we've never experienced anything similar...
Comment #24
nno commentedI have the same issue with Commerce 8.x-2.26 and Commerce Authorize.net 8.x-1.3.
Drupal\commerce_order\Exception\OrderVersionMismatchException: Attempted to save order 3069 with version 28. Current version is 29. in Drupal\commerce_order\Entity\Order->preSave() (line 660 of /web/modules/contrib/commerce/modules/order/src/Entity/Order.php).The exception is logged on every order and some orders get duplicate Shippment #1 with different state (attached screenshot). Didn't notice any data loss.
When testing with Manual payment gateway there is no exception, only with Authorize.net both sandbox and live. Not using Commerce Recurring.
Comment #25
cchoe1 commentedI might be wrong with this being the only contributor to causing an order to be deleted but what I saw in the logs was the OrderVersionMismatchException error message as the last error for the order, with the Order entity being completed deleted. It wasn't a clean ->delete(), there were still tons of references to that Order ID on the order_items tables and the custom field tables. I should mention that most of my customizations are within the ecosystem but there are some custom hook_form_alters() or other things that we have to patch certain functionality since we've been on Commerce for over 4 years now. However, I don't have anything that deletes an order on my end.
EDIT: If you want to skip this tl;dr stuff, I have steps on recreating the issue below:
I've done some deeper digging and found 1 cause of issue on my site on the Order Information page. In ShippingInformation.php, the if statement on Line 342 looks like this:
However, this means that it attempts to save the order whenever ShippingInformation gets rebuilt and there is no shipment selected. The buildPaneForm gets called on any ajax call that rebuilds the checkout form. This happens often with anonymous users who have not yet entered in their address and thus have no shipment selected, causing this if statement to be true and then causing an unnecessary save. So what seems to be happening here is that when a coupon is entered, it saves the order once from that and then the rebuilding of the checkout form also causes a second order save ShippingInformation. I believe this was added in the most recent commerce_shipping to auto update the order summary with your shipping selection.
Therefore, I updated the if statement line with this:
if (!$this->hasRateSelected($pane_form, $form_state) && !empty($shipments)) {That prevents an extra save if a rate is selected by default or already existed and prevents ShippingInformation.php from saving an empty $shipments variable and potentially causing double saves.
To recreate the issue:
I cannot get the version mismatch exception to occur locally. Yet if I follow these steps on production, it ALWAYS creates a version exception (currently my site is set to ignore them but it still causes an issue with the Coupon form). Not sure why that would be happening but the issues with the coupon form happen nonetheless, whether the exception gets thrown or not.
After adding that line, I'm no longer seeing errors in production. If anyone is interested, here is a patch--it is patched against "drupal/commerce_shipping" and not "drupal/commerce".
Comment #26
cchoe1 commentedSorry, here is the same patch, but renamed for the contrib guidelines on patch naming.
Comment #27
jsacksick commented@cchoe1: Could you please create an issue and post your patch in the Commerce shipping queue? This patch doesn't belong to this queue.
Comment #28
cchoe1 commentedSure I'll create a new issue over there. But it does result in this OrderVersionMismatchException so I thought it would make sense to at least bring it up here as well.
Edit: here is the new issue in case anyone wants to view what's being said over there
https://www.drupal.org/project/commerce_shipping/issues/3231138
Comment #29
jsacksick commentedThis was fixed in Commerce shipping.
Comment #30
nicxvan commentedI am getting this issue on commerce 2.36
Drupal\commerce_order\Exception\OrderVersionMismatchException: Attempted to save order 56 with version 6. Current version is 7. in Drupal\commerce_order\Entity\Order->preSave() (line 681 of /app/web/modules/contrib/commerce/modules/order/src/Entity/Order.php).
Comment #31
jsacksick commented@nicxvan: Reopening a 2 years old issue isn't going to help.
You'd need to figure out what's causing this on your installation... This could be due to many different reasons, race conditions, badly written code... Without reproducible steps, a backtrace or anything else we can exploit, simply copy/pasting this error message isn't helping in any way.
Also, in case you'd like to report a bug, open a new issue, but please make sure to provide something we can work with.
Comment #32
nicxvan commentedSorry I was going to open a new issue but thought some context from this would be helpful. I was trying to reproduce it and couldn't so I didn't add additional info.
I also ended up tracking down a few separate issues. Happy to open a new issue if it comes up again.
Comment #33
99gs3 commented@jsacksick the exact same error is appearing for me as well.
Drupal\commerce_order\Exception\OrderVersionMismatchException: Attempted to save order 3973 with version 7. Current version is 8. in Drupal\commerce_order\Entity\Order->preSave() (line 681 of /home/mysite/public_html/modules/contrib/commerce/modules/order/src/Entity/Order.php).
There are no other backtraces in the dblog for this error and the error will appear for every order saved in the shoppping cart.
I'm not sure how to reproduce it on my own. It just appears randomly.
If there's any instructions on how to cause a backtrace to appear for the next time this is triggered I would be glad to provide it for further diagnostic.
@nicxvan I am having the same issue. Please let me know if you have figured out the cause.
Comment #34
jsacksick commentedIf you change the setting from admin/commerce/config/orders/settings to "Prevent the save and throw an exception that may cause a fatal error..", the potential data loss will be prevented but it'll cause a crash. You should be able to see what's causing this this way.
Comment #35
99gs3 commented@jsacksick
Thanks I've changed the setting as suggested.
I've also tried to access various page, forms, add and remove items from cart.
But I am not able to cause this error to trigger on it's own.
Will need to observe the log and wait for the next time something to cause this error to trigger.
Thanks and happy Thanksgiving!
Comment #36
99gs3 commented@jsacksick
This new error is now triggering in addition to the previous error.
Drupal\Core\Entity\EntityStorageException: Attempted to save order 3932 with version 787. Current version is 788. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of /home/mysite/public_html/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Comparing the SqlContentEntityStorage.php on my server to the official core 10.1.6 release and there were some differences.
Looking to the date it would appear this particular file was edited previously. Possible from a patch.
Using the official core release version now to verify if problem persists.
Will report back. Thanks.
Comment #37
99gs3 commentedAfter correcting the SqlContentEntityStorage.php and having the setting from admin/commerce/config/orders/settings to "Prevent the save and throw an exception that may cause a fatal error.." The following error is still occuring.
The difference now is that only 1 order will show these codes instead of all orders in the cart.
Type commerce_order
Message Drupal\commerce_order\Exception\OrderVersionMismatchException: Attempted to save order 4016 with version 43. Current version is 44. in Drupal\commerce_order\Entity\Order->preSave() (line 681 of public_html/modules/contrib/commerce/modules/order/src/Entity/Order.php).
Severity Error
And
Type php
Message Drupal\Core\Entity\EntityStorageException: Attempted to save order 4016 with version 43. Current version is 44. in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of public_html/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
Severity Error
Code on line 681 of Order.php
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
if (isset($this->original) && !$this->isNew() && $this->original->getVersion() > $this->getVersion()) {
$mismatch_exception = new OrderVersionMismatchException(sprintf('Attempted to save order %s with version %s. Current version is %s.', $this->id(), $this->getVersion(), $this->original->getVersion()));
$log_only = $this->getEntityType()->get('log_version_mismatch');
if ($log_only) {
watchdog_exception('commerce_order', $mismatch_exception);
}
else {
throw $mismatch_exception;
}
}
$this->setVersion($this->getVersion() + 1);
Code on line 817 of SqlContentEntityStorage.php
public function save(EntityInterface $entity) {
try {
$transaction = $this->database->startTransaction();
$return = parent::save($entity);
// Ignore replica server temporarily.
\Drupal::service('database.replica_kill_switch')->trigger();
return $return;
}
catch (\Exception $e) {
if (isset($transaction)) {
$transaction->rollBack();
}
Error::logException(\Drupal::logger($this->entityTypeId), $e);
throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
}
}
Comment #38
jsacksick commented@99gs3: Apply the patch from #3404282: Improve the order version exception logging and switch back to logging the exception... You should now have access to the strack trace which should hopefully help.
Also, this issue is outdated / closed, so let's stop updating it please :).
Comment #39
nicxvan commentedI've created a new issue to track this issue so we can stop updating this :D https://www.drupal.org/project/commerce/issues/3404382
Comment #40
tonytheferg commentedSaw this in the log, dropping the stack trace here:
I presume this has something to do with a user reloading a cart, maybe with a different device, which causes a refresh, and saves the order, but not sure.