//获取池中的连接 public MySqlConnectHelper getConnection() { lock (pool) { MySqlConnectHelper tmp = null; if (pool.Count > 0) { tmp = (MySqlConnectHelper)pool[0]; pool.RemoveAt(0); //不成功 if (!isUserful(tmp)) { //可用的连接数据已去掉一个 useCount--; tmp = getConnection(); } } else { //可使用的连接小于连接数量 if (useCount < PoolSize) { try { //创建连接 MySqlConnectHelper conn = new MySqlConnectHelper(ConnectionStr); conn.OpenConnect(); useCount++; tmp = conn; } catch (Exception e) { } } } return(tmp); } }
public int InitMySqlConnectPool() { int ConnectSize = 0; for (; ConnectSize < PoolSize; ConnectSize++) { MySqlConnectHelper newConnect = new MySqlConnectHelper(ConnectionStr); if (newConnect.OpenConnect() == true) { pool.Add(newConnect); } else { break; } } if (ConnectSize < 1) { return(-1); } return(ConnectSize); }