Given: a database on PostgreSQL, which has 2 stored procedures. There are konsolka in C# with the Npgsql driver. In console you need to create 1000 threads, each of which has its connection and causes these same hranilki. Call flow and request the following:
new Thread(() => { Thread.CurrentThread.IsBackground = true; for (var i = 0; i < 500; i++) { try { var thread = new Thread(() => { Console.WriteLine($"Running thread: {Thread.CurrentThread.ManagedThreadId}"); SaveByteA(connectionString); var a = 2 + 4; }) {IsBackground = true,Priority = ThreadPriority.AboveNormal}; thread.Start(); } catch (Exception e) { Console.WriteLine(e); throw; } } Console.WriteLine("Finished Thread #1."); }) { IsBackground = true, Priority = ThreadPriority.Highest }.Start(); new Thread(() => { Thread.CurrentThread.IsBackground = true; for (var i = 0; i < 500; i++) { try { var thread = new Thread(() => { Console.WriteLine($"Running 2 thread: {Thread.CurrentThread.ManagedThreadId}"); SaveByteB(connectionString); }) { IsBackground = true, Priority = ThreadPriority.AboveNormal }; thread.Start(); } catch (Exception e) { Console.WriteLine(e); throw; } } Console.WriteLine("Finished Thread #2."); }) { IsBackground = true, Priority = ThreadPriority.Highest}.Start();
It is necessary: to Make so many requests (i.e. threads) to the number of simultaneous connected clients running transactions was approximately ~1000.
Problem :when I launch the stand with a small number of threads, all handled quickly -- DB is very powerful, so the result in ~1000 connection and process the transaction disappears. And when you put a large number of threads, you get a SocketException-connection interrupted forcibly by remote host. (Reception console complains about the database and the database on the client. who is right-I do not understand).
What my problem is? Where to dig? There are better options of threading or loading of the database as written in the statement?