Hi everyone!
I am currently facing an issue with creating records in the database. I have a ServerAction that ends with the following:
Inside the handler for the TicketUpdated event, I run an Aggregate like this:
Unfortunately, the record is not found.
Interestingly, when I run the app in debug mode and set a breakpoint right before the Aggregate, the record is found.
Does anyone know what might be causing this behavior?
Hello.
Server actions run inside a transaction. The event is trying to get a record that is still not there.
Because you have all the info (you sent the attributes and Create returns the Id) you can send the entire record (if small). Or just do a commit if possible.
In addition: the reason that setting a breakpoint finds the record is because the breakpoint causes a substantial delay, so an auto-commit will already have happened.
When you create a record in a Server Action, it’s not immediately committed to the database.
OutSystems batches the insert and commits it only at the end of the request cycle.
When you call another query (Aggregate or Advanced SQL) in the same request cycle, it often doesn’t see the new record yet, because the transaction hasn’t been committed.
In debug mode, the extra delay caused by breakpoints often gives the platform enough time to flush changes to the DB, which is why it appears to work.
Use CommitTransaction before event call Use carefully. Overusing CommitTransaction can cause performance issues and break transactional consistency.
Hi L,
have you checked the what is happening after event you calling in your code?
have you debugged the code after that event which is shown in your code?
Please try to debug the complete flow including your event one.
You can use outsystems logs action for debugging purpose.
regards,
Manish Jawla
Hi Lukasz
Did you use a Service Action somewhere in the process, that is an autonomous transaction?
Are you sure you are giving the correct TicketHistoryId inside the event?
What happens if you place a CommitTransaction before the raise event?
Best regards, Marlies
Hi @Nuno Reis, @Kilian Hekhuis, @Amit Jain
Thanks for your responses. I now understand the issue better, but I'm a bit confused, because in my app I have another ServerAction with similar logic:
Inside the TicketUpdated handler, I use the same Aggregate as previously, and in this case, it successfully finds the record in the database without calling "CommitTransaction".
What could be the reason for this difference?
Timing. When two things could happen in any order, sometimes by sheer "luck" the order is fairly fixed, while in other cases it seems truly random. The rule of thumb is that if you want to make sure an event handler sees data you just created, make sure you first do a Commit.
Ok, thank you so much. It helps me a lot.
You're most welcome!