Skip to main content
deleted 126 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

MySql Connection Pool Multithreaded app for writing to a MySQL database

I have a multi-threaded app (WCF) that writes to a MySqlMySQL db. I want to use MySql'sMySQL's built in connection pooling abilities. 

As far as I can tell, to do that, I should set MySqlConnectionStringBuilder.MinimumPoolSize to some value approximately equal to the number of threads I connect. Then Open/Close the connection for each call to the db. Is this correct? If not, what is the proper way to use pooling?

Here is the function I use to send data to the db. It gets called by many threads throughout the day. Hundreds, maybe thousands of times per day.

private static void execute(MySqlCommand cmd)
{
    try
    {
        MySqlConnectionStringBuilder cnx = new MySqlConnectionStringBuilder();
        cnx.Server = MySqlServer;
        cnx.Database = Database;
        cnx.UserID = UserId;
        cnx.Password = Pass;
        cnx.MinimumPoolSize = 100;
        cmd.Connection = new MySqlConnection(cnx.ToString());
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
    }
    catch (MySqlException e)
    {
        System.Console.WriteLine(e.Message);
    }
    finally
    {
        if (null != cmd.Connection)
        {
            cmd.Connection.Close();
        }
    }
}

The reason I am asking here instead of over at SO is that this code works. I'm just not sure if it is actually using pooling.

MySql Connection Pool

I have a multi-threaded app (WCF) that writes to a MySql db. I want to use MySql's built in connection pooling abilities. As far as I can tell, to do that, I should set MySqlConnectionStringBuilder.MinimumPoolSize to some value approximately equal to the number of threads I connect. Then Open/Close the connection for each call to the db. Is this correct? If not, what is the proper way to use pooling?

Here is the function I use to send data to the db. It gets called by many threads throughout the day. Hundreds, maybe thousands of times per day.

private static void execute(MySqlCommand cmd)
{
    try
    {
        MySqlConnectionStringBuilder cnx = new MySqlConnectionStringBuilder();
        cnx.Server = MySqlServer;
        cnx.Database = Database;
        cnx.UserID = UserId;
        cnx.Password = Pass;
        cnx.MinimumPoolSize = 100;
        cmd.Connection = new MySqlConnection(cnx.ToString());
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
    }
    catch (MySqlException e)
    {
        System.Console.WriteLine(e.Message);
    }
    finally
    {
        if (null != cmd.Connection)
        {
            cmd.Connection.Close();
        }
    }
}

The reason I am asking here instead of over at SO is that this code works. I'm just not sure if it is actually using pooling.

Multithreaded app for writing to a MySQL database

I have a multi-threaded app (WCF) that writes to a MySQL db. I want to use MySQL's built in connection pooling abilities. 

As far as I can tell, to do that, I should set MySqlConnectionStringBuilder.MinimumPoolSize to some value approximately equal to the number of threads I connect. Then Open/Close the connection for each call to the db. Is this correct? If not, what is the proper way to use pooling?

Here is the function I use to send data to the db. It gets called by many threads throughout the day. Hundreds, maybe thousands of times per day.

private static void execute(MySqlCommand cmd)
{
    try
    {
        MySqlConnectionStringBuilder cnx = new MySqlConnectionStringBuilder();
        cnx.Server = MySqlServer;
        cnx.Database = Database;
        cnx.UserID = UserId;
        cnx.Password = Pass;
        cnx.MinimumPoolSize = 100;
        cmd.Connection = new MySqlConnection(cnx.ToString());
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
    }
    catch (MySqlException e)
    {
        System.Console.WriteLine(e.Message);
    }
    finally
    {
        if (null != cmd.Connection)
        {
            cmd.Connection.Close();
        }
    }
}
Source Link
001
  • 250
  • 3
  • 11

MySql Connection Pool

I have a multi-threaded app (WCF) that writes to a MySql db. I want to use MySql's built in connection pooling abilities. As far as I can tell, to do that, I should set MySqlConnectionStringBuilder.MinimumPoolSize to some value approximately equal to the number of threads I connect. Then Open/Close the connection for each call to the db. Is this correct? If not, what is the proper way to use pooling?

Here is the function I use to send data to the db. It gets called by many threads throughout the day. Hundreds, maybe thousands of times per day.

private static void execute(MySqlCommand cmd)
{
    try
    {
        MySqlConnectionStringBuilder cnx = new MySqlConnectionStringBuilder();
        cnx.Server = MySqlServer;
        cnx.Database = Database;
        cnx.UserID = UserId;
        cnx.Password = Pass;
        cnx.MinimumPoolSize = 100;
        cmd.Connection = new MySqlConnection(cnx.ToString());
        cmd.Connection.Open();
        cmd.ExecuteNonQuery();
    }
    catch (MySqlException e)
    {
        System.Console.WriteLine(e.Message);
    }
    finally
    {
        if (null != cmd.Connection)
        {
            cmd.Connection.Close();
        }
    }
}

The reason I am asking here instead of over at SO is that this code works. I'm just not sure if it is actually using pooling.