C # MySQL connection thread-safe

0

I have a server that receives hundreds of asynchronous connections and needs to implement MySQL access to different threads. Actually, as I use the Async Socket Begin / End standard of .Net, there is a thread pool, so it is not 1 thread for each connection and even if it were, it would not create 1 MySQL connection for each thread, this would only be valid if 10 connections, with 1 MySQL connection for each socket connected.

How do I implement this? Could you suggest some links for study? I need to use some "designe pattern" where I have a thread-safe pool of MySQL connections, in which it keeps x active connections and releases as the need of the requests? Something like:

Connection myConnection = GetSomeConnection(); // Returns a shared object
Command cmd;

lock(myConnection)
{
    cmd = myConnection.CreateCommand();
}
    
asked by anonymous 01.07.2014 / 21:33

1 answer

1

It seems like a connection pool would be a good idea. One suggestion to implement this (there must be several other answers to this problem) would be to create a class that manages the connections and internally uses a ConcurrentQueue (#

  • The method that takes the connections could be in a loop "while it has no connections, thread.Sleep". When you find one, pick up the connection.

    At the end of the thread, it returns the connection used for the queue. This prevents blocking between threads (no lock type is used).

        
  • 17.07.2014 / 18:42