# Data ConcurrencyException when updating multiple deliveries to Delivered

**URL:** <https://forum.litium.com/t/data-concurrencyexception-when-updating-multiple-deliveries-to-delivered/1984>\
**Category:** Forum feedback\
**Tags:** litium-7\
**Created:** [April 13, 2021, 10:14am UTC](https://forum.litium.com/t/data-concurrencyexception-when-updating-multiple-deliveries-to-delivered/1984 "2021-04-13T10:14:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenni73](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@jenni73](https://forum.litium.com/u/jenni73)\
**Post date:** [April 13, 2021, 10:14am UTC](https://forum.litium.com/t/data-concurrencyexception-when-updating-multiple-deliveries-to-delivered/1984/1 "2021-04-13T10:14:10Z")

</div>

Hi,

In an integration for shippments I get shippments for several orders and when some orders has been processed I get below Concurrency error, and the site restarts.

The error occures within:  
delivery.SetDeliveryStatus((short)DeliveryState.Delivered, securityToken);

The delivery status is updated, the Payment is captured, but the orderstatus is not updated to completed.  
When running the same order again, it works.

—Error—  
Exception in Litium appId=/LM/W3SVC/1/ROOT type=Litium.Foundation.Modules.ECommerce.Data.ConcurrencyException message=Delivery (db9f64c1-1a12-4973-82be-6597aaf420f1) has already been changed in the database. Get a new copy of the object and submit your changes again. stack= at Litium.Foundation.Modules.ECommerce.Data.DeliveryDataProvider.Update(DeliveryCarrier carrier, SqlCommand openCommand) at Litium.Foundation.Modules.ECommerce.Data.DeliveryDataProvider.Update(DeliveryCarrier carrier) at Litium.Foundation.Modules.ECommerce.Deliveries.Delivery.UpdateFromCarrier(DeliveryCarrier carrier, SecurityToken token) at Litium.Foundation.Modules.ECommerce.StateTransitionSystem.FiniteStateMachine`1.UpdateDatabase(CarrierType carrier, UpdateFromCarrierHandler updateFromCarrier, SecurityToken token) at Litium.Foundation.Modules.ECommerce.StateTransitionSystem.FiniteStateMachine`1.TransitionAction(StateTransition`1 stateTransition, CarrierType carrier, UpdateFromCarrierHandler updateFromCarrier, SecurityToken token) at Litium.Foundation.Modules.ECommerce.StateTransitionSystem.FiniteStateMachine`1.TransitionAction(Int16 nextStateId, CarrierType carrier, UpdateFromCarrierHandler updateFromCarrier, SecurityToken token) at Litium.Foundation.Modules.ECommerce.Deliveries.Delivery.SetDeliveryStatus(Int16 state, SecurityToken token) at Litium.Accelerator.Tasks.FetchShipmentNumberTask.d\_\_5.MoveNext() in C:\projects\posterton\Src\Litium.Accelerator\Tasks\FetchShipmentNumberTask.cs:line 76 — End of stack trace from previous location where exception was thrown — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch()

---

<div class="post-metadata">

**Author:** ![MartinBrandt](https://avatars.discourse-cdn.com/v4/letter/m/439d5e/32.png) [@MartinBrandt](https://forum.litium.com/u/MartinBrandt)\
**Post date:** [April 13, 2021, 3:59pm UTC](https://forum.litium.com/t/data-concurrencyexception-when-updating-multiple-deliveries-to-delivered/1984/2 "2021-04-13T15:59:10Z")

</div>

Hello… “jenni73” 😇,

Do you update the order as well close to this code? If so in what order do you update your entities (order and delivery)?

---

<div class="post-metadata">

**Author:** ![jenni73](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@jenni73](https://forum.litium.com/u/jenni73)\
**Post date:** [April 13, 2021, 5:01pm UTC](https://forum.litium.com/t/data-concurrencyexception-when-updating-multiple-deliveries-to-delivered/1984/3 "2021-04-13T17:01:09Z")

</div>

> [@MartinBrandt](#):
>
> e to this code?

Halloj Martin;)

No, I update the delivery with:  
delivery.SetActualDeliveryDate(DateTime.Now, securityToken);  
delivery.SetTrackingUrl(shipmentNumber, securityToken);  
delivery.SetDeliveryStatus((short)DeliveryState.Delivered, securityToken);

When the error occur, ActualDeliveryDate and TrackingUrl is set and:  
DeliveryStatus=Delivered, PaymentStatus=Paid but Orderstatus=Processing(as before the job starts)

When everything works, after SetDeliveryStatus, DeliveryStatus= Delivered, PaymentStatus=Paid, Orderstatus=Complete.

I do not update the order directly in this job, only via the delivery.  
//J

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [April 13, 2021, 6:32pm UTC](https://forum.litium.com/t/data-concurrencyexception-when-updating-multiple-deliveries-to-delivered/1984/4 "2021-04-13T18:32:55Z")

</div>

Do the update with the carrier instead, something like

```cs
var carrier = delivery.GetAsCarrier();
carrier.ActualDeliveryDate = DateTime.Now;
carrier.TrackingUrl = shipmentNumber;
delivery.SetValuesFromCarrier(carrier, securityToken);
delivery.SetDeliveryStatus((short)DeliveryState.Delivered, securityToken); // Should be executed after the values are set, that will invoke state transition engine.

```

---

<div class="post-metadata">

**Author:** ![jenni73](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@jenni73](https://forum.litium.com/u/jenni73)\
**Post date:** [April 15, 2021, 5:06am UTC](https://forum.litium.com/t/data-concurrencyexception-when-updating-multiple-deliveries-to-delivered/1984/5 "2021-04-15T05:06:33Z")

</div>

Hi @patric.forsgard,  
I still get the problem with ConcurrencyException with the code  
var deliveryCarrier = delivery.GetAsCarrier();  
deliveryCarrier.ActualDeliveryDate = DateTime.Now;  
deliveryCarrier.TrackingUrl = shipmentNumber;  
delivery.SetValuesFromCarrier(deliveryCarrier, securityToken);

```
                delivery.SetDeliveryStatus((short)DeliveryState.Delivered, securityToken);

```

it looks like the problem only occurs with Klarna V3 payment provider, and when running in a Try-Catch statement, the next order works, if its not Klarana V3.

My Conclusion is that Klarna does something within the delivery.SetDeliveryStatus((short)DeliveryState.Delivered, securityToken) statemend, like sets the deliverystatus to something else first, it does the capture and then tryes to set de delivery to delivered…

however the problem remains ☹
