kingwang1995 6 tahun lalu
induk
melakukan
71938ae0e7

TEMPAT SAMPAH
parkMonitor/DataBase.rar


+ 179 - 0
parkMonitor/Database2/AbstractCommand.cs

@@ -0,0 +1,179 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Data;
+using MySql.Data.MySqlClient;
+
+namespace parkMonitor.Database2
+{
+    abstract class AbstractCommand
+    {
+        public string sql { set; get; }
+
+        public Connection conn { set; get; }
+
+        public AbstractCommand()
+        {
+            sql = null;
+            conn = null;
+        }
+
+        public AbstractCommand(string sql, Connection conn)
+        {
+            this.sql = sql;
+            this.conn = conn;
+        }
+
+        protected MySqlCommand CreateCommand()
+        {
+            MySqlCommand cmd = new MySqlCommand(sql, conn.mySqlConnection);
+
+            return cmd;
+        }
+        protected MySqlCommand CreateCommand(string sql, MySqlConnection mySqlConnection)
+        {
+            MySqlCommand cmd = new MySqlCommand(sql, conn.mySqlConnection);
+            return cmd;
+        }
+        /// <summary>
+        /// 抽象执行方法
+        /// </summary>
+        /// <returns></returns>
+        public abstract object ExecuteSqlCommand();
+
+    }
+
+    /// <summary>
+    /// 查询
+    /// </summary>
+    class QueryCommand : AbstractCommand
+    {
+        public override object ExecuteSqlCommand()
+        {
+            object result = null;
+            MySqlCommand cmd = CreateCommand(sql, conn.mySqlConnection);
+            result = cmd.ExecuteReader();
+            return result;
+        }
+    }
+
+    /// <summary>
+    /// 插入更新
+    /// </summary>
+    class NoQueryCommand : AbstractCommand
+    {
+        public override object ExecuteSqlCommand()
+        {
+            object result = null;
+            MySqlCommand cmd = CreateCommand(sql, conn.mySqlConnection);
+            result = cmd.ExecuteNonQuery();
+            return result;
+
+        }
+    }
+    /// <summary>
+    /// 统计个数
+    /// </summary>
+    class ScalarCommand : AbstractCommand
+    {
+        public override object ExecuteSqlCommand()
+        {
+            object result = null;
+            MySqlCommand cmd = CreateCommand(sql, conn.mySqlConnection);
+            result = cmd.ExecuteScalar();
+            return result;
+        }
+    }
+    /// <summary>
+    /// 存储过程执行
+    /// </summary>
+    class StoreProcedureCommand : AbstractCommand
+    {
+        public override object ExecuteSqlCommand()
+        {
+            object result = null;
+
+            MySqlCommand cmd = CreateCommand(sql, conn.mySqlConnection);
+            cmd.CommandType = CommandType.StoredProcedure;
+            //cmd.Parameters.AddWithValue();
+            result = cmd.ExecuteReader();
+            return result;
+        }
+    }
+
+    /// <summary>
+    /// 事务操作
+    /// </summary>
+    class TransactionCommand : AbstractCommand
+    {
+        public List<string> strs { set; get; }
+        public TransactionCommand()
+        {
+            sql = null;
+            conn = null;
+            strs = null;
+        }
+        public TransactionCommand(string sql, Connection conn, List<string> 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;
+            try
+            {
+                for (int i = 0; i < strs.Count(); i++)
+                {
+                    myCommand.CommandText = strs[i];
+                    myCommand.ExecuteNonQuery();
+                    result = Convert.ToInt32(myCommand.LastInsertedId);
+                }
+                myTrans.Commit();
+                //result = 1;
+                Console.WriteLine("Both records are written to database.");
+            }
+            catch (Exception e)
+            {
+                try
+                {
+                    myTrans.Rollback();
+                    //result = 2;
+                }
+                catch (MySqlException ex)
+                {
+                    if (myTrans.Connection != null)
+                    {
+                        Console.WriteLine("An exception of type " + ex.GetType() +
+                        " was encountered while attempting to roll back the transaction.");
+                        Console.WriteLine("事务回滚失败");
+                    }
+                    //result = 3;
+                }
+
+                Console.WriteLine("An exception of type " + e.GetType() +
+                " was encountered while inserting the data.");
+                Console.WriteLine("Neither record was written to database.");
+                Console.WriteLine("事务操作失败");
+            }
+            finally
+            {
+                myCommand.Dispose();
+                myTrans.Dispose();
+                //ConnectionPoolManager.ReleaseConnection(conn);
+            }
+            return result;
+        }
+    }
+    /// <summary>
+    /// 枚举类型
+    /// </summary>
+    enum CommandTypes { QUERYCOMMAND, NOQUERYCOMMAND, SCALARCOMMAND, TRANSACTIONCOMMAND, STOREPROCEDURECOMMAND };
+}

+ 157 - 0
parkMonitor/Database2/ConnectionPoolManager.cs

@@ -0,0 +1,157 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Collections.Concurrent;
+using parkMonitor.tools;
+using MySql.Data.MySqlClient;
+using System.Configuration;
+
+namespace parkMonitor.Database2
+{
+    class ConnectionPoolManager
+    {
+        //public static BlockingQueue bq = new BlockingQueue();
+        /// <summary>
+        /// 连接字符串
+        /// </summary>
+        public static string localStr = "SqlConnectionLocation";
+        public static string remoteStr = "SqlConnectionStr";
+        public static string localConf, remoteConf;
+        public static string localIP, remoteIP;
+        /// <summary>
+        /// 数据库连接字符串配置文件初始化
+        /// </summary>
+        public static void Init()
+        {
+            try
+            {
+                localConf = ConfigurationManager.AppSettings[localStr];
+                remoteConf = ConfigurationManager.AppSettings[remoteStr];
+                localIP = ConfigurationManager.AppSettings["localDBIP"];
+                remoteIP = ConfigurationManager.AppSettings["remoteDBIP"];
+            }
+            catch
+            {
+                Console.WriteLine("配置文件有误");
+            }
+        }
+        /// <summary>
+        /// 初始化连接池,在项目界面初始化时,进行数据库连接池初始化一般设为10个连接
+        /// </summary>
+        /// <param name="max"></param>
+        /// <param name="connectionString"></param>
+        /// <returns></returns>
+        public static BlockingQueue InitConnPooling(int max, string connectionString)
+        {
+            BlockingQueue bq = new BlockingQueue();
+            for (int i = 0; i < max; i++)
+            {
+                Connection conn = DatabaseConnPoolFactory.CreateConnection(connectionString);
+                if (conn.mySqlConnFlag)
+                {
+                    bq.Enqueue(conn);
+                }
+            }
+            return bq;
+        }
+        /// <summary>
+        /// 获取连接
+        /// </summary>
+        /// <returns></returns>
+        public static Connection GetConnection(BlockingQueue bq)
+        {
+            Connection connection = (Connection)bq.Dequeue();
+            while (!connection.mySqlConnFlag)
+            {
+                connection = (Connection)bq.Dequeue();                
+            }
+            return connection;
+        }
+        /// <summary>
+        /// 移除连接
+        /// </summary>
+        /// <param name="conn"></param>
+        public static void RemoveConnection(Connection conn)
+        {
+            conn.Dispose();
+        }
+        /// <summary>
+        /// 定时重置连接
+        /// </summary>
+        /// <param name="max"></param>
+        /// <param name="bq"></param>
+        public static void ResetConnPooling(int max, string connectionString, BlockingQueue bq)
+        {
+            CloseAll(bq);
+            InitConnPooling(max, connectionString);
+        }
+        /// <summary>
+        /// 添加连接到池中
+        /// </summary>
+        /// <param name="max"></param>
+        /// <param name="connectionString"></param>
+        public static void AddConnToPooling(int max, string connectionString)
+        {
+            InitConnPooling(max, connectionString);
+        }
+        /// <summary>
+        /// 检查连接是否可用,3<=连接数<=10
+        /// </summary>
+        /// <param name="min"> min = 3</param>
+        /// <param name="max">max = 10</param>
+        /// <param name="bq">server=59.175.148.85;uid=root;pwd=x5;database=zxpark</param>
+        public static void CheckConnPooling(int min, int max, string connectionString, BlockingQueue bq)
+        {
+            Connection conn = null;
+            int total = bq.Count();
+            for (int i = 0; i < total; i++)
+            {
+                conn = GetConnection(bq);
+                try
+                {
+                    string sql = "select count(1) from garage";
+                    MySqlCommand cmd = new MySqlCommand(sql, conn.mySqlConnection);
+                    object result = cmd.ExecuteScalar();
+                    if (result != null)
+                    {
+                        int r = Convert.ToInt32(result);
+                        Console.WriteLine("Number of garage in the database is: " + r);
+                    }
+                    ReleaseConnection(conn,bq);
+                }
+                catch (Exception ex)
+                {
+                    conn.Dispose();
+                    Console.WriteLine(ex.Message);
+                }
+            }
+            if (bq.Count() < min)
+            {
+                AddConnToPooling(max - min, connectionString);
+            }
+        }
+        /// <summary>
+        /// 释放连接,将连接返回阻塞队列
+        /// </summary>
+        /// <param name="conn"></param>
+        /// <param name="bq"></param>
+        public static void ReleaseConnection(Connection conn, BlockingQueue bq)
+        {
+            bq.Enqueue(conn);
+        }
+        /// <summary>
+        /// 关闭所有连接
+        /// </summary>
+        public static void CloseAll(BlockingQueue bq)
+        {
+            while (bq.Count() > 0)
+            {
+                Connection connection = (Connection)bq.Dequeue();
+                connection.Close();
+            }
+            Console.WriteLine("所有连接成功关闭!");
+        }
+    }
+}

+ 58 - 0
parkMonitor/Database2/DBAccess.cs

@@ -0,0 +1,58 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace parkMonitor.Database2
+{
+    class DBAccess
+    {
+        public string sql { set; get; }
+        public Connection conn { set; get; }
+        public CommandTypes commType { set; get; }
+
+        public List<string> strs { set; get; }
+        public DBAccess()
+        {
+            sql = null;
+            conn = null;
+
+        }
+
+        public DBAccess(string sql, Connection conn, CommandTypes commType)
+        {
+            this.sql = sql;
+            this.conn = conn;
+            this.commType = commType;
+
+        }
+
+        public DBAccess(Connection conn, CommandTypes commType, List<string> strs)
+        {
+            this.sql = null;
+            this.conn = conn;
+            this.commType = commType;
+            this.strs = strs;
+
+        }
+
+        public DBAccess(string sql, Connection conn, CommandTypes commType, Dictionary<string, string> args)
+        {
+
+            this.sql = sql;
+            this.conn = conn;
+            this.commType = commType;
+
+        }
+        public object GetResult()
+        {
+            object result = null;
+            AbstractCommand abscommand = SimpleCommandFactory.CreateCommandAndExcute(commType);
+            abscommand.sql = sql;
+            abscommand.conn = conn;
+            result = abscommand.ExecuteSqlCommand();
+            return result;
+        }
+    }
+}

+ 312 - 0
parkMonitor/Database2/DBOperation.cs

@@ -0,0 +1,312 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MySql.Data.MySqlClient;
+using parkMonitor.tools;
+using parkMonitor.server.CoreThread;
+
+namespace parkMonitor.Database2
+{
+    /// <summary>
+    /// 数据库操作类
+    /// </summary>
+    public class DBOperation
+    {
+        /// <summary>
+        /// 查询车库表获得剩余车位数
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="garageID"></param>
+        /// <returns></returns>
+        public int getGarageFreeSpace(BlockingQueue bq, int garageID)
+        {
+            MySqlDataReader reader = null;
+            int garageFreeSpace = 0;
+            string sql = "select * from garage where garageID = '" + garageID + "'";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                garageFreeSpace = reader.GetInt32("garageFreeSpace");
+                return garageFreeSpace;
+            }
+            else
+            {
+                Console.WriteLine("车位剩余数查无结果");
+            }
+            oper.DBClose(null,null,reader);
+            return 0;
+        }
+        /// <summary>
+        /// 查询所有车位位置及状态
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="garageID"></param>
+        /// <returns></returns>
+        public Dictionary<int, Parking_Space> GetAllParkingSpace(BlockingQueue bq, int garageID)
+        {
+            Dictionary<int, Parking_Space> lps = new Dictionary<int, Parking_Space>();
+            MySqlDataReader reader = null;
+            string sql = "select * from parkingspace where garageID = '" + garageID + "' ";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0)
+            {
+                for (int i = 1; i <= count && reader.Read(); i++)
+                {
+                    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;
+            }
+            else
+            {
+                Console.WriteLine("所有车位查无结果");
+            }
+            oper.DBClose(null,null,reader);
+            return null;
+        }
+        /// <summary>
+        /// 根据车牌查询得到车库id和车位id以及轮距
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="numberPlate"></param>
+        /// <returns></returns>
+        public Vehicle GetVehicle(BlockingQueue bq, string numberPlate)
+        {
+            Vehicle v = new Vehicle();
+            MySqlDataReader reader = null;
+            string sql = "select * from vehicle where numberPlate = '" + numberPlate + "'";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                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;
+            }
+            else
+            {
+                Console.WriteLine("云端车辆查无结果");
+            }
+            oper.DBClose(null,null,reader);
+            return null;
+        }
+        /// <summary>
+        /// 根据车位id获得x,y,z
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="parkingSpaceID"></param>
+        /// <returns></returns>
+        public Parking_Space GetFetchingSpace(BlockingQueue bq, int parkingSpaceID)
+        {
+            Parking_Space ps = new Parking_Space();
+            MySqlDataReader reader = null;
+            string sql = "select * from parkingspace where parkingSpaceID = '" + parkingSpaceID + " '";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                ps.parkingSpaceID = parkingSpaceID;
+                ps.parkingSpaceX = reader.GetInt32("parkingSpaceX");
+                ps.parkingSpaceY = reader.GetInt32("parkingSpaceY");
+                ps.parkingSpaceZ = reader.GetInt32("parkingSpaceZ");
+                return ps;
+            }
+            else
+            {
+                Console.WriteLine("车位xyz查无结果");
+            }
+            oper.DBClose(null,null,reader);
+            return null;
+        }
+        /// <summary>
+        /// 查询手机号是否被注册
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="tel"></param>
+        /// <returns></returns>
+        public bool IsTelRegister(BlockingQueue bq, string tel)
+        {
+            bool isTelRegister = false;
+            MySqlDataReader reader = null;
+            string sql = "select * from user where userTelephone = '" + tel + "'";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0)
+            {
+                isTelRegister = true;
+            }
+            else
+            {
+                isTelRegister = false;
+            }
+            oper.DBClose(null,null,reader);
+            return isTelRegister;
+        }
+        /// <summary>
+        /// 注册信息写入数据库,返回注册成功信息
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="tel"></param>
+        /// <param name="password"></param>
+        /// <returns></returns>
+        public int InsertUser(BlockingQueue bq, string tel, string password)
+        {
+            string sql = "insert into user(userTelephone,userPassword,userLevel) values('" + tel + "','" + password + "',1)";
+            int userID = 0;
+            Operation oper = new Operation(bq, sql);
+            oper.getInsert();
+            userID = oper.getInsertId();
+            return userID;
+        }
+        /// <summary>
+        /// 根据电话号码查询userID
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="tel"></param>
+        /// <returns></returns>
+        public int GetUserID(BlockingQueue bq, string tel)
+        {
+            int userID = 0;
+            MySqlDataReader reader = null;
+            string sql = "select userID from user where userTelephone = '" + tel + "'";
+            int count = 0;
+            Operation oper = new Operation(bq, sql);
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                userID = reader.GetInt32("userID");
+                return userID;
+            }
+            else
+            {
+                Console.WriteLine("userID查无结果");
+            }
+            oper.DBClose(null,null,reader);
+            return 0;
+        }
+        /// <summary>
+        /// 查询停车记录id
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="numberPlate"></param>
+        /// <returns></returns>
+        public int GetParkingRecordsID(BlockingQueue bq, string numberPlate)
+        {
+            int parkingRecordsID = 0;
+            MySqlDataReader reader = null;
+            string sql = "select parkingRecordsID from parkingrecords where numberPlate = '" + numberPlate + "' and parkingRecordsState = 3";
+            int count = 0;
+            Operation oper = new Operation(bq, sql);
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                parkingRecordsID = reader.GetInt32("parkingRecordsID");
+                return parkingRecordsID;
+            }
+            else
+            {
+                Console.WriteLine("停车记录id查无结果");
+            }
+            oper.DBClose(null,null,reader);
+            return 0;
+        }
+
+        /// <summary>
+        /// 车库有无此车
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="numberPlate"></param>
+        /// <param name="garageID"></param>
+        /// <returns></returns>
+        public bool IsNumberPlate(BlockingQueue bq, string numberPlate, int garageID)
+        {
+            bool isNumberPlate = true;
+            MySqlDataReader reader = null;
+            string sql = "select * from parkingrecords where numberPlate = '" + numberPlate + "' and parkingRecordsState = 3 and garageID = '" + garageID + "'";
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                isNumberPlate = true;
+            }
+            else
+            {
+                isNumberPlate = false;
+            }
+            oper.DBClose(null,null,reader);
+            return isNumberPlate;
+        }
+        /// <summary>
+        /// 判断车辆表中是否存在该车辆号牌
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="numberPlate"></param>
+        /// <returns></returns>
+        public bool IsNumberPlateFromVehicle(BlockingQueue bq, string numberPlate)
+        {
+            bool isNumberPlateFromVehicle = true;
+            string sql = "select * from vehicle where numberPlate = '" + numberPlate + "' and vehiclepParkState = 1";
+            MySqlDataReader reader = null;
+            Operation oper = new Operation(bq, sql);
+            int count = 0;
+            reader = oper.getResultSet(ref count);
+            if (count > 0 && reader.Read())
+            {
+                isNumberPlateFromVehicle = true;
+            }
+            else
+            {
+                isNumberPlateFromVehicle = false;
+            }
+            oper.DBClose(null,null,reader);
+            return isNumberPlateFromVehicle;
+        }
+        /// <summary>
+        /// 根据车牌号更新车辆状态
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="numberPlate"></param>
+        /// <param name="vehiclepParkState"></param>
+        public void UpdateVehicleParkState(BlockingQueue bq, string numberPlate, int vehiclepParkState)
+        {
+            string sql = "update vehicle set vehiclepParkState = '" + vehiclepParkState + "'where numberPlate = '" + numberPlate + "'";
+            Operation oper = new Operation(bq, sql);
+            oper.getUpdate();
+        }
+        /// <summary>
+        /// 更新车位状态
+        /// </summary>
+        /// <param name="bq"></param>
+        /// <param name="parkingSpaceID"></param>
+        /// <param name="parkingSpaceState"></param>
+        public void UpdateParkingSpaceState(BlockingQueue bq, int parkingSpaceID, int parkingSpaceState)
+        {
+            string sql = "update parkingspace set parkingSpaceState = '" + parkingSpaceState + "'where parkingSpaceID = '" + parkingSpaceID + "'";
+            Operation oper = new Operation(bq, sql);
+            oper.getUpdate();
+        }
+    }
+}

+ 123 - 0
parkMonitor/Database2/DatabaseConnPoolFactory.cs

@@ -0,0 +1,123 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MySql.Data.MySqlClient;
+
+namespace parkMonitor.Database2
+{
+    class DatabaseConnPoolFactory
+    {
+        public static Connection CreateConnection(string connectionString)
+        {
+            MySql.Data.MySqlClient.MySqlConnection conn = null;
+            Connection connection = new Connection();
+            try
+            {
+                conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
+                conn.Open();
+                Console.WriteLine("连接成功");
+                connection.mySqlConnection = conn;
+                connection.mySqlConnFlag = true;
+                connection.state = conn.State;
+                return connection;
+            }
+            catch (MySql.Data.MySqlClient.MySqlException ex)
+            {
+                switch (ex.Number)
+                {
+                    case 0:
+                        Console.WriteLine("Cannot connect to server.  Contact administrator");
+                        connection.mySqlConnFlag = false;
+                        connection.state = conn.State;
+                        return connection;
+                    case 1045:
+                        Console.WriteLine("Invalid username/password, please try again");
+                        connection.mySqlConnFlag = false;
+                        connection.state = conn.State;
+                        return connection;
+                    default:
+                        Console.WriteLine("database connection exception, please try again");
+                        connection.mySqlConnFlag = false;
+                        connection.state = conn.State;
+                        return connection;
+                }
+            }
+            catch (System.TimeoutException ex)
+            {
+                connection.mySqlConnFlag = false;
+                connection.state = conn.State;
+                Console.WriteLine(ex.Message);
+                return connection;
+            }
+            catch (System.IO.IOException ex)
+            {
+                connection.mySqlConnFlag = false;
+                connection.state = conn.State;
+                Console.WriteLine(ex.Message);
+                return connection;
+            }
+            catch (System.Net.Sockets.SocketException ex)
+            {
+                connection.mySqlConnFlag = false;
+                connection.state = conn.State;
+                Console.WriteLine(ex.Message);
+                return connection;
+            }
+        }
+        public static MySqlCommand CreateCommand(string sql,Connection conn) 
+        {
+            MySqlCommand cmd = null;
+            try
+            {
+                cmd = new MySqlCommand(sql, conn.mySqlConnection);
+                return cmd;
+            }
+            catch (MySqlException)
+            {
+                Console.WriteLine("数据库连接异常");
+            }
+            catch
+            {
+                Console.WriteLine("sql语句异常");
+            }
+            return null;
+        }
+    }
+
+    public class Connection
+    {
+        public Connection()
+        {
+            mySqlConnection = null;
+            mySqlConnFlag = false;
+        }
+        public MySql.Data.MySqlClient.MySqlConnection mySqlConnection { get; set; }
+
+        public System.Data.ConnectionState state { get; set; }
+        public bool mySqlConnFlag { get; set; }
+        public void Close()
+        {
+            try
+            {
+                mySqlConnection.Close();
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message);
+            }
+        }
+        public void Dispose()
+        {
+            try
+            {
+                mySqlConnection.Dispose();
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message);
+            }
+        }
+    }
+}

+ 223 - 0
parkMonitor/Database2/IDBOperation.cs

@@ -0,0 +1,223 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using MySql.Data.MySqlClient;
+using parkMonitor.tools;
+using parkMonitor.server.uiLogServer;
+using parkMonitor.LOG;
+using parkMonitor.server.CoreThread;
+using parkMonitor.entity;
+
+namespace parkMonitor.Database2
+{
+    interface IDBOperation
+    {
+        MySqlDataReader getResultSet(ref int count);
+        void getInsert();
+        void getUpdate();
+        void getDelete();
+    }
+    public class Operation : IDBOperation
+    {
+        /// <summary>
+        /// 数据库连接异常
+        /// </summary>
+        public static bool malfunctionRemote = false;
+        public static bool malfunctionLocal = false;
+        private int count = 0;
+        Connection connection = null;
+        MySqlCommand cmd = null;
+        MySqlDataReader reader = null;
+        public Operation(BlockingQueue bq, string sql)
+        {
+            connection = ConnectionPoolManager.GetConnection(bq);
+            cmd = DatabaseConnPoolFactory.CreateCommand(sql, connection);
+        }
+        /// <summary>
+        /// 数据库查询
+        /// </summary>
+        /// <param name="sql"></param>
+        /// <returns></returns>
+        public MySqlDataReader getResultSet(ref int count)
+        {
+            TryExecute(true, ref count);
+            return reader;
+        }
+        /// <summary>
+        /// 数据库插入
+        /// </summary>
+        public void getInsert()
+        {
+            TryExecute(false, ref this.count);
+        }
+        /// <summary>
+        /// 数据库更新
+        /// </summary>
+        public void getUpdate()
+        {
+            TryExecute(false, ref this.count);
+        }
+        /// <summary>
+        /// 数据库删除
+        /// </summary>
+        public void getDelete()
+        {
+            TryExecute(false, ref this.count);
+        }
+        /// <summary>
+        /// 获得插入记录ID
+        /// </summary>
+        /// <returns></returns>
+        public int getInsertId()
+        {
+            int insertID = 0;
+            try
+            {
+                insertID = Convert.ToInt32(cmd.LastInsertedId);
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine("插入数据库失败");
+                Console.WriteLine(ex.Message);
+            }
+            finally
+            {
+                DBClose(connection, cmd, reader);
+                //ConnectionPoolManager.ReleaseConnection(connection);
+            }
+            return insertID;
+        }
+
+        public void TryExecute(bool isSearch, ref int count)
+        {
+            MyTimer mt = new MyTimer();
+            mt.StartTiming();
+            int counter = 0;
+            while (true)
+            {
+                mt.EndTiming();
+                if (mt.IsLonger(30, 1, false, out counter) && counter >= 1)
+                {
+                    if (connection.mySqlConnection.DataSource == ConnectionPoolManager.remoteIP)
+                    {
+                        if (counter == 1)
+                        {
+                            UILogServer.ins.error("数据库操作异常,暂停处理自动命令。回滚后点击“启动远端DB”恢复");
+                            Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "操作远端DB异常");
+                        }
+                        Operation.malfunctionRemote = true;
+                    }
+                    else if (connection.mySqlConnection.DataSource == ConnectionPoolManager.localIP)
+                    {
+                        if (counter == 1)
+                        {
+                            UILogServer.ins.error("数据库操作异常,暂停处理自动命令。回滚后点击“启动本地DB”恢复");
+                            Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "操作本地DB异常");
+                        }
+                        Operation.malfunctionLocal = true;
+                    }
+                    break;
+                }
+                try
+                {
+                    //非查询,在内部关闭
+                    if (!isSearch)
+                    {
+                        cmd.ExecuteNonQuery();
+                        //DBClose(connection,cmd,null);
+                        break;
+                    }
+                    //查询,在上层关闭
+                    else
+                    {
+                        reader = cmd.ExecuteReader();
+                        while (reader.Read())
+                        {
+                            if (reader.HasRows)
+                            {
+                                count++;
+                            }
+                        }
+                        reader.Close();
+                        reader.Dispose();
+                        if (count > 0)
+                        {
+                            reader = cmd.ExecuteReader();
+                        }
+                        break;
+                    }
+                }
+                catch (Exception ex)
+                {
+                    Console.WriteLine(ex.Message);
+                    DBClose(connection,cmd,reader);
+                }
+                finally
+                {
+                    DBClose(connection, cmd, null);
+                }
+            }
+        }
+        public static bool MyTransaction(BlockingQueue bq,string sql, out int returnedValue)
+        {
+            List<string> strs = new List<string>();
+            strs.Add(sql);
+            return MyTransaction(bq,strs, out returnedValue);
+        }
+
+        public static bool MyTransaction(BlockingQueue bq,List<string> sqls, out int returnedValue)
+        {
+            Connection conn = ConnectionPoolManager.GetConnection(bq);
+            MySqlTransaction transaction = conn.mySqlConnection.BeginTransaction();
+            MySqlCommand cmd = conn.mySqlConnection.CreateCommand();
+            cmd.Transaction = transaction;
+            returnedValue = 0;
+            try
+            {
+                for (int i = 0; i < sqls.Count; i++)
+                {
+                    cmd.CommandText = sqls[i];
+                    cmd.ExecuteNonQuery();
+                    returnedValue = Convert.ToInt32(cmd.LastInsertedId);
+                }
+                transaction.Commit();
+                cmd.Dispose();
+                ConnectionPoolManager.ReleaseConnection(conn, EntityForCore.remoteBQ);
+                return true;
+            }
+            catch
+            {
+                try
+                {
+                    transaction.Rollback();
+                }
+                catch { return false; }
+                UILogServer.ins.error("数据库操作失败,事务回滚");
+                if (cmd != null) { cmd.Dispose(); }
+                if (conn != null)
+                {
+                    ConnectionPoolManager.ReleaseConnection(conn, EntityForCore.remoteBQ);
+                }
+            }
+            return false;
+        }
+        public void DBClose(Connection conn, MySqlCommand cmd, MySqlDataReader reader)
+        {
+            if (reader != null)
+            {
+                reader.Close();
+                reader.Dispose();
+            }
+            if (cmd != null)
+            {
+                cmd.Dispose();
+            }
+            if (conn != null)
+            {
+                ConnectionPoolManager.ReleaseConnection(conn, EntityForCore.remoteBQ);
+            }
+        }
+    }
+}

+ 38 - 0
parkMonitor/Database2/SimpleCommandFactory.cs

@@ -0,0 +1,38 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace parkMonitor.Database2
+{
+    class SimpleCommandFactory
+    {
+        public static AbstractCommand CreateCommandAndExcute(CommandTypes type)
+        {
+            AbstractCommand absCommands = null;
+
+            switch (type)
+            {
+                case CommandTypes.NOQUERYCOMMAND:
+                    absCommands = new NoQueryCommand();
+                    break;
+                case CommandTypes.QUERYCOMMAND:
+                    absCommands = new QueryCommand();
+                    break;
+                case CommandTypes.SCALARCOMMAND:
+                    absCommands = new ScalarCommand();
+                    break;
+                case CommandTypes.TRANSACTIONCOMMAND:
+                    absCommands = new TransactionCommand();
+                    break;
+                case CommandTypes.STOREPROCEDURECOMMAND:
+                    absCommands = new StoreProcedureCommand();
+                    break;
+                default:
+                    break;
+            }
+            return absCommands;
+        }
+    }
+}

+ 20 - 0
parkMonitor/Database2/Vehicle.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace parkMonitor.Database2
+{
+    /// <summary>
+    /// 车辆表数据结构
+    /// </summary>
+    public class Vehicle
+    {
+        public int parkingSpaceID { get; set; }
+        public int garageID { get; set; }
+        public int frontwheelbase { get; set; }
+        public int rearwheelbase { get; set; }
+        public int parkingRecordsID { get; set; }
+    }
+}

+ 32 - 45
parkMonitor/bll/MainBll.cs

@@ -13,6 +13,7 @@ using System.Threading;
 using parkMonitor.LOG;
 using System.Threading;
 using parkMonitor.server.CoreThread;
+using parkMonitor.Database2;
 
 namespace parkMonitor.bll
 {
@@ -44,59 +45,45 @@ namespace parkMonitor.bll
                 });
             }
 
-            ////日志文件执行数据库操作
-            //Task.Factory.StartNew(() =>
+
+            //Connection conn = null;
+            //try
             //{
-            //    while (!EntityForCore.ins.globalStatus)
+            //    conn = ConnectionPoolManager.GetConnection(CoreThreadTest2.remoteBQ); //从连接池获取连接对象;
+            //    if (conn.state == System.Data.ConnectionState.Open)
+            //    {
+            //        Console.WriteLine(conn.state + "hello");
+            //    }
+            //    else
             //    {
-            //        Thread.Sleep(1000);
+            //        conn.Close();
             //    }
-            //    lock (Parking_Space.spaceLock)
+
+            //    string sql = "select * from user";
+            //    MySqlCommand cmd = new MySqlCommand(sql, conn.mySqlConnection);
+            //    MySqlDataReader rdr = cmd.ExecuteReader();
+            //    while (rdr.Read())
             //    {
-            //        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;
-            //                }
-            //            }
-            //        }
+            //        Console.WriteLine(rdr[0] + "---------" + rdr[1] + "---------" + rdr[2]);
             //    }
-            //});
+            //    Thread.Sleep(5000);
+            //    rdr.Close();
+            //    ConnectionPoolManager.ReleaseConnection(conn,CoreThreadTest2.remoteBQ);
+
+            //}
+            //catch (Exception ex)
+            //{
+            //    Console.WriteLine(ex.Message + "==============" + "数据库连接已断开,请检查网络连接状态,然后重置连接");
+            //    Thread.Sleep(10000);
+            //    ConnectionPoolManager.ResetConnPooling(10, ConnectionPoolManager.remoteConf,CoreThreadTest2.remoteBQ);
+            //}
+
+           
             //主窗口关闭事件
             CmdServer.ins.listen(CmdType.MainWinClosed, () =>
             {
+                ConnectionPoolManager.CloseAll(EntityForCore.remoteBQ);
+                ConnectionPoolManager.CloseAll(EntityForCore.localBQ);
                 coreThread.Stop();
                 monitorThread.Stop();
             });

+ 10 - 47
parkMonitor/entity/EntityForCore.cs

@@ -1,5 +1,5 @@
 using MySql.Data.MySqlClient;
-using parkMonitor.DataBase;
+using parkMonitor.Database2;
 using parkMonitor.LOG;
 using parkMonitor.server.CoreThread;
 using parkMonitor.server.uiLogServer;
@@ -9,6 +9,7 @@ using System.Configuration;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
+using parkMonitor.tools;
 
 namespace parkMonitor.entity
 {
@@ -29,6 +30,8 @@ namespace parkMonitor.entity
         public int fetching_startRobot_address { get; set; }
         public int frontWheelbase_address { get; set; }
         public int rearWheelbase_address { get; set; }
+        public static BlockingQueue remoteBQ;
+        public static BlockingQueue localBQ;
         public static EntityForCore ins = new EntityForCore();
         public EntityForCore()
         {
@@ -63,26 +66,11 @@ namespace parkMonitor.entity
             Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "******************************************* 新的开始 *******************************************");
             bool initState = true;
             //数据库连接参数初始化及连接测试
-            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "数据库初始配置");
-            DBConnection.Init();
-            //远端连接测试
-            using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
-            //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
-            {
-                Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "远端数据库连接测试");
-                Operation.TryOpen(conn);
-                initState = conn.Ping();
-                Operation.DBClose(conn, null, null);
-            }
-            //本地连接测试
-            using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
-            //using (MySqlConnection conn = new MySqlConnection(DBConnection.localConf))
-            {
-                Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "本地数据库连接测试");
-                Operation.TryOpen(conn);
-                initState = initState && conn.Ping();
-                Operation.DBClose(conn, null, null);
-            }
+            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "数据库初始配置");       
+            //数据库初始化
+            ConnectionPoolManager.Init();
+            remoteBQ = ConnectionPoolManager.InitConnPooling(10, ConnectionPoolManager.remoteConf);
+            localBQ = ConnectionPoolManager.InitConnPooling(10, ConnectionPoolManager.localConf);
             //车位管理初始化
             Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "初始化车位");
             ParkingSpaceManager.ins = new ParkingSpaceManager();
@@ -105,32 +93,7 @@ namespace parkMonitor.entity
             UILogServer.ins.info("系统初始化中......");
             bool initState = true;
             //数据库连接参数初始化及连接测试
-            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "数据库初始配置");
-            DBConnection.Init();
-            //远端连接测试
-            if (DBmode == 0 || DBmode == 1)
-            {
-                using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
-                //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
-                {
-                    Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "远端数据库连接测试");
-                    Operation.TryOpen(conn);
-                    initState = conn.Ping();
-                    DBConnectionPool.getPool(DBConnection.remoteConf).ConnectionClose(conn);
-                }
-            }
-            //本地连接测试
-            if (DBmode == 0 || DBmode == 1)
-            {
-                using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
-                //using (MySqlConnection conn = new MySqlConnection(DBConnection.localConf))
-                {
-                    Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "本地数据库连接测试");
-                    Operation.TryOpen(conn);
-                    initState = initState && conn.Ping();
-                    Operation.DBClose(conn, null, null);
-                }
-            }
+            Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "数据库初始配置");          
             //车位管理初始化
             Log.WriteLog(LogType.NOT_DATABASE, LogFile.LOG, "初始化车位");
             ParkingSpaceManager.ins = new ParkingSpaceManager();

+ 8 - 0
parkMonitor/parkMonitor.csproj

@@ -99,6 +99,14 @@
     <Compile Include="controlPanel\InitWin.Designer.cs">
       <DependentUpon>InitWin.cs</DependentUpon>
     </Compile>
+    <Compile Include="Database2\AbstractCommand.cs" />
+    <Compile Include="Database2\ConnectionPoolManager.cs" />
+    <Compile Include="Database2\DatabaseConnPoolFactory.cs" />
+    <Compile Include="Database2\DBAccess.cs" />
+    <Compile Include="Database2\DBOperation.cs" />
+    <Compile Include="Database2\IDBOperation.cs" />
+    <Compile Include="Database2\SimpleCommandFactory.cs" />
+    <Compile Include="Database2\Vehicle.cs" />
     <Compile Include="DataBase\DBConnection.cs" />
     <Compile Include="DataBase\DBConnectionPool.cs" />
     <Compile Include="DataBase\DBOperation.cs" />

+ 148 - 159
parkMonitor/server/CoreThread/AbstractCmd.cs

@@ -10,7 +10,8 @@ using System.Configuration;
 using System.Threading;
 using parkMonitor.server.uiLogServer;
 using parkMonitor.tools;
-using parkMonitor.DataBase;
+//using parkMonitor.DataBase;
+using parkMonitor.Database2;
 using MySql.Data.MySqlClient;
 using parkMonitor.server.NumMachine;
 
@@ -198,7 +199,6 @@ namespace parkMonitor.server.CoreThread
         /// <param name="isParking"></param>
         public void Rollback(Command queueCmd, int parkingSpaceID, bool parkingSpaceUpdated, bool isParking, LaserMessage lm)
         {
-            string connectionStr = null;
             //命令回退
             queueCmd.returnedCount += 1;
             queuingThread.SetMessage(queueCmd);
@@ -228,27 +228,22 @@ namespace parkMonitor.server.CoreThread
                     }
                     try
                     {
-                        using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
-                        //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                        bool DBStoreStatus = false;                       //判断数据库事务操作是否正常
+                        int temp;
+                        string updateParkingSpaceSql = "update parkingspace set parkingSpaceState = 0 where parkingSpaceID = '" + parkingSpaceID + "'";
+                        string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + ParkingSpaceManager.ins.GetFreeSpaceCount() + "' where garageID = '" + ParkingSpaceManager.garageID + "'";
+                        string updateVehicleStateSql = "update vehicle set vehiclepParkState = '" + 0 + "'where numberPlate = '" + queueCmd.LicenseNum + "'";
+                        List<string> strs = new List<string>();
+                        strs.Add(updateParkingSpaceSql);
+                        strs.Add(updateFreeSpaceSql);
+                        strs.Add(updateVehicleStateSql);
+                        if (!Operation.MyTransaction(EntityForCore.remoteBQ,strs, out temp))
                         {
-                            Operation.TryOpen(conn);
-                            bool DBStoreStatus = false;                       //判断数据库事务操作是否正常
-                            int temp;
-                            string updateParkingSpaceSql = "update parkingspace set parkingSpaceState = 0 where parkingSpaceID = '" + parkingSpaceID + "'";
-                            string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + ParkingSpaceManager.ins.GetFreeSpaceCount() + "' where garageID = '" + ParkingSpaceManager.garageID + "'";
-                            string updateVehicleStateSql = "update vehicle set vehiclepParkState = '" + 0 + "'where numberPlate = '" + queueCmd.LicenseNum + "'";
-                            List<string> strs = new List<string>();
-                            strs.Add(updateParkingSpaceSql);
-                            strs.Add(updateFreeSpaceSql);
-                            strs.Add(updateVehicleStateSql);
-                            if (!Operation.MyTransaction(conn, strs, out temp))
-                            {
-                                //数据库操作失败写日志
-                                Log.WriteLog(LogType.DATABASE, updateParkingSpaceSql);
-                                Log.WriteLog(LogType.DATABASE, updateFreeSpaceSql);
-                                Log.WriteLog(LogType.DATABASE, updateVehicleStateSql);
-                                //Log.WriteLog(LogType.DATABASE, strs.ToArray().ToString());
-                            }
+                            //数据库操作失败写日志
+                            Log.WriteLog(LogType.DATABASE, updateParkingSpaceSql);
+                            Log.WriteLog(LogType.DATABASE, updateFreeSpaceSql);
+                            Log.WriteLog(LogType.DATABASE, updateVehicleStateSql);
+                            //Log.WriteLog(LogType.DATABASE, strs.ToArray().ToString());
                         }
                     }
                     catch
@@ -267,27 +262,22 @@ namespace parkMonitor.server.CoreThread
                     }
                     try
                     {
-                        using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
-                        //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                        bool DBStoreStatus = false;                       //判断数据库事务操作是否正常
+                        int temp;
+                        string updateParkingSpaceSql = "update parkingspace set parkingSpaceState = 1 where parkingSpaceID = '" + parkingSpaceID + "'";
+                        string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + ParkingSpaceManager.ins.GetFreeSpaceCount() + "' where garageID = '" + ParkingSpaceManager.garageID + "'";
+                        string updateVehicleStateSql = "update vehicle set vehiclepParkState = '" + 1 + "'where numberPlate = '" + queueCmd.LicenseNum + "'";
+                        List<string> strs = new List<string>();
+                        strs.Add(updateParkingSpaceSql);
+                        strs.Add(updateFreeSpaceSql);
+                        strs.Add(updateVehicleStateSql);
+                        if (!Operation.MyTransaction(EntityForCore.remoteBQ,strs, out temp))
                         {
-                            Operation.TryOpen(conn);
-                            bool DBStoreStatus = false;                       //判断数据库事务操作是否正常
-                            int temp;
-                            string updateParkingSpaceSql = "update parkingspace set parkingSpaceState = 1 where parkingSpaceID = '" + parkingSpaceID + "'";
-                            string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + ParkingSpaceManager.ins.GetFreeSpaceCount() + "' where garageID = '" + ParkingSpaceManager.garageID + "'";
-                            string updateVehicleStateSql = "update vehicle set vehiclepParkState = '" + 1 + "'where numberPlate = '" + queueCmd.LicenseNum + "'";
-                            List<string> strs = new List<string>();
-                            strs.Add(updateParkingSpaceSql);
-                            strs.Add(updateFreeSpaceSql);
-                            strs.Add(updateVehicleStateSql);
-                            if (!Operation.MyTransaction(conn, strs, out temp))
-                            {
-                                //数据库操作失败写日志
-                                Log.WriteLog(LogType.DATABASE, updateParkingSpaceSql);
-                                Log.WriteLog(LogType.DATABASE, updateFreeSpaceSql);
-                                Log.WriteLog(LogType.DATABASE, updateVehicleStateSql);
-                                //Log.WriteLog(LogType.DATABASE, strs.ToArray().ToString());
-                            }
+                            //数据库操作失败写日志
+                            Log.WriteLog(LogType.DATABASE, updateParkingSpaceSql);
+                            Log.WriteLog(LogType.DATABASE, updateFreeSpaceSql);
+                            Log.WriteLog(LogType.DATABASE, updateVehicleStateSql);
+                            //Log.WriteLog(LogType.DATABASE, strs.ToArray().ToString());
                         }
                     }
                     catch
@@ -298,21 +288,20 @@ namespace parkMonitor.server.CoreThread
             }
             else
             {
-                connectionStr = "SqlConnectionLocation";
                 if (isParking)
                 {
-                    oper.UpdateVehicleParkState(connectionStr, queueCmd.LicenseNum, 0);
+                    oper.UpdateVehicleParkState(EntityForCore.remoteBQ, queueCmd.LicenseNum, 0);
                     if (parkingSpaceUpdated)
                     {
-                        oper.UpdateParkingSpaceState(connectionStr, parkingSpaceID, 0);
+                        oper.UpdateParkingSpaceState(EntityForCore.remoteBQ, parkingSpaceID, 0);
                     }
                 }
                 else
                 {
-                    oper.UpdateVehicleParkState(connectionStr, queueCmd.LicenseNum, 1);
+                    oper.UpdateVehicleParkState(EntityForCore.remoteBQ, queueCmd.LicenseNum, 1);
                     if (parkingSpaceUpdated)
                     {
-                        oper.UpdateParkingSpaceState(connectionStr, parkingSpaceID, 1);
+                        oper.UpdateParkingSpaceState(EntityForCore.remoteBQ, parkingSpaceID, 1);
                     }
                 }
             }
@@ -395,11 +384,11 @@ namespace parkMonitor.server.CoreThread
             {
                 if (!queueCmd.manual)
                 {
-                    ppp = ParkingSpaceManager.ins.MallocParkingSpace(cEntrance, DBConnection.remoteConf, queueCmd, out count);//自动 
+                    ppp = ParkingSpaceManager.ins.MallocParkingSpace(cEntrance, ConnectionPoolManager.remoteConf, queueCmd, out count);//自动 
                 }
                 else
                 {
-                    ppp = ParkingSpaceManager.ins.MallocParkingSpace(cEntrance, DBConnection.localConf, queueCmd, out count);//手动
+                    ppp = ParkingSpaceManager.ins.MallocParkingSpace(cEntrance, ConnectionPoolManager.localConf, queueCmd, out count);//手动
                 }
                 if (ppp != null)
                 {
@@ -550,13 +539,12 @@ namespace parkMonitor.server.CoreThread
                     queuingThread.SetMessage(queueCmd);
                     if (!queueCmd.manual)
                     {
-                        connectionStr = "SqlConnectionStr";
-                        oper.UpdateParkingSpaceState(connectionStr, parkingSpaceID, 0);
+                        oper.UpdateParkingSpaceState(EntityForCore.remoteBQ, parkingSpaceID, 0);
                     }
                     else
                     {
                         connectionStr = "SqlConnectionLocation";
-                        oper.UpdateParkingSpaceState(connectionStr, parkingSpaceID, 0);
+                        oper.UpdateParkingSpaceState(EntityForCore.localBQ, parkingSpaceID, 0);
                     }
                     break;
                 }
@@ -669,7 +657,7 @@ namespace parkMonitor.server.CoreThread
             {
                 bool displayed = false;
                 //等待获取缓冲位资源
-                
+
 
                 //获取车位资源
                 ppp = WaitForParkingSpaceResource(queueCmd, out freeSpaceCount);
@@ -750,11 +738,11 @@ namespace parkMonitor.server.CoreThread
                 else
                 {
                     //更新本地车位表车位状态
-                    oper.UpdateParkingSpaceState(DBConnection.localConf, ppp.parkingSpaceID, 1);
+                    oper.UpdateParkingSpaceState(EntityForCore.localBQ, ppp.parkingSpaceID, 1);
                     //更新车库表剩余车位数
                     //int freeSpaceCount = oper.getGarageFreeSpace(connectionStr, garageID);
                     //freeSpaceCount = freeSpaceCount - 1;
-                    oper.UpdateGarageFreeSpace(DBConnection.localConf, freeSpaceCount, garageID);
+                    //oper.UpdateGarageFreeSpace(CoreThreadTest2.localBQ, freeSpaceCount, garageID);
                 }
                 //号牌失效,数据库回滚
                 if (disappeared)
@@ -762,11 +750,11 @@ namespace parkMonitor.server.CoreThread
                     NumReset(queueCmd);
                     if (!queueCmd.manual)
                     {
-                        oper.UpdateVehicleParkState(DBConnection.remoteConf, queueCmd.LicenseNum, 0);
+                        oper.UpdateVehicleParkState(EntityForCore.remoteBQ, queueCmd.LicenseNum, 0);
                     }
                     else
                     {
-                        oper.UpdateVehicleParkState(DBConnection.remoteConf, queueCmd.LicenseNum, 0);
+                        oper.UpdateVehicleParkState(EntityForCore.localBQ, queueCmd.LicenseNum, 0);
                     }
                     return;
                 }
@@ -858,53 +846,73 @@ namespace parkMonitor.server.CoreThread
                 {
                     if (!queueCmd.manual)
                     {
-                        //connectionStr = "SqlConnectionStr";
-                        //更新云端数据库
-                        //插入停车记录表
-                        //int parkingRecordsID = oper.InsertToParkingRecords(connectionStr, userID, numberPlate, ppp.parkingSpaceID, ppp.garageID, 3, realParkTime);
-                        //车辆表更新车辆信息
-                        //oper.UpdateVehicle(connectionStr, numberPlate, 1, realParkTime, parkingRecordsID, ppp.parkingSpaceID, 1, frontWheelbase, rearWheelbase);
-                        //插入消息队列表
-                        //oper.InsertToMessageQueue(connectionStr, userID, "停车成功", 1);
-
                         //事务,写入停车记录, 更新车辆信息
                         int parkingRecordsID = 0;
                         try
                         {
-                            using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
-                            //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                            string insertRecordSql = "insert into parkingrecords(userID,numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime) values('" + userID + "','" + queueCmd.LicenseNum + "','" + ppp.parkingSpaceID + "','" + garageID + "',3,'" + realParkTime + "')";
+                            if (!Operation.MyTransaction(EntityForCore.remoteBQ,insertRecordSql, out parkingRecordsID))
                             {
-                                Operation.TryOpen(conn);
-                                //string insertRecordSql = "insert into parkingrecords(userID,numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime) values('" + userID + "','" + queueCmd.LicenseNum + "','" + ppp.parkingSpaceID + "','" + garageID + "',3,'" + realParkTime + "')";
-                                string insertRecordSql = "insert into parkingrecords(userID,numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime) values('" + userID + "','" + queueCmd.LicenseNum + "','" + ppp.parkingSpaceID + "','" + garageID + "',3,'" + realParkTime + "')";
-                                if (!Operation.MyTransaction(conn, insertRecordSql, out parkingRecordsID))
-                                {
-                                    //数据库操作失败写日志
-                                    Log.WriteLog(LogType.DATABASE, "1", insertRecordSql);
-                                }
+                                //数据库操作失败写日志
+                                Log.WriteLog(LogType.DATABASE, "1", insertRecordSql);
                             }
-                            using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
-                            //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                            int freeSpace = ParkingSpaceManager.ins.GetFreeSpaceCount();
+                            List<string> strs = new List<string>();
+                            string updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 1 where parkingSpaceID = '" + ppp.parkingSpaceID + "'";
+                            string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpace + "' where garageID = '" + garageID + "'";
+                            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 + "'";
+                            strs.Add(updateParkingSpaceStateSql);
+                            strs.Add(updateFreeSpaceSql);
+                            strs.Add(updateVehicleSql);
+                            int temp;
+                            if (!Operation.MyTransaction(EntityForCore.remoteBQ,strs, out temp))
                             {
-                                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 + "'";
-                                int temp;
-                                if (!Operation.MyTransaction(conn, updateVehicleSql, out temp))
-                                {
-                                    //数据库操作失败写日志
-                                    Log.WriteLog(LogType.DATABASE, "0", updateVehicleSql);
-                                }
+                                //数据库操作失败写日志
+                                Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
+                                Log.WriteLog(LogType.DATABASE, "0", updateFreeSpaceSql);
+                                Log.WriteLog(LogType.DATABASE, "0", updateVehicleSql);
                             }
                         }
                         catch
                         {
-                            throw;//数据库操作失败异常待处理
+                            UILogServer.ins.error("停车数据库操作失败");
+                            //throw;//数据库操作失败异常待处理
                         }
                     }
                     else
                     {
-                        //插入停车记录表
-                        int parkingRecordsID = oper.InsertToLocalParkingRecords(DBConnection.localConf, 1, userID, queueCmd.LicenseNum, ppp.parkingSpaceID, garageID, 3, realParkTime, frontWheelbase, rearWheelbase);
+                        //事务,写入停车记录, 更新车辆信息
+                        int parkingRecordsID = 0;
+                        try
+                        {
+                            string insertRecordSql = "insert into parkingrecords(userID,numberPlate,parkingSpaceID,garageID,parkingRecordsState,realParkTime) values('" + userID + "','" + queueCmd.LicenseNum + "','" + ppp.parkingSpaceID + "','" + garageID + "',3,'" + realParkTime + "')";
+                            if (!Operation.MyTransaction(EntityForCore.remoteBQ, insertRecordSql, out parkingRecordsID))
+                            {
+                                //数据库操作失败写日志
+                                Log.WriteLog(LogType.DATABASE, "1", insertRecordSql);
+                            }
+                            int freeSpace = ParkingSpaceManager.ins.GetFreeSpaceCount();
+                            List<string> strs = new List<string>();
+                            string updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 1 where parkingSpaceID = '" + ppp.parkingSpaceID + "'";
+                            string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpace + "' where garageID = '" + garageID + "'";
+                            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 + "'";
+                            strs.Add(updateParkingSpaceStateSql);
+                            strs.Add(updateFreeSpaceSql);
+                            strs.Add(updateVehicleSql);
+                            int temp;
+                            if (!Operation.MyTransaction(EntityForCore.remoteBQ, strs, out temp))
+                            {
+                                //数据库操作失败写日志
+                                Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
+                                Log.WriteLog(LogType.DATABASE, "0", updateFreeSpaceSql);
+                                Log.WriteLog(LogType.DATABASE, "0", updateVehicleSql);
+                            }
+                        }
+                        catch
+                        {
+                            UILogServer.ins.error("停车数据库操作失败");
+                            //throw;//数据库操作失败异常待处理
+                        }
                     }
                 }
             }
@@ -962,13 +970,11 @@ namespace parkMonitor.server.CoreThread
                     queuingThread.SetMessage(queueCmd);
                     if (!queueCmd.manual)
                     {
-                        connectionStr = "SqlConnectionStr";
-                        oper.UpdateParkingSpaceState(connectionStr, parkingSpaceID, 0);
+                        oper.UpdateParkingSpaceState(EntityForCore.remoteBQ, parkingSpaceID, 0);
                     }
                     else
                     {
-                        connectionStr = "SqlConnectionLocation";
-                        oper.UpdateParkingSpaceState(connectionStr, parkingSpaceID, 0);
+                        oper.UpdateParkingSpaceState(EntityForCore.localBQ, parkingSpaceID, 0);
                     }
                     break;
                 }
@@ -1009,13 +1015,11 @@ namespace parkMonitor.server.CoreThread
             //获取车辆表中车辆相关信息
             if (queueCmd.manual)
             {
-                connectionStr = DBConnection.localConf;
-                vehiclelist = oper.GetLocalVehicle(connectionStr, queueCmd.LicenseNum, queueCmd.garageID);
+                //vehiclelist = oper.GetLocalVehicle(connectionStr, queueCmd.LicenseNum, queueCmd.garageID);
             }
             else
             {
-                connectionStr = DBConnection.remoteConf;
-                vehiclelist = oper.GetVehicle(connectionStr, queueCmd.LicenseNum);
+                vehiclelist = oper.GetVehicle(EntityForCore.remoteBQ, queueCmd.LicenseNum);
             }
             if (vehiclelist == null)
             {
@@ -1040,17 +1044,6 @@ namespace parkMonitor.server.CoreThread
             frontwheelbase = vehiclelist.frontwheelbase;
             rearwheelbase = vehiclelist.rearwheelbase;
             queueCmd.parkingRecordsID = vehiclelist.parkingRecordsID;
-            //获取车位表中车辆具体的车位信息
-            //if (queueCmd.manual)
-            //{
-            //    connectionStr = DBConnection.localStr;
-            //    ps = oper.GetFetchingSpace(connectionStr, parkingSpaceID);
-            //}
-            //else
-            //{
-            //    connectionStr = DBConnection.remoteStr;
-            //    ps = oper.GetFetchingSpace(connectionStr, parkingSpaceID);
-            //}
             if (!ParkingSpaceManager.ins.parkingSpaceStatusMap.TryGetValue(parkingSpaceID, out ps) || ps == null)
             {
                 EntityForCore.ins.globalStatus = false;
@@ -1139,49 +1132,54 @@ namespace parkMonitor.server.CoreThread
                         //取车事务
                         try
                         {
-                            using (MySqlConnection conn = DBConnectionPool.getPool(DBConnection.remoteConf).getConnection())
-                            //using (MySqlConnection conn = new MySqlConnection(DBConnection.remoteConf))
+                            bool DBStoreStatus = false;       //判断数据库事务操作是否正常 
+                            int temp;
+                            List<string> strs = new List<string>();
+                            string updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 0 where parkingSpaceID = '" + ps.parkingSpaceID + "'";
+                            string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpaceCount + "' where garageID = '" + garageID + "'";
+                            string updateVehicleStateSql = "update vehicle set vehiclepParkState = 0 where numberPlate = '" + queueCmd.LicenseNum + "'";
+                            string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + queueCmd.TimeRecord + "'where parkingRecordsID = '" + queueCmd.parkingRecordsID + "'";
+                            strs.Add(updateParkingSpaceStateSql);
+                            strs.Add(updateFreeSpaceSql);
+                            strs.Add(updateVehicleStateSql);
+                            strs.Add(updateParkingRecordsSql);
+                            if (!Operation.MyTransaction(EntityForCore.remoteBQ,strs, out temp))
                             {
-                                Operation.TryOpen(conn);
-                                bool DBStoreStatus = false;       //判断数据库事务操作是否正常 
-                                int temp;
-                                List<string> strs = new List<string>();
-                                //string updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 0 where parkingSpaceID = '" + ps.parkingSpaceID + "'";
-                                //string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpaceCount + "' where garageID = '" + garageID + "'";
-                                string updateVehicleStateSql = "update vehicle set vehiclepParkState = 0 where numberPlate = '" + queueCmd.LicenseNum + "'";
-                                string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + queueCmd.TimeRecord + "'where parkingRecordsID = '" + queueCmd.parkingRecordsID + "'";
-                                //strs.Add(updateParkingSpaceStateSql);
-                                //strs.Add(updateFreeSpaceSql);
-                                strs.Add(updateVehicleStateSql);
-                                strs.Add(updateParkingRecordsSql);
-                                if (!Operation.MyTransaction(conn, strs, out temp))
-                                {
-                                    Log.WriteLog(LogType.NOT_DATABASE, "数据库操作出错,记录sql语句等待流程回滚");
-                                    //写日志记录sql,以待之后处理
-                                    //Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
-                                    //Log.WriteLog(LogType.DATABASE, "0", updateFreeSpaceSql);
-                                    Log.WriteLog(LogType.DATABASE, "0", updateVehicleStateSql);
-                                    Log.WriteLog(LogType.DATABASE, "0", updateParkingRecordsSql);
-                                }
+                                Log.WriteLog(LogType.NOT_DATABASE, "数据库操作出错,记录sql语句等待流程回滚");
+                                //写日志记录sql,以待之后处理
+                                Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
+                                Log.WriteLog(LogType.DATABASE, "0", updateFreeSpaceSql);
+                                Log.WriteLog(LogType.DATABASE, "0", updateVehicleStateSql);
+                                Log.WriteLog(LogType.DATABASE, "0", updateParkingRecordsSql);
                             }
                         }
                         catch
                         {
-                            throw;//数据库操作失败异常待处理
+                            UILogServer.ins.error("数据库操作失败");
+                            //throw;//数据库操作失败异常待处理
                         }
                     }
                     else
                     {
-                        //更新车库表车位数
-                        //int freeSpaceCount = oper.getGarageFreeSpace(connectionStr, garageID);
-                        //freeSpaceCount = freeSpaceCount + 1;
-                        oper.UpdateGarageFreeSpace(DBConnection.localConf, freeSpaceCount, garageID);
-                        //更新车位表车位状态
-                        oper.UpdateParkingSpaceState(DBConnection.localConf, ps.parkingSpaceID, 0);
-                        ////更新车辆表车辆停车状态
-                        //locationOper.UpdateVehicleParkState(queueCmd.LicenseNum, 0);
-                        //更新停车记录表
-                        oper.UpdateParkingRecords(DBConnection.localConf, 0, 6, queueCmd.TimeRecord, queueCmd.parkingRecordsID);
+                        int temp;
+                        List<string> strs = new List<string>();
+                        string updateParkingSpaceStateSql = "update parkingspace set parkingSpaceState = 0 where parkingSpaceID = '" + ps.parkingSpaceID + "'";
+                        string updateFreeSpaceSql = "update garage set garageFreeSpace = '" + freeSpaceCount + "' where garageID = '" + garageID + "'";
+                        string updateVehicleStateSql = "update vehicle set vehiclepParkState = 0 where numberPlate = '" + queueCmd.LicenseNum + "'";
+                        string updateParkingRecordsSql = "update parkingrecords set parkingRecordsState = 6,realGetTime = '" + queueCmd.TimeRecord + "'where parkingRecordsID = '" + queueCmd.parkingRecordsID + "'";
+                        strs.Add(updateParkingSpaceStateSql);
+                        strs.Add(updateFreeSpaceSql);
+                        strs.Add(updateVehicleStateSql);
+                        strs.Add(updateParkingRecordsSql);
+                        if (!Operation.MyTransaction(EntityForCore.localBQ, strs, out temp))
+                        {
+                            Log.WriteLog(LogType.NOT_DATABASE, "数据库操作出错,记录sql语句等待流程回滚");
+                            //写日志记录sql,以待之后处理
+                            Log.WriteLog(LogType.DATABASE, "0", updateParkingSpaceStateSql);
+                            Log.WriteLog(LogType.DATABASE, "0", updateFreeSpaceSql);
+                            Log.WriteLog(LogType.DATABASE, "0", updateVehicleStateSql);
+                            Log.WriteLog(LogType.DATABASE, "0", updateParkingRecordsSql);
+                        }
                     }
                 }
                 Thread.Sleep(1000);
@@ -1198,14 +1196,14 @@ namespace parkMonitor.server.CoreThread
         {
             if (queueCmd.commandType == 'e')
             {
-                int userId = Convert.ToInt32(queueCmd.userID);
-                //过期用户指令
-                oper.InsertToMessageQueue(DBConnection.remoteConf, userId, "停车异常,请联系管理员!", 2);
-                //未能停车,将车辆状态复位
-                oper.UpdateVehicleParkState(DBConnection.remoteConf, queueCmd.LicenseNum, 0);
-                Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "过期用户指令,车牌号:" + queueCmd.LicenseNum);
-                UILogServer.ins.error("过期用户指令,车牌号:" + queueCmd.LicenseNum);
-                //continue;
+                //int userId = Convert.ToInt32(queueCmd.userID);
+                ////过期用户指令
+                //oper.InsertToMessageQueue(DBConnection.remoteConf, userId, "停车异常,请联系管理员!", 2);
+                ////未能停车,将车辆状态复位
+                //oper.UpdateVehicleParkState(DBConnection.remoteConf, queueCmd.LicenseNum, 0);
+                //Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "过期用户指令,车牌号:" + queueCmd.LicenseNum);
+                //UILogServer.ins.error("过期用户指令,车牌号:" + queueCmd.LicenseNum);
+                ////continue;
             }
         }
     }
@@ -1227,15 +1225,6 @@ namespace parkMonitor.server.CoreThread
             if (queueCmd.commandType == 'f')
             {
                 abstractCmd = new FetchCmd();
-                //bool IsNumberPlateFromVehicle = oper.IsNumberPlateFromVehicle("SqlConnectionStr",queueCmd.LicenseNum);
-                //if (IsNumberPlateFromVehicle)
-                //{
-                //    abstractCmd = new FetchCmd();
-                //}
-                //else
-                //{
-                //    Console.WriteLine(queueCmd.LicenseNum+"不在车库中");
-                //}
             }
             if (queueCmd.commandType == 'e')
             {

+ 20 - 71
parkMonitor/server/CoreThread/CoreThreadTest2.cs

@@ -10,7 +10,8 @@ using System.Configuration;
 using System.Threading;
 using parkMonitor.server.uiLogServer;
 using parkMonitor.tools;
-using parkMonitor.DataBase;
+//using parkMonitor.DataBase;
+using parkMonitor.Database2;
 using MySql.Data.MySqlClient;
 using parkMonitor.server.CoreThread;
 using System.Threading;
@@ -19,7 +20,6 @@ namespace parkMonitor.server.CoreThread
 {
     class CoreThreadTest2 : IEquipments, ICoreThreadDoWorking
     {
-
         int equipmentStatus_address;//设备总控状态地址
         bool initialized = false;
         /// <summary>
@@ -49,8 +49,7 @@ namespace parkMonitor.server.CoreThread
         }
 
         public void Start()
-        {
-
+        {          
             for (int i = 0; i < equipNames.Length; ++i)
             {
                 IEquipments equip = EquipmentSimpleFactory.ins.CreateEquipment(equipNames[i]);
@@ -100,13 +99,16 @@ 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 checkRemoteTimer = new System.Threading.Timer(CheckRemotePool, null, 300000, 300000);
+            Timer checkLocalTimer = new System.Threading.Timer(CheckLocalPool, null, 300000, 300000);
+            //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;
@@ -222,58 +224,7 @@ namespace parkMonitor.server.CoreThread
         }
 
         /// <summary>
-        /// 定时执行车库、车位更新
-        /// </summary>
-        private void updateGarageAndParkingSpace(object o)
-        {
-            if (EntityForCore.ins.globalStatus && ParkingBufferManager.ins.checkedManually)
-            {
-                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)
@@ -295,17 +246,12 @@ namespace parkMonitor.server.CoreThread
                         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);
-                            }
+                            Operation.MyTransaction(EntityForCore.remoteBQ,sqlMsg, out parkingRecordsID);
                         }
                         catch
                         {
                             Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
-                            throw;
+                            //throw;
                         }
                     }
                     if (sqlStatus == "0")
@@ -313,22 +259,25 @@ namespace parkMonitor.server.CoreThread
                         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);
-                            }
+                            Operation.MyTransaction(EntityForCore.remoteBQ,sqlMsg, out temp); 
                         }
                         catch
                         {
                             Log.WriteLog(LogType.DATABASE, sqlStatus + ":" + sqlMsg);
-                            throw;
+                            //throw;
                         }
                     }
                     Log.ReadLog(out sqlStatus, out sqlMsg, out count);
                 }
             }
         }
+        private void CheckRemotePool(object o)
+        {
+            ConnectionPoolManager.CheckConnPooling(3, 10, ConnectionPoolManager.remoteConf, EntityForCore.remoteBQ);
+        }
+        private void CheckLocalPool(object o)
+        {
+            ConnectionPoolManager.CheckConnPooling(3, 10, ConnectionPoolManager.localConf, EntityForCore.localBQ);
+        }
     }
 }

+ 3 - 2
parkMonitor/server/CoreThread/SpaceManager.cs

@@ -3,7 +3,8 @@ using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
-using parkMonitor.DataBase;
+//using parkMonitor.DataBase;
+using parkMonitor.Database2;
 using System.Configuration;
 using parkMonitor.entity;
 using System.Configuration;
@@ -51,7 +52,7 @@ namespace parkMonitor.server.CoreThread
                     catch { UILogServer.ins.error("无法获得车位权重,请检查配置"); Log.WriteLog(LogType.NOT_DATABASE, LogFile.ERROR, "无法获得车位权重"); return false; }
                     if (garageID != 0)
                     {
-                        parkingSpaceStatusMap = oper.GetAllParkingSpace(DBConnection.remoteConf, garageID);
+                        parkingSpaceStatusMap = oper.GetAllParkingSpace(EntityForCore.remoteBQ, garageID);
                     }
                     else
                     {

+ 1 - 2
parkMonitor/tools/BlockingQueue.cs

@@ -26,9 +26,8 @@ namespace parkMonitor.tools
             //} 
         }
 
-        public int count()
+        public int Count()
         {
-
             return bQueue.Count();
         }
     }