Tools
ConnectionPool.cs
using MySql.Data.MySqlClient;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Newbe.Mahua.Plugins.iChunqiuQQBoot.Beta.Tools
{
///
/// MySQL数据库管理工具类
///
clast ConnectionPool
{
// 池管理对象
private static ConnectionPool connectionPool = null;
// 池管理对象实例类
private static Object objlock = typeof(ConnectionPool);
// 池中连接数
private int size = 100;
// 链接保存集合
private ArrayList pool = null;
// 已经使用的连接数
private int useCount = 0;
// 数据库链接字符串
private String connectionStr = "";
///
/// 无参构造
///
public ConnectionPool() {
connectionStr = Constants.connectionStr;
// 创建可用链接集合
pool = new ArrayList();
}
///
/// 获取数据库连接池
///
///
public static ConnectionPool getPool() {
lock (objlock)
{
if (connectionPool == null)
{
connectionPool = new ConnectionPool();
}
return connectionPool;
}
}
///
/// 获取数据库链接对象
///
///
public MySqlConnection getConnection() {
lock (pool)
{
MySqlConnection mySqlConnection = null;
if (pool.Count > 0)
{
mySqlConnection = (MySqlConnection)pool[0];
mySqlConnection.Open();
// 在可用连接中移除此链接
pool.RemoveAt(0);
// 不成功
if (isUserful(mySqlConnection))
{
// 可用的连接数据已去掉一个
useCount--;
mySqlConnection = getConnection();
}
}
else
{
// 可用链接小于链接数量
if (useCount