Bug in OrderStatusService?
Posted: Tue Sep 24, 2013 1:16 pm
In CommerceBuilder.Services.OrderStatusService, method UpdateInventory(Order, OrderStatus), line 157 of build 6169 I have encountered the following issue:
This line executes a HQL Query against the ac_OrderItems table that updates the inventory status of the order's items. However, since this is being executed as a direct HQL query, the OrderItem objects in memory become stale. Any code that uses the same Order object reference passed to this method and accesses its Items collection is working with objects that are not synchronized with the database. An obvious workaround is to reload the items collection in the client code, but client code does not not know that this is necessary until encountering the problem.
Suggested Change:
a) Modify line 157 so that after NHibernateHelper.CreateSQLQuery executes the update, a call to reload the OrderItem collection occurs.
b) Modify line 157 to not use NHibernateHelper.CreateSQLQuery, but rather iterate over the existing collection, updating each item and then save the collection.
EDIT: Upon further investigation, this may not be the issue after all. What I've noticed instead is that setting the inventory action to Destock on the Order Placed event has no effect. This seems to be because when StoreEventEngine.UpdateOrderStatus is called during the OrderPlaced event, the status does not actually update since the status has not changed from its initial value. I mistakenly thought the order item objects were stale since their InventoryStatuses were not set to Destocked after the OrderPlaced event completed. This is still a bug in my opinion as the expected behavior upon setting the inventory action for an Order Status would be that that action would occur, but it does not when that status is the initial status.
This line executes a HQL Query against the ac_OrderItems table that updates the inventory status of the order's items. However, since this is being executed as a direct HQL query, the OrderItem objects in memory become stale. Any code that uses the same Order object reference passed to this method and accesses its Items collection is working with objects that are not synchronized with the database. An obvious workaround is to reload the items collection in the client code, but client code does not not know that this is necessary until encountering the problem.
Suggested Change:
a) Modify line 157 so that after NHibernateHelper.CreateSQLQuery executes the update, a call to reload the OrderItem collection occurs.
b) Modify line 157 to not use NHibernateHelper.CreateSQLQuery, but rather iterate over the existing collection, updating each item and then save the collection.
EDIT: Upon further investigation, this may not be the issue after all. What I've noticed instead is that setting the inventory action to Destock on the Order Placed event has no effect. This seems to be because when StoreEventEngine.UpdateOrderStatus is called during the OrderPlaced event, the status does not actually update since the status has not changed from its initial value. I mistakenly thought the order item objects were stale since their InventoryStatuses were not set to Destocked after the OrderPlaced event completed. This is still a bug in my opinion as the expected behavior upon setting the inventory action for an Order Status would be that that action would occur, but it does not when that status is the initial status.