basil365
asked on
DB Connection pool
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?
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);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.