简体   繁体   中英

.NET with MySqlConnection and Connection Pool

I'm beginner working with .NET and Mysql Connection.

To avoid misunderstanding about Connection pool for .NET I want to ask some my question.

Here is simple my code

class Program
{
    static void Main(string[] args)
    {
        string connectionSetting = "Data Source=127.0.0.1;Database=my_table; User Id=root;Password=root" + ";charset=euckr";

        MySqlConnection con = new MySqlConnection(connectionSetting);

        con.Open();

        MySqlCommand command = new MySqlCommand("insert into log(strLog) values('log1')", con);

        command.ExecuteNonQuery();

        con.Close();
    }
}

I've been trying to how to make connection instance for mysql-server, the answer was singleton, But I found many post in StackOverflow and Some answers say Don't share MySql Connection instance and that's all i have to do.

My question is "Who is controlling Connection Pool?"

In the above code, there is no code that relates to controlling the connection pool such as the GetPool, PutPool and so on calls. So I thought database connection pool is controlled in mysql-server side and there is not any other jobs on the .NET side. Is my understanding correct?

If my thought is right, I'll use the following codes.

class dbcp
{
    public const string m_ConnectionString = "Data Source=127.0.0.1;Database=my_table; User Id=root;Password=root" + ";charset=euckr";

    public MySqlConnection connection;

    public dbcp() { }

    public void QueryLog(string log)
    {
        connection = new MySqlConnection(m_ConnectionString);
        connection.Open();

        string strSQL = "insert into log(strLog) values('log1')";
        MySqlCommand cmd = new MySqlCommand(strSQL, connection);
        cmd.ExecuteNonQuery();

        connection.Close();
    }
}

class SomeClassA
{
    public dbcp dbHandler;
}

class SomeClassB
{
    public dbcp dbHandler;
}

class Program
{
    static void Main(string[] args)
    {
        string connectionSetting = "Data Source=127.0.0.1;Database=gw_ocpp_server; User Id=root;Password=root" + ";charset=euckr";

        MySqlConnection con = new MySqlConnection(connectionSetting);

        con.Open();

        MySqlCommand command = new MySqlCommand("insert into log(strLog) values('log1')", con);

        command.ExecuteNonQuery();

        con.Close();
    }
}

Connection pooling is a technique of creating and managing a pool of connections:

Here's the additional info about connection pooling

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM