Search code examples
c#db2database-connectionpool

DB2 connection pooling


Please consider a piece of code:

    static string GetUser(int id)
    {
        Console.WriteLine("Start get -> {0}", id);

        var connb = new DB2ConnectionStringBuilder(ConfigurationManager.ConnectionStrings["DB2_USER"].ConnectionString);
        connb.Pooling = true;
        connb.MinPoolSize = 10;
        connb.MaxPoolSize = 100;

        var conn = new DB2Connection(connb.ConnectionString);

        var cmd = new DB2Command("SELECT name FROM USR.USERS WHERE id = @id");
        cmd.Parameters.Add("@id", id);
        cmd.Connection = conn;

        conn.Open();

        // conn.IsConnectionFromPool <-- false

        var reader = cmd.ExecuteReader();
        var result = string.Empty;
        while(reader.Read())
            result = reader.GetString(0);

        conn.Close();
        conn.Dispose();

        Console.WriteLine("End get <- {0} {1}", id, result);

        return result;
    }

An IBM .NET Database driver is used in this sample.

Though connection string explicitly defines connection pooling settings, the property IsConnectionFromPool is never true.

Does this really mean that the database driver does not maintain pool for my case? Shall .NET driver settings be tuned in some way for this case?


Solution

  •     connb.Pooling = true;
        connb.MinPoolSize = 10;
        connb.MaxPoolSize = 100;
    

    Pooling will be enabled after exceeding MinPoolSize simultaneous connections.