|
@@ -5,6 +5,7 @@ using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using MySql.Data.MySqlClient;
|
|
|
using System.Collections;
|
|
|
+using System.Configuration;
|
|
|
|
|
|
namespace parkMonitor.DataBase
|
|
|
{
|
|
@@ -15,15 +16,31 @@ namespace parkMonitor.DataBase
|
|
|
{
|
|
|
private static DBConnectionPool connectionPool = null;//池管理对象
|
|
|
private static Object objlock = typeof(DBConnectionPool);//池管理对象实例
|
|
|
- private int size = 1;//池中连接数
|
|
|
+ private int Min_Pool_Size { get; set; }//最小连接数
|
|
|
+ private int Max_Pool_Size { get; set; }//最大连接数
|
|
|
private int useCount = 0;//已经使用连接数
|
|
|
private ArrayList pool = null;//连接保存的集合
|
|
|
private static string connectionStr = "";//连接字符串
|
|
|
|
|
|
public DBConnectionPool(string connectionString)
|
|
|
{
|
|
|
+ try
|
|
|
+ {
|
|
|
+ Min_Pool_Size = Convert.ToInt32(ConfigurationManager.AppSettings["Min_Pool_Size"]);
|
|
|
+ Max_Pool_Size = Convert.ToInt32(ConfigurationManager.AppSettings["Max_Pool_Size"]);
|
|
|
+ }
|
|
|
+ catch { }
|
|
|
connectionStr = connectionString;
|
|
|
pool = new ArrayList();
|
|
|
+ for (int i = 0; i < 10; i++)
|
|
|
+ {
|
|
|
+ MySqlConnection conn = null;
|
|
|
+ conn = CreateConnection(conn);
|
|
|
+ if (conn != null)
|
|
|
+ {
|
|
|
+ pool.Add(conn);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 创建获取池对象
|
|
@@ -65,7 +82,7 @@ namespace parkMonitor.DataBase
|
|
|
else
|
|
|
{
|
|
|
//可使用连接小于连接数
|
|
|
- if (useCount <= size)
|
|
|
+ if (useCount <= Min_Pool_Size)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
@@ -79,7 +96,7 @@ namespace parkMonitor.DataBase
|
|
|
if (conn == null)
|
|
|
{
|
|
|
//达到最大连接数递归调用获取连接否则创建新连接
|
|
|
- if (useCount <= size)
|
|
|
+ if (useCount <= Min_Pool_Size)
|
|
|
{
|
|
|
conn = getConnection();
|
|
|
}
|
|
@@ -100,9 +117,13 @@ namespace parkMonitor.DataBase
|
|
|
{
|
|
|
MySqlConnection conn = new MySqlConnection(connectionStr);
|
|
|
conn.Open();
|
|
|
- useCount++;
|
|
|
- temp = conn;
|
|
|
- return temp;
|
|
|
+ if (useCount < Max_Pool_Size)
|
|
|
+ {
|
|
|
+ useCount++;
|
|
|
+ temp = conn;
|
|
|
+ return temp;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 连接是否创建成功可用
|