POS: SQL connections in case of multi-threading

POS: SQL connections in case of multi-threading

[Total: 0    Average: 0/5]

Usually, when we execute any custom SQL statements in POS, we use SQLConnection object from ApplicationSettings.Database.LocalConnection.

This code works without issues when it runs in main POS thread, but if it runs in the background (different thread), sometimes code executing can throw error
System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection’s current state is closed.

The simplest workaround is to create new SQLConnection object for all background threads (in below sample we create new localConnection).

In view of the fact that creating a connection is time-consuming operation, code can be optimized to create SQLConnection object once per thread and reuse it across all code executed by the thread.