|
@@ -12,6 +12,9 @@ using parkMonitor.entity;
|
|
|
|
|
|
namespace parkMonitor.Database2
|
|
|
{
|
|
|
+ /// <summary>
|
|
|
+ /// 连接池管理
|
|
|
+ /// </summary>
|
|
|
class ConnectionPoolManager
|
|
|
{
|
|
|
//public static BlockingQueue bq = new BlockingQueue();
|
|
@@ -53,7 +56,7 @@ namespace parkMonitor.Database2
|
|
|
for (int i = 0; i < max; i++)
|
|
|
{
|
|
|
Connection conn = DatabaseConnPoolFactory.CreateConnection(connectionString);
|
|
|
- if (conn!=null&&conn.mySqlConnFlag)
|
|
|
+ if (conn != null && conn.mySqlConnFlag)
|
|
|
{
|
|
|
bq.Enqueue(conn);
|
|
|
Console.WriteLine("连接成功入队");
|
|
@@ -68,9 +71,9 @@ namespace parkMonitor.Database2
|
|
|
public static Connection GetConnection(BlockingQueue bq)
|
|
|
{
|
|
|
Connection connection = (Connection)bq.Dequeue();
|
|
|
- if (connection!=null&&!CheckConnection(connection, bq))
|
|
|
+ if (connection != null && !CheckConnection(connection, bq))
|
|
|
{
|
|
|
- connection.mySqlConnFlag = false;
|
|
|
+ connection.mySqlConnFlag = false;
|
|
|
while (!connection.mySqlConnFlag)
|
|
|
{
|
|
|
connection.Dispose();
|
|
@@ -81,10 +84,16 @@ namespace parkMonitor.Database2
|
|
|
Console.WriteLine("连接成功出队");
|
|
|
return connection;
|
|
|
}
|
|
|
+ /// <summary>
|
|
|
+ /// 获取连接,超时返回null
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="bq"></param>
|
|
|
+ /// <param name="millisecondsTimeout"></param>
|
|
|
+ /// <returns></returns>
|
|
|
public static Connection GetConnection(BlockingQueue bq, int millisecondsTimeout)
|
|
|
{
|
|
|
object conn = null;
|
|
|
- Connection connection = null;
|
|
|
+ Connection connection = null;
|
|
|
bq.Dequeue(out conn, millisecondsTimeout);
|
|
|
connection = (Connection)conn;
|
|
|
Console.WriteLine("连接出队检测");
|
|
@@ -130,7 +139,7 @@ namespace parkMonitor.Database2
|
|
|
InitConnPooling(max, connectionString);
|
|
|
}
|
|
|
/// <summary>
|
|
|
- /// 检查连接是否可用,3<=连接数<=10
|
|
|
+ /// 检查连接池连接是否可用,3<=连接数<=10
|
|
|
/// </summary>
|
|
|
/// <param name="min"> min = 3</param>
|
|
|
/// <param name="max">max = 10</param>
|
|
@@ -149,7 +158,7 @@ namespace parkMonitor.Database2
|
|
|
{
|
|
|
for (int i = 0; i < total; i++)
|
|
|
{
|
|
|
- conn = GetConnection(bq,1000);
|
|
|
+ conn = GetConnection(bq, 1000);
|
|
|
CheckConnection(conn, bq);
|
|
|
ReleaseConnection(conn, bq);
|
|
|
}
|
|
@@ -168,19 +177,28 @@ namespace parkMonitor.Database2
|
|
|
{
|
|
|
bq.Enqueue(conn);
|
|
|
}
|
|
|
+ /// <summary>
|
|
|
+ /// 检查连接
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="conn"></param>
|
|
|
+ /// <param name="bq"></param>
|
|
|
+ /// <returns></returns>
|
|
|
public static bool CheckConnection(Connection conn, BlockingQueue bq)
|
|
|
{
|
|
|
bool isUseful = true;
|
|
|
try
|
|
|
{
|
|
|
string sql = "select count(*) from garage";
|
|
|
- DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
|
|
|
- object result = access.GetResult();
|
|
|
- if (result != null)
|
|
|
+ if (conn != null)
|
|
|
{
|
|
|
- int r = Convert.ToInt32(result);
|
|
|
- isUseful = true;
|
|
|
- Console.WriteLine("连接测试正常");
|
|
|
+ DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
|
|
|
+ object result = access.GetResult();
|
|
|
+ if (result != null)
|
|
|
+ {
|
|
|
+ int r = Convert.ToInt32(result);
|
|
|
+ isUseful = true;
|
|
|
+ Console.WriteLine("连接测试正常");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
@@ -189,7 +207,7 @@ namespace parkMonitor.Database2
|
|
|
{
|
|
|
conn.Dispose();
|
|
|
}
|
|
|
- AddConnToPooling(1,remoteConf);
|
|
|
+ AddConnToPooling(1, remoteConf);
|
|
|
Console.WriteLine("连接检测失败,重新添加一个连接入队");
|
|
|
isUseful = false;
|
|
|
Console.WriteLine(ex.Message);
|