调用SQL连接池 重复打开connection.Open()链接超时异常的处理

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/laizhixue/article/details/83585054

最近遇到一个很奇葩的问题,就是反复刷新页面通过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);
                }
            }

猜你喜欢

转载自blog.csdn.net/laizhixue/article/details/83585054