kingwang1995 6 年之前
父節點
當前提交
540faea8d8

+ 37 - 16
parkMonitor/Database2/AbstractCommand.cs

@@ -26,11 +26,6 @@ namespace parkMonitor.Database2
             this.sql = sql;
             this.conn = conn;
         }
-        public AbstractCommand(Connection conn, List<string> strs)
-        {
-            this.conn = conn;
-            this.strs = strs;
-        }
 
         protected MySqlCommand CreateCommand()
         {
@@ -127,6 +122,10 @@ namespace parkMonitor.Database2
             {
                 Console.WriteLine(ex.Message);
                 Console.WriteLine("ExecuteScalar执行异常");
+                if(conn != null)
+                {
+                    conn.Dispose();
+                }
             }
             return result;
         }
@@ -160,20 +159,22 @@ namespace parkMonitor.Database2
             strs = null;
         }
         public TransactionCommand(string sql, Connection conn, List<string> strs)
-            : base(conn,strs)
+            : base(sql,conn)
         {
             this.strs = strs;
         }
         public override object ExecuteSqlCommand()
         {
-            object result = null;
-            MySqlCommand myCommand = conn.mySqlConnection.CreateCommand();
-            MySqlTransaction myTrans;
-            myTrans = conn.mySqlConnection.BeginTransaction();
-            myCommand.Connection = conn.mySqlConnection;
-            myCommand.Transaction = myTrans;
+            int result = 0;
+            bool flag = true;
+            MySqlCommand myCommand = null;
+            MySqlTransaction myTrans = null;
             try
             {
+                myCommand = conn.mySqlConnection.CreateCommand();                
+                myTrans = conn.mySqlConnection.BeginTransaction();
+                myCommand.Connection = conn.mySqlConnection;
+                myCommand.Transaction = myTrans;
                 for (int i = 0; i < strs.Count(); i++)
                 {
                     myCommand.CommandText = strs[i];
@@ -181,12 +182,16 @@ namespace parkMonitor.Database2
                     result = Convert.ToInt32(myCommand.LastInsertedId);
                 }
                 myTrans.Commit();
+                flag = true;
             }
             catch (Exception e)
             {
                 try
                 {
-                    myTrans.Rollback();
+                    if (myTrans != null)
+                    {
+                        myTrans.Rollback();
+                    }
                 }
                 catch (MySqlException ex)
                 {
@@ -195,19 +200,35 @@ namespace parkMonitor.Database2
                         Console.WriteLine(ex.Message + "事务回滚失败");
                     }
                 }
+                flag = false;
                 Console.WriteLine(e.Message + "事务操作失败");
             }
             finally
             {
-                myCommand.Dispose();
-                myTrans.Dispose();
+                if (myCommand != null)
+                {
+                    myCommand.Dispose();
+                }
+                if (myTrans != null)
+                {
+                    myTrans.Dispose();
+                }
                 //ConnectionPoolManager.ReleaseConnection(conn);
             }
-            return result;
+            TransactionResult transactionResult = new TransactionResult();
+            transactionResult.result = result;
+            transactionResult.flag = flag;
+            return transactionResult;
         }
     }
     /// <summary>
     /// 枚举类型
     /// </summary>
     enum CommandTypes { QUERYCOMMAND, NOQUERYCOMMAND, SCALARCOMMAND, TRANSACTIONCOMMAND, STOREPROCEDURECOMMAND };
+
+    public class TransactionResult
+    {
+        public int result { get; set; }
+        public bool flag { get; set; }
+    }
 }

+ 39 - 20
parkMonitor/Database2/ConnectionPoolManager.cs

@@ -8,6 +8,7 @@ using System.Collections.Concurrent;
 using parkMonitor.tools;
 using MySql.Data.MySqlClient;
 using System.Configuration;
+using parkMonitor.entity;
 
 namespace parkMonitor.Database2
 {
@@ -64,12 +65,22 @@ namespace parkMonitor.Database2
         public static Connection GetConnection(BlockingQueue bq)
         {
             Connection connection = (Connection)bq.Dequeue();
+            if (!CheckConnection(connection, bq)) 
+            {
+                connection.mySqlConnFlag = false;
+            }
             while (!connection.mySqlConnFlag)
             {
-                connection = (Connection)bq.Dequeue();                
+                connection = (Connection)bq.Dequeue();
             }
             return connection;
         }
+        public static Connection GetConnection(BlockingQueue bq,int millisecondsTimeout)
+        {
+            object conn = null;
+            bq.Dequeue(out conn, millisecondsTimeout);
+            return (Connection) conn;
+        }
         /// <summary>
         /// 移除连接
         /// </summary>
@@ -118,24 +129,8 @@ namespace parkMonitor.Database2
             {
                 for (int i = 0; i < total; i++)
                 {
-                    conn = GetConnection(bq);
-                    try
-                    {
-                        string sql = "select count(1) from garage";
-                        DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
-                        object result = access.GetResult();
-                        if (result != null)
-                        {
-                            int r = Convert.ToInt32(result);
-                            Console.WriteLine("连接测试正常");
-                        }
-                        ReleaseConnection(conn, bq);
-                    }
-                    catch (Exception ex)
-                    {
-                        conn.Dispose();
-                        Console.WriteLine(ex.Message);
-                    }
+                    conn = GetConnection(bq,1000);
+                    CheckConnection(conn, bq);
                 }
             }
             if (bq.Count() < min)
@@ -152,12 +147,36 @@ namespace parkMonitor.Database2
         {
             bq.Enqueue(conn);
         }
+        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)
+                {
+                    int r = Convert.ToInt32(result);
+                    isUseful = true;
+                    Console.WriteLine("连接测试正常");
+                }
+                ReleaseConnection(conn, bq);
+            }
+            catch (Exception ex)
+            {
+                conn.Dispose();
+                isUseful = false;
+                Console.WriteLine(ex.Message);
+            }
+            return isUseful;
+        }
         /// <summary>
         /// 关闭所有连接
         /// </summary>
         public static void CloseAll(BlockingQueue bq)
         {
-            if (bq.Count() > 0)
+            if (bq!=null)
             {
                 while (bq.Count() > 0)
                 {

+ 5 - 6
parkMonitor/Database2/DBAccess.cs

@@ -17,7 +17,6 @@ namespace parkMonitor.Database2
         {
             sql = null;
             conn = null;
-
         }
 
         public DBAccess(string sql, Connection conn, CommandTypes commType)
@@ -34,7 +33,6 @@ namespace parkMonitor.Database2
             this.conn = conn;
             this.commType = commType;
             this.strs = strs;
-
         }
 
         public DBAccess(string sql, Connection conn, CommandTypes commType, Dictionary<string, string> args)
@@ -55,13 +53,14 @@ namespace parkMonitor.Database2
             return result;
         }
 
-        public object DealTransaction()
+        public TransactionResult DealTransaction(List<string> strs)
         {
-            object result = null;
-            AbstractCommand abscommand = SimpleCommandFactory.CreateCommandAndExcute(commType);
+            TransactionResult result = null;
+            TransactionCommand abscommand = (TransactionCommand) SimpleCommandFactory.CreateCommandAndExcute(commType);
             abscommand.strs = strs;
             abscommand.conn = conn;
-            result = abscommand.ExecuteSqlCommand();
+            abscommand.sql = sql;
+            result = (TransactionResult)abscommand.ExecuteSqlCommand();
             return result;
         }
     }

+ 479 - 294
parkMonitor/Database2/DBOperation.cs

@@ -14,6 +14,7 @@ namespace parkMonitor.Database2
     /// </summary>
     public class DBOperation
     {
+        public const int TIMEOUT = 120000;
         /// <summary>
         /// 查询车库表获得剩余车位数
         /// </summary>
@@ -25,36 +26,52 @@ namespace parkMonitor.Database2
             MySqlDataReader reader = null;
             int garageFreeSpace = 0;
             string sql = "select * from garage where garageID = '" + garageID + "'";
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
-            object result = access.GetResult();
-            try
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
             {
-                reader = (MySqlDataReader)result;
-                while (reader.Read())
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
+            }
+            if (conn == null)
+            {
+                Console.WriteLine("请检查网络!等待网络修复超时");
+            }
+            else
+            {
+                DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
+                object result = access.GetResult();
+                try
                 {
-                    if (reader.HasRows)
+                    reader = (MySqlDataReader)result;
+                    while (reader.Read())
                     {
-                        garageFreeSpace = reader.GetInt32("garageFreeSpace");
-                        return garageFreeSpace;
+                        if (reader.HasRows)
+                        {
+                            garageFreeSpace = reader.GetInt32("garageFreeSpace");
+                        }
+                        else
+                        {
+                            Console.WriteLine("车位剩余数查无结果");
+                        }
                     }
-                    else
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.Message + "查询剩余车位异常");
+                }
+                finally
+                {
+                    if (reader != null)
                     {
-                        Console.WriteLine("车位剩余数查无结果");
+                        reader.Close();
+                        reader.Dispose();
                     }
-                }               
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine(ex.Message+"查询剩余车位异常");
-            }
-            finally
-            {
-                reader.Close();
-                reader.Dispose();
-                ConnectionPoolManager.ReleaseConnection(conn, bq);
+                    ConnectionPoolManager.ReleaseConnection(conn, bq);
+                }
             }
-            return 0;
+            return garageFreeSpace; ;
         }
         /// <summary>
         /// 查询所有车位位置及状态
@@ -67,37 +84,54 @@ namespace parkMonitor.Database2
             Dictionary<int, Parking_Space> lps = new Dictionary<int, Parking_Space>();
             MySqlDataReader reader = null;
             string sql = "select * from parkingspace where garageID = '" + garageID + "' ";
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
-            object result = access.GetResult();
-            try
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
             {
-                reader = (MySqlDataReader)result;
-                while (reader.Read())
-                {
-                    if (reader.HasRows)
-                    {
-                        Parking_Space ps = new Parking_Space();
-                        ps.parkingSpaceID = reader.GetInt32("parkingSpaceID");
-                        ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
-                        ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
-                        ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
-                        ps.parkingSpaceState = reader.GetInt32("parkingSpaceState");
-                        ps.garageID = garageID;
-                        lps.Add(ps.parkingSpaceID, (Parking_Space)ps.Clone());
-                    }
-                }
-                return lps;
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
             }
-            catch (Exception ex)
+            if (conn == null)
             {
-                Console.WriteLine(ex.Message + "查询车位表异常");
+                Console.WriteLine("请检查网络!等待网络修复超时");
             }
-            finally
+            else
             {
-                reader.Close();
-                reader.Dispose();
-                ConnectionPoolManager.ReleaseConnection(conn, bq);
+                DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
+                object result = access.GetResult();
+                try
+                {
+                    reader = (MySqlDataReader)result;
+                    while (reader.Read())
+                    {
+                        if (reader.HasRows)
+                        {
+                            Parking_Space ps = new Parking_Space();
+                            ps.parkingSpaceID = reader.GetInt32("parkingSpaceID");
+                            ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
+                            ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
+                            ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
+                            ps.parkingSpaceState = reader.GetInt32("parkingSpaceState");
+                            ps.garageID = garageID;
+                            lps.Add(ps.parkingSpaceID, (Parking_Space)ps.Clone());
+                        }
+                    }
+                    return lps;
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.Message + "查询车位表异常");
+                }
+                finally
+                {
+                    if (reader != null)
+                    {
+                        reader.Close();
+                        reader.Dispose();
+                    }
+                    ConnectionPoolManager.ReleaseConnection(conn, bq);
+                }
             }
             return null;
         }
@@ -112,38 +146,55 @@ namespace parkMonitor.Database2
             Vehicle v = new Vehicle();
             MySqlDataReader reader = null;
             string sql = "select * from vehicle where numberPlate = '" + numberPlate + "'";
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
-            object result = access.GetResult();
-            try
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
             {
-                reader = (MySqlDataReader)result;
-                while (reader.Read())
-                {
-                    if (reader.HasRows)
-                    {
-                        int parkingSpaceID = reader.GetInt32("parkingSpaceID");
-                        int garageID = reader.GetInt32("garageID");
-                        int frontwheelbase = reader.GetInt32("frontwheelbase");
-                        int rearwheelbase = reader.GetInt32("rearwheelbase");
-                        v.parkingRecordsID = reader.GetInt32("parkingRecordsID");
-                        v.parkingSpaceID = parkingSpaceID;
-                        v.garageID = garageID;
-                        v.frontwheelbase = frontwheelbase;
-                        v.rearwheelbase = rearwheelbase;
-                        return v;
-                    }
-                }
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
             }
-            catch (Exception ex)
+            if (conn == null)
             {
-                Console.WriteLine(ex.Message + "查询车辆表异常");
+                Console.WriteLine("请检查网络!等待网络修复超时");
             }
-            finally
+            else
             {
-                reader.Close();
-                reader.Dispose();
-                ConnectionPoolManager.ReleaseConnection(conn, bq);
+                DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
+                object result = access.GetResult();
+                try
+                {
+                    reader = (MySqlDataReader)result;
+                    while (reader.Read())
+                    {
+                        if (reader.HasRows)
+                        {
+                            int parkingSpaceID = reader.GetInt32("parkingSpaceID");
+                            int garageID = reader.GetInt32("garageID");
+                            int frontwheelbase = reader.GetInt32("frontwheelbase");
+                            int rearwheelbase = reader.GetInt32("rearwheelbase");
+                            v.parkingRecordsID = reader.GetInt32("parkingRecordsID");
+                            v.parkingSpaceID = parkingSpaceID;
+                            v.garageID = garageID;
+                            v.frontwheelbase = frontwheelbase;
+                            v.rearwheelbase = rearwheelbase;
+                            return v;
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.Message + "查询车辆表异常");
+                }
+                finally
+                {
+                    if (reader != null)
+                    {
+                        reader.Close();
+                        reader.Dispose();
+                    }
+                    ConnectionPoolManager.ReleaseConnection(conn, bq);
+                }
             }
             return null;
         }
@@ -158,33 +209,50 @@ namespace parkMonitor.Database2
             Parking_Space ps = new Parking_Space();
             MySqlDataReader reader = null;
             string sql = "select * from parkingspace where parkingSpaceID = '" + parkingSpaceID + " '";
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
-            object result = access.GetResult();
-            try
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
             {
-                reader = (MySqlDataReader)result;
-                while (reader.Read())
-                {
-                    if (reader.HasRows)
-                    {
-                        ps.parkingSpaceID = parkingSpaceID;
-                        ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
-                        ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
-                        ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
-                        return ps;
-                    }
-                }
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
             }
-            catch (Exception ex)
+            if (conn == null)
             {
-                Console.WriteLine(ex.Message + "查询车位表获取x,y,z异常");
+                Console.WriteLine("请检查网络!等待网络修复超时");
             }
-            finally
+            else
             {
-                reader.Close();
-                reader.Dispose();
-                ConnectionPoolManager.ReleaseConnection(conn, bq);
+                DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
+                object result = access.GetResult();
+                try
+                {
+                    reader = (MySqlDataReader)result;
+                    while (reader.Read())
+                    {
+                        if (reader.HasRows)
+                        {
+                            ps.parkingSpaceID = parkingSpaceID;
+                            ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
+                            ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
+                            ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
+                            return ps;
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.Message + "查询车位表获取x,y,z异常");
+                }
+                finally
+                {
+                    if (reader != null)
+                    {
+                        reader.Close();
+                        reader.Dispose();
+                    }
+                    ConnectionPoolManager.ReleaseConnection(conn, bq);
+                }
             }
             return null;
         }
@@ -198,28 +266,42 @@ namespace parkMonitor.Database2
         {
             bool isTelRegister = false;
             string sql = "select * from user where userTelephone = '" + tel + "'";
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
-            object result = access.GetResult();
-            try
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
             {
-                int count = (int)result;
-                if (count > 0)
-                {
-                    isTelRegister = true;
-                }
-                else
-                {
-                    isTelRegister = false;
-                }
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
             }
-            catch (Exception ex)
+            if (conn == null)
             {
-                Console.WriteLine(ex.Message + "检测电话号码注册异常");
+                Console.WriteLine("请检查网络!等待网络修复超时");
             }
-            finally
+            else
             {
-                ConnectionPoolManager.ReleaseConnection(conn, bq);
+                DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
+                object result = access.GetResult();
+                try
+                {
+                    int count = (int)result;
+                    if (count > 0)
+                    {
+                        isTelRegister = true;
+                    }
+                    else
+                    {
+                        isTelRegister = false;
+                    }
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.Message + "检测电话号码注册异常");
+                }
+                finally
+                {
+                    ConnectionPoolManager.ReleaseConnection(conn, bq);
+                }
             }
             return isTelRegister;
         }
@@ -234,36 +316,52 @@ namespace parkMonitor.Database2
             int parkingRecordsID = 0;
             MySqlDataReader reader = null;
             string sql = "select parkingRecordsID from parkingrecords where numberPlate = '" + numberPlate + "' and parkingRecordsState = 3";
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
-            object result = access.GetResult();
-            try
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
+            {
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
+            }
+            if (conn == null)
+            {
+                Console.WriteLine("请检查网络!等待网络修复超时");
+            }
+            else
             {
-                reader = (MySqlDataReader)result;
-                while (reader.Read())
+                DBAccess access = new DBAccess(sql, conn, CommandTypes.QUERYCOMMAND);
+                object result = access.GetResult();
+                try
                 {
-                    if (reader.HasRows)
+                    reader = (MySqlDataReader)result;
+                    while (reader.Read())
                     {
-                        parkingRecordsID = reader.GetInt32("parkingRecordsID");
-                        return parkingRecordsID;
+                        if (reader.HasRows)
+                        {
+                            parkingRecordsID = reader.GetInt32("parkingRecordsID");
+                        }
+                        else
+                        {
+                            Console.WriteLine("停车记录id查无结果");
+                        }
                     }
-                    else
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.Message + "查询停车记录id异常");
+                }
+                finally
+                {
+                    if (reader != null)
                     {
-                        Console.WriteLine("停车记录id查无结果");
+                        reader.Close();
+                        reader.Dispose();
                     }
+                    ConnectionPoolManager.ReleaseConnection(conn, bq);
                 }
             }
-            catch (Exception ex)
-            {
-                Console.WriteLine(ex.Message + "查询停车记录id异常");
-            }
-            finally
-            {
-                reader.Close();
-                reader.Dispose();
-                ConnectionPoolManager.ReleaseConnection(conn, bq);
-            }
-            return 0;
+            return parkingRecordsID;
         }
 
         /// <summary>
@@ -277,28 +375,42 @@ namespace parkMonitor.Database2
         {
             bool isNumberPlate = true;
             string sql = "select * from parkingrecords where numberPlate = '" + numberPlate + "' and parkingRecordsState = 3 and garageID = '" + garageID + "'";
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
-            object result = access.GetResult();
-            try
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
             {
-                int count = (int)result;
-                if (count > 0)
-                {
-                    isNumberPlate = true;
-                }
-                else
-                {
-                    isNumberPlate = false;
-                }
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
             }
-            catch (Exception ex)
+            if (conn == null)
             {
-                Console.WriteLine(ex.Message + "检测车库中车辆异常");
+                Console.WriteLine("请检查网络!等待网络修复超时");
             }
-            finally
+            else
             {
-                ConnectionPoolManager.ReleaseConnection(conn, bq);
+                DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
+                object result = access.GetResult();
+                try
+                {
+                    int count = (int)result;
+                    if (count > 0)
+                    {
+                        isNumberPlate = true;
+                    }
+                    else
+                    {
+                        isNumberPlate = false;
+                    }
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.Message + "检测车库中车辆异常");
+                }
+                finally
+                {
+                    ConnectionPoolManager.ReleaseConnection(conn, bq);
+                }
             }
             return isNumberPlate;
         }
@@ -312,28 +424,42 @@ namespace parkMonitor.Database2
         {
             bool isNumberPlateFromVehicle = true;
             string sql = "select * from vehicle where numberPlate = '" + numberPlate + "' and vehiclepParkState = 1";
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
-            object result = access.GetResult();
-            try
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
             {
-                int count = (int)result;
-                if (count > 0)
-                {
-                    isNumberPlateFromVehicle = true;
-                }
-                else
-                {
-                    isNumberPlateFromVehicle = false;
-                }
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
             }
-            catch (Exception ex)
+            if (conn == null)
             {
-                Console.WriteLine(ex.Message + "检测车辆表车辆号牌异常");
+                Console.WriteLine("请检查网络!等待网络修复超时");
             }
-            finally
+            else
             {
-                ConnectionPoolManager.ReleaseConnection(conn, bq);
+                DBAccess access = new DBAccess(sql, conn, CommandTypes.SCALARCOMMAND);
+                object result = access.GetResult();
+                try
+                {
+                    int count = (int)result;
+                    if (count > 0)
+                    {
+                        isNumberPlateFromVehicle = true;
+                    }
+                    else
+                    {
+                        isNumberPlateFromVehicle = false;
+                    }
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.Message + "检测车辆表车辆号牌异常");
+                }
+                finally
+                {
+                    ConnectionPoolManager.ReleaseConnection(conn, bq);
+                }
             }
             return isNumberPlateFromVehicle;
         }
@@ -346,10 +472,24 @@ namespace parkMonitor.Database2
         public void UpdateVehicleParkState(BlockingQueue bq, string numberPlate, int vehiclepParkState)
         {
             string sql = "update vehicle set vehiclepParkState = '" + vehiclepParkState + "'where numberPlate = '" + numberPlate + "'";
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            DBAccess access = new DBAccess(sql, conn, CommandTypes.NOQUERYCOMMAND);
-            object result = access.GetResult();
-            ConnectionPoolManager.ReleaseConnection(conn, bq);
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
+            {
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
+            }
+            if (conn == null)
+            {
+                Console.WriteLine("请检查网络!等待网络修复超时");
+            }
+            else
+            {
+                DBAccess access = new DBAccess(sql, conn, CommandTypes.NOQUERYCOMMAND);
+                object result = access.GetResult();
+                ConnectionPoolManager.ReleaseConnection(conn, bq);
+            }
         }
         /// <summary>
         /// 更新车位状态
@@ -360,11 +500,94 @@ namespace parkMonitor.Database2
         public void UpdateParkingSpaceState(BlockingQueue bq, int parkingSpaceID, int parkingSpaceState)
         {
             string sql = "update parkingspace set parkingSpaceState = '" + parkingSpaceState + "'where parkingSpaceID = '" + parkingSpaceID + "'";
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            DBAccess access = new DBAccess(sql, conn, CommandTypes.NOQUERYCOMMAND);
-            object result = access.GetResult(); 
-            ConnectionPoolManager.ReleaseConnection(conn, bq);
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
+            {
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
+            }
+            if (conn == null)
+            {
+                Console.WriteLine("请检查网络!等待网络修复超时");
+            }
+            else
+            {
+                DBAccess access = new DBAccess(sql, conn, CommandTypes.NOQUERYCOMMAND);
+                object result = access.GetResult();
+                ConnectionPoolManager.ReleaseConnection(conn, bq);
+            }
         }
+        /// <summary>
+        /// 插入停车记录表事务
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="strs"></param>
+        /// <returns></returns>
+        public static int InsertParkingRecords(BlockingQueue bq, List<string> strs)
+        {
+            int parkingRecordsID = 0;
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
+            {
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
+            }
+            if (conn == null)
+            {
+                Console.WriteLine("请检查网络!等待网络修复超时");
+            }
+            else
+            {
+                DBAccess access = new DBAccess(null, conn, CommandTypes.TRANSACTIONCOMMAND);
+                TransactionResult transactionResult = access.DealTransaction(strs);               
+                try
+                {
+                    parkingRecordsID = transactionResult.result;
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.Message + "停车记录ID获取失败");
+                }
+                finally
+                {
+                    ConnectionPoolManager.ReleaseConnection(conn, bq);
+                }
+            }
+            return parkingRecordsID;
+        }
+        /// <summary>
+        /// 更新数据库事务
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="strs"></param>
+        public static bool UpdateTransaction(BlockingQueue bq, List<string> strs)
+        {
+            bool flag = true;
+            Connection conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+            int i = 0;
+            while (conn == null && i < 2)
+            {
+                ConnectionPoolManager.CheckConnPooling(2, 10, ConnectionPoolManager.remoteConf, bq);
+                conn = ConnectionPoolManager.GetConnection(bq, TIMEOUT);
+                i++;
+            }
+            if (conn == null)
+            {
+                Console.WriteLine("请检查网络!等待网络修复超时");
+            }
+            else
+            {
+                DBAccess access = new DBAccess(null, conn, CommandTypes.TRANSACTIONCOMMAND);
+                TransactionResult transactionResult = access.DealTransaction(strs);
+                flag = transactionResult.flag;
+            }
+            return flag;
+        }
+
         ///// <summary>
         ///// 插入停车记录表事务
         ///// </summary>
@@ -374,8 +597,43 @@ namespace parkMonitor.Database2
         //public static int InsertParkingRecords(BlockingQueue bq, List<string> strs)
         //{
         //    Connection conn = ConnectionPoolManager.GetConnection(bq);
-        //    DBAccess access = new DBAccess(conn, CommandTypes.TRANSACTIONCOMMAND, strs);
-        //    object result = access.DealTransaction();
+        //    object result = null;
+        //    MySqlCommand myCommand = conn.mySqlConnection.CreateCommand();
+        //    MySqlTransaction myTrans;
+        //    myTrans = conn.mySqlConnection.BeginTransaction();
+        //    myCommand.Connection = conn.mySqlConnection;
+        //    myCommand.Transaction = myTrans;
+        //    try
+        //    {
+        //        for (int i = 0; i < strs.Count(); i++)
+        //        {
+        //            myCommand.CommandText = strs[i];
+        //            myCommand.ExecuteNonQuery();
+        //            result = Convert.ToInt32(myCommand.LastInsertedId);
+        //        }
+        //        myTrans.Commit();
+        //    }
+        //    catch (Exception e)
+        //    {
+        //        try
+        //        {
+        //            myTrans.Rollback();
+        //        }
+        //        catch (MySqlException ex)
+        //        {
+        //            if (myTrans.Connection != null)
+        //            {
+        //                Console.WriteLine(ex.Message + "事务回滚失败");
+        //            }
+        //        }
+        //        Console.WriteLine(e.Message + "事务操作失败");
+        //    }
+        //    finally
+        //    {
+        //        myCommand.Dispose();
+        //        myTrans.Dispose();
+        //        //ConnectionPoolManager.ReleaseConnection(conn);
+        //    }
         //    int parkingRecordsID = 0;
         //    try
         //    {
@@ -383,7 +641,7 @@ namespace parkMonitor.Database2
         //    }
         //    catch (Exception ex)
         //    {
-        //        Console.WriteLine(ex.Message+"停车记录ID获取失败");
+        //        Console.WriteLine(ex.Message + "停车记录ID获取失败");
         //    }
         //    finally
         //    {
@@ -396,120 +654,47 @@ namespace parkMonitor.Database2
         ///// </summary>
         ///// <param name="bq"></param>
         ///// <param name="strs"></param>
-        //public static void UpdateTransaction(BlockingQueue bq, List<string> strs)
+        //public static object UpdateTransaction(BlockingQueue bq, List<string> strs)
         //{
         //    Connection conn = ConnectionPoolManager.GetConnection(bq);
-        //    DBAccess access = new DBAccess(conn, CommandTypes.TRANSACTIONCOMMAND, strs);
-        //    object result = access.DealTransaction();
+        //    object result = false;
+        //    MySqlCommand myCommand = conn.mySqlConnection.CreateCommand();
+        //    MySqlTransaction myTrans;
+        //    myTrans = conn.mySqlConnection.BeginTransaction();
+        //    myCommand.Connection = conn.mySqlConnection;
+        //    myCommand.Transaction = myTrans;
+        //    try
+        //    {
+        //        for (int i = 0; i < strs.Count(); i++)
+        //        {
+        //            myCommand.CommandText = strs[i];
+        //            myCommand.ExecuteNonQuery();
+        //            result = true;
+        //        }
+        //        myTrans.Commit();
+        //    }
+        //    catch (Exception e)
+        //    {
+        //        try
+        //        {
+        //            myTrans.Rollback();
+        //        }
+        //        catch (MySqlException ex)
+        //        {
+        //            if (myTrans.Connection != null)
+        //            {
+        //                Console.WriteLine(ex.Message + "事务回滚失败");
+        //            }
+        //        }
+        //        Console.WriteLine(e.Message + "事务操作失败");
+        //    }
+        //    finally
+        //    {
+        //        myCommand.Dispose();
+        //        myTrans.Dispose();
+        //        ConnectionPoolManager.ReleaseConnection(conn,bq);
+        //    }
+        //    return result;
         //}
-
-        /// <summary>
-        /// 插入停车记录表事务
-        /// </summary>
-        /// <param name="bq"></param>
-        /// <param name="strs"></param>
-        /// <returns></returns>
-        public static int InsertParkingRecords(BlockingQueue bq, List<string> strs)
-        {
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            object result = null;
-            MySqlCommand myCommand = conn.mySqlConnection.CreateCommand();
-            MySqlTransaction myTrans;
-            myTrans = conn.mySqlConnection.BeginTransaction();
-            myCommand.Connection = conn.mySqlConnection;
-            myCommand.Transaction = myTrans;
-            try
-            {
-                for (int i = 0; i < strs.Count(); i++)
-                {
-                    myCommand.CommandText = strs[i];
-                    myCommand.ExecuteNonQuery();
-                    result = Convert.ToInt32(myCommand.LastInsertedId);
-                }
-                myTrans.Commit();
-            }
-            catch (Exception e)
-            {
-                try
-                {
-                    myTrans.Rollback();
-                }
-                catch (MySqlException ex)
-                {
-                    if (myTrans.Connection != null)
-                    {
-                        Console.WriteLine(ex.Message + "事务回滚失败");
-                    }
-                }
-                Console.WriteLine(e.Message + "事务操作失败");
-            }
-            finally
-            {
-                myCommand.Dispose();
-                myTrans.Dispose();
-                //ConnectionPoolManager.ReleaseConnection(conn);
-            }
-            int parkingRecordsID = 0;
-            try
-            {
-                parkingRecordsID = (int)result;
-            }
-            catch (Exception ex)
-            {
-                Console.WriteLine(ex.Message + "停车记录ID获取失败");
-            }
-            finally
-            {
-                ConnectionPoolManager.ReleaseConnection(conn, bq);
-            }
-            return parkingRecordsID;
-        }
-        /// <summary>
-        /// 更新数据库事务
-        /// </summary>
-        /// <param name="bq"></param>
-        /// <param name="strs"></param>
-        public static object UpdateTransaction(BlockingQueue bq, List<string> strs)
-        {
-            Connection conn = ConnectionPoolManager.GetConnection(bq);
-            object result = false;
-            MySqlCommand myCommand = conn.mySqlConnection.CreateCommand();
-            MySqlTransaction myTrans;
-            myTrans = conn.mySqlConnection.BeginTransaction();
-            myCommand.Connection = conn.mySqlConnection;
-            myCommand.Transaction = myTrans;
-            try
-            {
-                for (int i = 0; i < strs.Count(); i++)
-                {
-                    myCommand.CommandText = strs[i];
-                    myCommand.ExecuteNonQuery();
-                    result = true;
-                }
-                myTrans.Commit();
-            }
-            catch (Exception e)
-            {
-                try
-                {
-                    myTrans.Rollback();
-                }
-                catch (MySqlException ex)
-                {
-                    if (myTrans.Connection != null)
-                    {
-                        Console.WriteLine(ex.Message + "事务回滚失败");
-                    }
-                }
-                Console.WriteLine(e.Message + "事务操作失败");
-            }
-            finally
-            {
-                myCommand.Dispose();
-                myTrans.Dispose();
-                ConnectionPoolManager.ReleaseConnection(conn,bq);
-            }
-            return result;
-        }
     }
 }

+ 1 - 1
parkMonitor/entity/EntityForCore.cs

@@ -70,7 +70,7 @@ namespace parkMonitor.entity
             //数据库初始化
             ConnectionPoolManager.Init();
             remoteBQ = ConnectionPoolManager.InitConnPooling(10, ConnectionPoolManager.remoteConf);
-            localBQ = ConnectionPoolManager.InitConnPooling(10, ConnectionPoolManager.localConf);
+            //localBQ = ConnectionPoolManager.InitConnPooling(10, ConnectionPoolManager.localConf);
             //车位管理初始化
             Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "初始化车位");
             ParkingSpaceManager.ins = new ParkingSpaceManager();

+ 17 - 7
parkMonitor/server/CoreThread/AbstractCmd.cs

@@ -809,6 +809,11 @@ namespace parkMonitor.server.CoreThread
                             List<string> strs = new List<string>();
                             strs.Add(insertRecordSql);
                             parkingRecordsID = DBOperation.InsertParkingRecords(EntityForCore.remoteBQ, strs);
+                            if (parkingRecordsID == 0)
+                            {
+                                //数据库操作失败写日志
+                                Log.WriteLog(LogType.DATABASE, "1", insertRecordSql);
+                            }
                         }
                         catch (Exception ex)
                         {
@@ -829,7 +834,14 @@ namespace parkMonitor.server.CoreThread
                             strs.Add(updateParkingSpaceStateSql);
                             strs.Add(updateFreeSpaceSql);
                             strs.Add(updateVehicleSql);
-                            DBOperation.UpdateTransaction(EntityForCore.remoteBQ, strs);
+                            bool result = DBOperation.UpdateTransaction(EntityForCore.remoteBQ, strs);
+                            if (!result)
+                            {
+                                //数据库操作失败写日志
+                                Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
+                                Log.WriteLog(LogType.DATABASE, "0", updateFreeSpaceSql);
+                                Log.WriteLog(LogType.DATABASE, "0", updateVehicleSql);
+                            }
                         }
                         catch (Exception ex)
                         {
@@ -874,8 +886,8 @@ namespace parkMonitor.server.CoreThread
                             strs.Add(updateParkingSpaceStateSql);
                             strs.Add(updateFreeSpaceSql);
                             strs.Add(updateVehicleSql);
-                            object result = DBOperation.UpdateTransaction(EntityForCore.localBQ, strs);
-                            if (!(bool)result)
+                            bool result = DBOperation.UpdateTransaction(EntityForCore.localBQ, strs);
+                            if (!result)
                             {
                                 //数据库操作失败写日志
                                 Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
@@ -977,7 +989,6 @@ namespace parkMonitor.server.CoreThread
         {
             //UILogServer.ins.info("取车流程:" + queueCmd.LicenseNum + "开始");
             Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "取车流程:" + queueCmd.LicenseNum + "开始");
-            string connectionStr = null;            //数据库连接字符串
             Vehicle vehiclelist = null;                 //待取车辆信息
             ControlMessage cm = null;
             Parking_Space ps = null;                 //车位信息
@@ -1118,8 +1129,8 @@ namespace parkMonitor.server.CoreThread
                             strs.Add(updateFreeSpaceSql);
                             strs.Add(updateVehicleStateSql);
                             strs.Add(updateParkingRecordsSql);
-                            object result = DBOperation.UpdateTransaction(EntityForCore.remoteBQ, strs);
-                            if (!(bool)result)
+                            bool result = DBOperation.UpdateTransaction(EntityForCore.remoteBQ, strs);
+                            if (!result)
                             {
                                 //写日志记录sql,以待之后处理
                                 Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
@@ -1195,7 +1206,6 @@ namespace parkMonitor.server.CoreThread
     /// </summary>
     public class SimpleCMDFactory
     {
-        //DBOperation oper = new DBOperation();
         public AbstractCmd createCmd(Command queueCmd)
         {
             AbstractCmd abstractCmd = null;

+ 4 - 5
parkMonitor/server/CoreThread/CoreThreadTest2.cs

@@ -110,9 +110,8 @@ namespace parkMonitor.server.CoreThread
         public void BeginWorking()
         {
             Timer checkRemoteTimer = new System.Threading.Timer(CheckRemotePool, null, 10000, 300000);
-            Timer checkLocalTimer = new System.Threading.Timer(CheckLocalPool, null, 10000, 300000);
-            //Timer updateTimer = new System.Threading.Timer(updateGarageAndParkingSpace, null, 1000, 3000);
-            Timer logTimer = new System.Threading.Timer(displayLog, null, 1000, 30000);
+            //Timer checkLocalTimer = new System.Threading.Timer(CheckLocalPool, null, 10000, 300000);
+            //Timer logTimer = new System.Threading.Timer(displayLog, null, 1000, 30000);
             Object lockObj = new object();
             Command queueCmdRecord = null;
             while (!isClosing)
@@ -266,12 +265,12 @@ namespace parkMonitor.server.CoreThread
                     }
                     if (sqlStatus == "0")
                     {
-                        object result;
+                        bool result;
                         try
                         {
                             //Operation.MyTransaction(EntityForCore.remoteBQ,sqlMsg, out temp); 
                             result = DBOperation.UpdateTransaction(EntityForCore.remoteBQ, strs);
-                            if (!(bool)result)
+                            if (!result)
                             {
                                 Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
                             }

+ 58 - 10
parkMonitor/tools/BlockingQueue.cs

@@ -12,23 +12,71 @@ namespace parkMonitor.tools
         private BlockingCollection<object> bQueue = new BlockingCollection<object>(new ConcurrentQueue<object>());
         public void Enqueue(object message)
         {
-            bQueue.Add(message);
+            try
+            {
+                bQueue.Add(message);
+            }
+            catch (ObjectDisposedException ex)
+            {
+                Console.WriteLine(ex.Message+"对象已被释放");
+            }
+            catch (InvalidOperationException ex)
+            {
+                Console.WriteLine(ex.Message+"已标记为已完成添加操作或队列未接收该项");
+            }
         }
         public object Dequeue()
         {
-            //if(count() != 0)
-            //{
-                return bQueue.Take();
-            //}
-            //else
-            //{
-            //    return null;
-            //} 
+            object obj = null;
+            try
+            {
+                obj = bQueue.Take();
+            }
+            catch (ObjectDisposedException ex)
+            {
+                Console.WriteLine(ex.Message + "对象已被释放");
+            }
+            catch (InvalidOperationException ex)
+            {
+                Console.WriteLine(ex.Message + "队列为空,无法获取");
+            }
+            return obj;
+        }
+
+        public bool Dequeue(out object item, int millisecondsTimeout)
+        {
+            bool flag = false;
+            try
+            {
+                flag = bQueue.TryTake(out item, millisecondsTimeout);
+            }
+            catch (ObjectDisposedException ex)
+            {
+                Console.WriteLine(ex.Message + "对象已被释放,连接池为空");
+                item = null;
+                flag = false;
+            }
+            catch (ArgumentOutOfRangeException ex)
+            {
+                Console.WriteLine(ex.Message + "millisecondsTimeout为-1表无期限超时,不能为非-1的负数");
+                item = null;
+                flag = false;
+            }
+            return flag;
         }
 
         public int Count()
         {
-            return bQueue.Count();
+            int counter = 0;
+            try
+            {
+                counter = bQueue.Count();
+            }
+            catch (ObjectDisposedException ex)
+            {
+                Console.WriteLine(ex.Message+"对象已被释放");
+            }
+            return counter;
         }
     }
 }