The SAP service that kept freezing accounting
Work done at Logiztik Alliance Group
Context
Logiztik is a logistics company in Ecuador, handling imports and exports for hundreds of clients. Accounting depended on a small service that took our financial data and sent it to SAP, into a HANA database. It was written in .NET Framework 4.6 and had been running for years.
I was a senior developer there. I did not write the service and it was not mine. By then I had fixed hard-to-find bugs and performance problems in several other services, and people had noticed I was fast at it. Most of the team was busy with other priorities and this was breaking every day, so it came to me.
The problem
The service had not always been bad. As the company grew, it got worse. On important dates it blocked accounting completely, and the IT team had to reset the whole thing by hand, sometimes several times a day.
For accounting, a single operation already took several minutes. When it froze, they waited those minutes, got an error at the end, and then waited for IT to reset it so they could start again. For IT it was the same tedious reset over and over.
The developer who wrote it thought it was a bug somewhere, a loop that never ended and piled up memory until the service fell over. Nobody knew for sure.
Making it debuggable first
The service was really hard to debug as it was. So before looking for the cause, I rebuilt it. I moved it to a newer .NET version that the SAP library supported and redid the project structure. I fixed loops and bugs, added error handling, logging and observability, and asked for a server with more resources.
Along the way I found work that did not need to be slow. For example, the service generated and sent its documents one by one inside a loop, a kind of N+1. I moved that out of the loop and did it in a batch.
I still did not know what the problem was. I shipped all of this anyway, once I was sure it behaved the same as before, because it would make the real bug a lot easier to find. It helped more than I expected. Each operation took half the time or less, and the freezes happened less often. But they still happened.
Finding the cause
At first I had no idea. I tried a lot of things. My first guess was a missing
await somewhere, blocking threads. It was not that.
The clue came when I started several instances of the service locally. I paused the debugger in one of them right after it logged into HANA. The next instance I tried was blocked completely. It did not fail, it just stopped.
Our licence limited how we could connect: only one session could be logged into the database at a time. And for whatever reason, that version of HANA did not release the session as soon as a thread logged out. So when two operations overlapped, the second one waited for a login that never came. I never found out why HANA behaved like that.
To confirm it, I asked someone in accounting to run something that would take a long time, and watched it happen.
The fix
The better fix was probably to stop logging in on every operation and keep one long-lived connection. We decided against it. With only one session available, anyone who needed to get into the database by hand would have had to kill the app first. I was not fully convinced, but my boss had worked with this SAP service much longer than me and knew risks I did not. So I looked for something else.
There was not much to go on. I did not have access to SAP support. The SDK had almost no documentation, online or anywhere else. I did not find any support for connection pools, and more licences were not an option.
So I added a semaphore. Only one thread can be logged into SAP and running operations at a time. The rest wait their turn.
That costs some time when several people use the service at once, since a request waits for the other inserts to finish. Those waits were short, and with everything else faster, accounting was happy with it. Nobody pushed back either. Nobody had a better idea, and if it did not work we would keep trying.
Result
It never froze again. It ran for at least a year with no resets.
What I would tell a client
First, if you can, get a provider that actually supports you. Figuring out how their database behaves is their job, not yours. SAP is used so widely that I do not think this should have been so hard. If I had been able to work with them and get proper documentation for the SDK, I am sure there were better options.
If that is not possible, then look for the fix yourself. Our cause was very specific and I doubt many people will hit the same one. But when a whole app freezes like this, something is usually blocking threads. That is where I would start.