Solved

DB Connection pool

Posted on 2011-09-07
1
902 Views
Last Modified: 2013-12-17
Hi,

I created a db pool class to use in various applications. Can people tell me ways of getting the best performance/use from it?


public class MySqlConnectionPool
    {

        private string dbUrl;
        private string database;
        private string username;
        private string password;
        private int initPoolSize;

        private string connectionString;

        private ConcurrentBag<MySqlConnection> availableConnections = new ConcurrentBag<MySqlConnection>();

        /// <summary>
        /// Constructor for a mysql connection pool
        /// </summary>
        /// <param name="dbUrl">Url of the db that we wish to connect to</param>
        /// <param name="username">Username that we will use to connect</param>
        /// <param name="password">Password that we will use to connect</param>
        /// <param name="initPoolSize">Size of the pool initially</param>
        public MySqlConnectionPool(string dbUrl,string database, string username, string password,
            int initPoolSize)
        {
            this.dbUrl = dbUrl;
            this.database = database;
            this.username = username;
            this.password = password;
            this.initPoolSize = initPoolSize;

            connectionString = "SERVER = "+this.dbUrl+";DATABASE="+this.database+
                ";UID="+this.username+";PASSWORD="+this.password+";Connection Timeout = 5;";

            for (int i = 0; i < initPoolSize; i++)
            {
                MySqlConnection con = new MySqlConnection(this.connectionString);
                availableConnections.Add(con);
            }
        }

        [MethodImpl(MethodImplOptions.Synchronized)]
        public MySqlConnection checkOut()
        {       
            MySqlConnection con = null;

            while (con == null)
            {
                bool checkedOut = this.availableConnections.TryTake(out con);
            }
            return con;
        }

        public void checkIn(MySqlConnection con)
        {
            if (con.State != ConnectionState.Closed)
                con.Close();

            this.availableConnections.Add(con);
        }


    }

Open in new window

0
Comment
Question by:basil365
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 2

Accepted Solution

by:
AJRDev earned 500 total points
ID: 36497082
I assume that you are using the MySQL Connector/Net class library to connect to MySQL Server. This implements the required ADO.NET interfaces, and as such provides connection pooling by default, so the type of code above is not really neccessary. Every new connection you create, with exactly the same connection string, will be part of a pool implemented internally by the MySQL ADO.NET provider.

The connection string can include Pooling=true, MinimumPoolSize=, MaximumPoolSize=, and various other values to control pooling.
http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlconnection.html#connector-net-examples-mysqlconnection-connectionstring


The only time you should need to write your own pooling is if you are coding connections at the socket level, when you would want to maintain a collection of sockets and their states (as you would if you were writing your own provider).
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question