浏览代码

Merge remote-tracking branch 'jinwang/dev' into dev

yc_t 7 年之前
父节点
当前提交
c6d95a628b

+ 18 - 18
parkMonitor/DataBase/DBConnection.cs

@@ -36,24 +36,24 @@ namespace parkMonitor.DataBase
         /// </summary>
         /// <param name="connectionStr"></param>
         /// <returns></returns>
-        public MySqlConnection getConn(string connectionStr)
-        {
-            MySqlConnection con = null;
-            try
-            {
-                if (connectionStr == localStr)
-                {
-                    con = new MySqlConnection(localConf);
-                }
-                else if (connectionStr == remoteStr)
-                {
-                    con = new MySqlConnection(remoteConf);
-                }
-                return con;
-            }
-            catch { }
-            return null;
-        }
+        //public MySqlConnection getConn(string connectionStr)
+        //{
+        //    MySqlConnection con = null;
+        //    try
+        //    {
+        //        if (connectionStr == localStr)
+        //        {
+        //            con = new MySqlConnection(localConf);
+        //        }
+        //        else if (connectionStr == remoteStr)
+        //        {
+        //            con = new MySqlConnection(remoteConf);
+        //        }
+        //        return con;
+        //    }
+        //    catch { }
+        //    return null;
+        //}
         /// <summary>
         /// 预处理句柄
         /// </summary>

+ 145 - 0
parkMonitor/DataBase/DBConnectionPool.cs

@@ -0,0 +1,145 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MySql.Data.MySqlClient;
+using System.Collections;
+
+namespace parkMonitor.DataBase
+{
+    /// <summary>
+    /// 数据库连接池
+    /// </summary>
+    class DBConnectionPool
+    {
+        private static DBConnectionPool connectionPool = null;//池管理对象
+        private static Object objlock = typeof(DBConnectionPool);//池管理对象实例
+        private int size = 1;//池中连接数
+        private int useCount = 0;//已经使用连接数
+        private ArrayList pool = null;//连接保存的集合
+        private static string connectionStr = "";//连接字符串
+
+        public DBConnectionPool(string connectionString)
+        {
+            connectionStr = connectionString;
+            pool = new ArrayList();
+        }
+        /// <summary>
+        /// 创建获取池对象
+        /// </summary>
+        /// <returns></returns>
+        public static DBConnectionPool getPool(string connectionString)
+        {
+            lock (objlock)
+            {
+                if (connectionPool == null)
+                {
+                    connectionPool = new DBConnectionPool(connectionString);
+                }
+                return connectionPool;
+            }
+        }
+        /// <summary>
+        /// 获取池连接
+        /// </summary>
+        /// <returns></returns>
+        public MySqlConnection getConnection()
+        {
+            lock (pool)
+            {
+                MySqlConnection conn = null;
+                if (pool.Count > 0)
+                {
+                    //取第一个可用连接
+                    conn = (MySqlConnection)pool[0];
+                    //可用连接中移除
+                    pool.RemoveAt(0);
+                    //创建不成功
+                    if (!isUseful(conn))
+                    {
+                        useCount--;
+                        conn = getConnection();
+                    }
+                }
+                else
+                {
+                    //可使用连接小于连接数
+                    if (useCount <= size)
+                    {
+                        try
+                        {
+                            conn = CreateConnection(conn);
+                        }
+                        catch (Exception)
+                        {
+                        }
+                    }
+                }
+                if (conn == null)
+                {
+                    //达到最大连接数递归调用获取连接否则创建新连接
+                    if (useCount <= size)
+                    {
+                        conn = getConnection();
+                    }
+                    else
+                    {
+                        conn = CreateConnection(conn);
+                    }
+                }
+                return conn;
+            }
+        }
+        /// <summary>
+        /// 创建连接
+        /// </summary>
+        /// <param name="temp"></param>
+        /// <returns></returns>
+        private MySqlConnection CreateConnection(MySqlConnection temp)
+        {
+            MySqlConnection conn = new MySqlConnection(connectionStr);
+            conn.Open();
+            useCount++;
+            temp = conn;
+            return temp;
+        }
+        /// <summary>
+        /// 连接是否创建成功可用
+        /// </summary>
+        /// <param name="conn"></param>
+        /// <returns></returns>
+        private bool isUseful(MySqlConnection conn)
+        {
+            bool result = true;
+            if (conn != null)
+            {
+                string sql = "select garageFreeSpace from garage where garageID = '1'";
+                MySqlCommand cmd = new MySqlCommand(sql, conn);
+                try
+                {
+                    cmd.ExecuteScalar().ToString();
+                }
+                catch
+                {
+                    result = false;
+                }
+            }
+            return result;
+        }
+        /// <summary>
+        /// 释放连接,回到连接池
+        /// </summary>
+        /// <param name="conn"></param>
+        public void ConnectionClose(MySqlConnection conn)
+        {
+            lock (pool)
+            {
+                if (conn != null)
+                {
+                    pool.Add(conn);
+                }
+            }
+        }
+    }
+}

+ 11 - 6
parkMonitor/DataBase/IDBOperation.cs

@@ -33,7 +33,7 @@ namespace parkMonitor.DataBase
         public Operation(string connectionStr, string sql)
         {
             connection = new DBConnection();
-            con = connection.getConn(connectionStr);
+            con = DBConnectionPool.getPool(connectionStr).getConnection();
             cmd = connection.getComm(sql, con);
         }
 
@@ -165,8 +165,9 @@ namespace parkMonitor.DataBase
                 }
                 transaction.Commit();
                 cmd.Dispose();
-                conn.Close();
-                conn.Dispose();
+                //conn.Close();
+                //conn.Dispose();
+                DBConnectionPool.getPool(DBConnection.remoteConf).ConnectionClose(conn);
                 return true;
             }
             catch
@@ -178,7 +179,12 @@ namespace parkMonitor.DataBase
                 catch { return false; }
                 UILogServer.ins.error("数据库操作失败,事务回滚");
                 if (cmd != null) { cmd.Dispose(); }
-                if (conn != null) { conn.Close();conn.Dispose(); }
+                if (conn != null)
+                {
+                    //conn.Close(); 
+                    //conn.Dispose();
+                    DBConnectionPool.getPool(DBConnection.remoteConf).ConnectionClose(conn);
+                }
             }
             return false;
         }
@@ -267,8 +273,7 @@ namespace parkMonitor.DataBase
             }
             if (conn != null)
             {
-                conn.Close();
-                conn.Dispose();
+                DBConnectionPool.getPool(DBConnection.remoteConf).ConnectionClose(conn);
             }
         }
 

+ 53 - 52
parkMonitor/bll/MainBll.cs

@@ -11,6 +11,8 @@ using MySql.Data.MySqlClient;
 using parkMonitor.DataBase;
 using System.Threading;
 using parkMonitor.LOG;
+using System.Threading;
+using parkMonitor.server.CoreThread;
 
 namespace parkMonitor.bll
 {
@@ -42,63 +44,62 @@ namespace parkMonitor.bll
                 });
             }
 
-            //日志文件执行数据库操作
-            Task.Factory.StartNew(() =>
-            {
-                while (!EntityForCore.ins.globalStatus)
-                {
-                    Thread.Sleep(1000);
-                }
-                lock (Parking_Space.spaceLock)
-                {
-                    string sqlStatus;
-                    string sqlMsg;
-                    int count;
-                    Log.ReadLog(out sqlStatus, out sqlMsg, out count);
-                    if (count-- > 0)
-                    {
-                        if (sqlStatus == "1")
-                        {
-                            int parkingRecordsID = 0;
-                            try
-                            {
-                                using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
-                                {
-                                    Operation.TryOpen(conn);
-                                    Operation.MyTransaction(conn, sqlMsg, out parkingRecordsID);
-                                }
-                            }
-                            catch
-                            {
-                                throw;
-                            }
-                        }
-                        if (sqlStatus == "0")
-                        {
-                            int temp;
-                            try
-                            {
-                                using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
-                                {
-                                    Operation.TryOpen(conn);
-                                    Operation.MyTransaction(conn, sqlMsg, out temp);
-                                }
-                            }
-                            catch
-                            {
-                                throw;
-                            }
-                        }
-                    }
-                }
-            });
-
+            ////日志文件执行数据库操作
+            //Task.Factory.StartNew(() =>
+            //{
+            //    while (!EntityForCore.ins.globalStatus)
+            //    {
+            //        Thread.Sleep(1000);
+            //    }
+            //    lock (Parking_Space.spaceLock)
+            //    {
+            //        string sqlStatus;
+            //        string sqlMsg;
+            //        int count;
+            //        Log.ReadLog(out sqlStatus, out sqlMsg, out count);
+            //        if (count-- > 0)
+            //        {
+            //            if (sqlStatus == "1")
+            //            {
+            //                int parkingRecordsID = 0;
+            //                try
+            //                {
+            //                    using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+            //                    {
+            //                        Operation.TryOpen(conn);
+            //                        Operation.MyTransaction(conn, sqlMsg, out parkingRecordsID);
+            //                    }
+            //                }
+            //                catch
+            //                {
+            //                    throw;
+            //                }
+            //            }
+            //            if (sqlStatus == "0")
+            //            {
+            //                int temp;
+            //                try
+            //                {
+            //                    using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+            //                    {
+            //                        Operation.TryOpen(conn);
+            //                        Operation.MyTransaction(conn, sqlMsg, out temp);
+            //                    }
+            //                }
+            //                catch
+            //                {
+            //                    throw;
+            //                }
+            //            }
+            //        }
+            //    }
+            //});
             //主窗口关闭事件
             CmdServer.ins.listen(CmdType.MainWinClosed, () =>
             {
                 coreThread.Stop();
                 monitorThread.Stop();
             });
-        }
+        }      
     }
 }

+ 1 - 0
parkMonitor/parkMonitor.csproj

@@ -100,6 +100,7 @@
       <DependentUpon>InitWin.cs</DependentUpon>
     </Compile>
     <Compile Include="DataBase\DBConnection.cs" />
+    <Compile Include="DataBase\DBConnectionPool.cs" />
     <Compile Include="DataBase\DBOperation.cs" />
     <Compile Include="DataBase\IDBOperation.cs" />
     <Compile Include="DataBase\Vehicle.cs" />

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

@@ -739,56 +739,8 @@ namespace parkMonitor.server.CoreThread
 
                 //车位赋值与写数据库
                 if (!queueCmd.manual)
-                {
-                    //connectionStr = "SqlConnectionStr";
-                    //更新云端车位表车位状态
-                    //oper.UpdateParkingSpaceState(connectionStr, parkingSpaceID, 1);
-                    //更新车库表剩余车位数
-                    //int freeSpaceCount = oper.getGarageFreeSpace(connectionStr, garageID);
-                    //freeSpaceCount = freeSpaceCount - 1;
-                    //oper.UpdateGarageFreeSpace(connectionStr, freeSpaceCount, garageID);
-
-                    try
-                    {
-                        using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
-                        {
-                            Operation.TryOpen(conn);
-                            bool DBStoreStatus = false;                       //判断数据库事务操作是否正常
-                            int temp;
-                            string updateParkingSpaceSql = "update parkingspace set parkingSpaceState = 1 where parkingSpaceID = '" + ppp.parkingSpaceID + "'";
-                            string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpaceCount + "' where garageID = '" + garageID + "'";
-                            List<string> strs = new List<string>();
-                            strs.Add(updateParkingSpaceSql);
-                            strs.Add(updateFreeSpaceSql);
-                            if (!Operation.MyTransaction(conn, strs, out temp))
-                            {
-                                //此处为关键步骤,不记入日志,设置全局状态异常
-                                EntityForCore.ins.globalStatus = false;
-                                int count = 0;
-                                while (!MyTimer.restart)
-                                {
-                                    count++;
-                                    if (count == 1)
-                                    {
-                                        UILogServer.ins.error("数据库操作异常");
-                                        Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "缓冲位分配失败,流程已回滚");
-                                    }
-                                    Thread.Sleep(5000);
-                                }
-                                Rollback(queueCmd, ppp.parkingSpaceID, false, true, lmToBeReleased);
-                                ////数据库操作失败写日志
-                                //Log.WriteLog(LogType.DATABASE, "0",updateParkingSpaceSql);
-                                //Log.WriteLog(LogType.DATABASE, "0",updateFreeSpaceSql);
-                                ////Log.WriteLog(LogType.DATABASE, "0",strs.ToArray().ToString());
-                            }
-                        }
-                    }
-                    catch
-                    {
-                        throw;          //数据库异常待处理
-                    }
+                {                  
                 }
-
                 else
                 {
                     connectionStr = DBConnection.localStr;
@@ -916,7 +868,8 @@ namespace parkMonitor.server.CoreThread
                         int parkingRecordsID = 0;
                         try
                         {
-                            using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                            using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
+                            //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
                             {
                                 Operation.TryOpen(conn);
                                 string insertRecordSql = "insert into parkingrecords(userID,numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime) values('" + userID + "','" + queueCmd.LicenseNum + "','" + ppp.parkingSpaceID + "','" + garageID + "',3,'" + realParkTime + "')";
@@ -926,7 +879,8 @@ namespace parkMonitor.server.CoreThread
                                     Log.WriteLog(LogType.DATABASE, "1", insertRecordSql);
                                 }
                             }
-                            using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                            using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
+                            //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
                             {
                                 Operation.TryOpen(conn);
                                 string updateVehicleSql = "update vehicle set vehiclepParkState = 1,scanEntryTime = '" + queueCmd.TimeRecord + "',parkingRecordsID = '" + parkingRecordsID + "',parkingSpaceID = '" + ppp.parkingSpaceID + "',vehicleTypeConfirm = 1,frontwheelbase = '" + frontWheelbase + "',rearwheelbase = '" + rearWheelbase + "' where numberPlate = '" + queueCmd.LicenseNum + "'";
@@ -1193,7 +1147,8 @@ namespace parkMonitor.server.CoreThread
                         //取车事务
                         try
                         {
-                            using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                            using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
+                            //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
                             {
                                 Operation.TryOpen(conn);
                                 bool DBStoreStatus = false;       //判断数据库事务操作是否正常 

+ 113 - 1
parkMonitor/server/CoreThread/CoreThreadTest2.cs

@@ -12,6 +12,8 @@ using parkMonitor.server.uiLogServer;
 using parkMonitor.tools;
 using parkMonitor.DataBase;
 using MySql.Data.MySqlClient;
+using parkMonitor.server.CoreThread;
+using System.Threading;
 
 namespace parkMonitor.server.CoreThread
 {
@@ -99,9 +101,13 @@ namespace parkMonitor.server.CoreThread
             AbstractCmd.isClosing = true;//停止运行中指令
             this.isClosing = true;
         }
-
+        /// <summary>
+        /// 核心线程工作
+        /// </summary>
         public void BeginWorking()
         {
+            Timer updateTimer = new System.Threading.Timer(updateGarageAndParkingSpace, null, 1000, 3000);
+            Timer logTimer = new System.Threading.Timer(displayLog, null, 1000, 1800000);
             Object lockObj = new object();
             Command queueCmdRecord = null;
             while (!isClosing)
@@ -260,5 +266,111 @@ namespace parkMonitor.server.CoreThread
         //    }
         //    return msg;
         //}
+        /// <summary>
+        /// 定时执行车库、车位更新
+        /// </summary>
+        private void updateGarageAndParkingSpace(object o)
+        {
+            int freeSpaceCount = ParkingSpaceManager.ins.GetFreeSpaceCount();
+            try
+            {
+                using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
+                //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                {
+
+                    Operation.TryOpen(conn);
+                    bool DBStoreStatus = false;                       //判断数据库事务操作是否正常
+                    int temp;
+                    List<string> strs = new List<string>();
+                    string updateParkingSpaceSql = "";
+                    Dictionary<int, Parking_Space>.Enumerator enumer = ParkingSpaceManager.ins.parkingSpaceStatusMap.GetEnumerator();
+                    while (enumer.MoveNext())
+                    {
+                        updateParkingSpaceSql = "update parkingspace set parkingSpaceState = " + enumer .Current.Value.parkingSpaceState+ " where parkingSpaceID = '" + enumer .Current.Value.parkingSpaceID+ "'";
+                        strs.Add(updateParkingSpaceSql);
+                    }
+                    string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpaceCount + "' where garageID = '" + ParkingSpaceManager.garageID + "'";
+                    strs.Add(updateFreeSpaceSql);
+                    if (!Operation.MyTransaction(conn, strs, out temp))
+                    {
+                        //此处为关键步骤,不记入日志,设置全局状态异常
+                        EntityForCore.ins.globalStatus = false;
+                        int count = 0;
+                        while (!MyTimer.restart)
+                        {
+                            count++;
+                            if (count == 1)
+                            {
+                                UILogServer.ins.error("数据库操作异常");
+                                Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "缓冲位分配失败,流程已回滚");
+                            }
+                            Thread.Sleep(5000);
+                        }
+                    }
+                }
+            }
+            catch
+            {
+                throw;          //数据库异常待处理
+            }
+        }
+        /// <summary>
+        /// 执行日志文件数据库操作
+        /// </summary>
+        /// <param name="o"></param>
+        private void displayLog(object o)
+        {
+            while (!EntityForCore.ins.globalStatus)
+            {
+                Thread.Sleep(1000);
+            }
+            lock (Parking_Space.spaceLock)
+            {
+                string sqlStatus;
+                string sqlMsg;
+                int count;
+                Log.ReadLog(out sqlStatus, out sqlMsg, out count);
+                if (count > 0)
+                {
+                    if (sqlStatus == "1")
+                    {
+                        int parkingRecordsID = 0;
+                        try
+                        {
+                            using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
+                            //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                            {
+                                Operation.TryOpen(conn);
+                                Operation.MyTransaction(conn, sqlMsg, out parkingRecordsID);
+                            }
+                        }
+                        catch
+                        {
+                            Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
+                            throw;
+                        }
+                    }
+                    if (sqlStatus == "0")
+                    {
+                        int temp;
+                        try
+                        {
+                            using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
+                            //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                            {
+                                Operation.TryOpen(conn);
+                                Operation.MyTransaction(conn, sqlMsg, out temp);
+                            }
+                        }
+                        catch
+                        {
+                            Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
+                            throw;
+                        }
+                    }
+                    Log.ReadLog(out sqlStatus, out sqlMsg, out count);
+                }
+            }
+        }
     }
 }

+ 1 - 1
parkMonitor/server/NumMachine/ParkingSimul.cs

@@ -17,7 +17,7 @@ namespace parkMonitor.server.NumMachine
     class ParkingSimul
     {
         public static ParkingSimul ins;
-        private const string hostname = "127.0.0.1";
+        private const string hostname = "192.168.111.84";
         private const int port = 9000;
         private const string userId = "18202736439";
         private const string garageId = "1";