1. 程式人生 > >呼叫SQL連線池 重複開啟connection.Open()連結超時異常的處理

呼叫SQL連線池 重複開啟connection.Open()連結超時異常的處理

最近遇到一個很奇葩的問題,就是反覆重新整理頁面通過SQL去查詢資料的時候,按了10多遍了後系統會GG,直接卡住奔潰,一直在找問題,最後是SQL讀取資料後資源無釋放,連線無關閉的原因。

DBHelper.cs程式碼:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Configuration;
namespace CPMA.Models
{
    public class DBHelper
    {
        public static string strconn = ConfigurationManager.ConnectionStrings["NewUserTest"].ToString();
        private readonly object balanceLock = new object();
        public SqlDataReader ExecuteReader(String sql)
        {
            SqlConnection connection = new SqlConnection(strconn);
            try
            {
                connection.Open();
                SqlCommand command = new SqlCommand(sql, connection);
                //SqlDataReader result = command.ExecuteReader();
                return command.ExecuteReader(CommandBehavior.CloseConnection);
            }
            catch
            {
                //關閉連線,丟擲異常
                connection.Close();
                throw;
            }

        }

        public bool ExecuteCommand(String sql)
        {
            bool result = false;
            try
            {
                SqlConnection connection = new SqlConnection(strconn);
                connection.Open();
                SqlCommand command = new SqlCommand(sql, connection);
                //command.Connection = connection;
                //command.CommandText = sql;
                int sum = command.ExecuteNonQuery();
                connection.Close();
                if (sum >= 1)
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return result;
        }

    }

}

如何釋放?

只要在執行了Reader的方法使用using去釋放資源,關閉連線就行了,例子如下程式碼。

 //獲取推薦的電子產品
        public JsonResult GetAllHotGoods()
        {
            List<Goods> GoodsList = new List<Goods>();
            string sql = "select * from Goods where HotState=1 Order by GoodsConcernNum DESC";
            using (var reader = helper.ExecuteReader(sql))
            {
                try
                {
                    while (reader.Read())
                    {
                        Goods baking = new Goods();
                        baking.GoodsId = int.Parse(reader["GoodsId"].ToString());
                        baking.ShopId = int.Parse(reader["ShopId"].ToString());
                        baking.GoodsName = reader["GoodsName"].ToString();
                        baking.GoodsPrice = int.Parse(reader["GoodsPrice"].ToString());
                        baking.GoodsConcernNum = int.Parse(reader["GoodsConcernNum"].ToString());
                        baking.GoodsIntroduce = reader["GoodsIntroduce"].ToString();
                        baking.Category = reader["Category"].ToString();
                        baking.GoodsFMImgPath = reader["GoodsFMImgPath"].ToString();
                        baking.HotState = int.Parse(reader["HotState"].ToString());
                        baking.CreateDate = reader["CreateDate"].ToString();
                        GoodsList.Add(baking);
                    }
                    return Json(GoodsList);
                }
                catch (Exception ex)
                {
                    return Json(GoodsList);
                }
            }