When writing an http post call inside a for loop with TypeScript, I encountered an issue while debugging the backend method. It seems that the requests are being handled simultaneously.
For instance, if the server needs to execute 2 methods M1()
and then M2()
for each single request, and in the case of n = 2, it ends up executing M1()
twice for request 1, M1()
again for request 2, followed by M2()
for request 2, and finally M2()
once more for request 2.
After the _session.commit()
, an exception is thrown in the intercept method with the following description:
NHibernate.StaleObjectStateException: 'Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [Server.Model.Identity.ApplicationRole#3]'
Here's the relevant code snippet:
public calculate(index?: number): void {
for (var i = 0; i < this.policy.coverages.length; i++) {
this.callCalculate(i);
}
}
// More code snippets here...
Any suggestions on how to resolve this issue?