网站异常点击过多就没有任何反应了

.Net技术 码拜 9年前 (2015-03-28) 1087次浏览 0个评论
 

放到服务器上的网站,自己测试,点击不到30下,就没有任何反应了。
之间还出现过这样的异常,我百度了,web.config里也添加了相应的代码。超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小。
DBHelper里的代码:
 public static SqlConnection Conn
        {
            get
            {
                string conStr = ConfigurationManager.ConnectionStrings[“CCWNConnectionString”].ToString();
                //上面这个必须添加引用System.configuartion
                conn = new SqlConnection(conStr);
                conn.Open();
                return conn;
            }
        }
        public static int SqlExc(string sql)
        {
            using (SqlCommand sqlCmd = new SqlCommand(sql, Conn))
            {
                int result = sqlCmd.ExecuteNonQuery();
                return result;
            }
        }
        
        public static DataTable GetDataTable(string sql)
        {
            using (SqlCommand sqlCmd = new SqlCommand(sql, Conn))
            {
                SqlDataAdapter sqlDap = new SqlDataAdapter(sqlCmd);
                DataSet ds = new DataSet();
                sqlDap.Fill(ds);
                return ds.Tables[0];
            }
        }

web.config里这么写了:
<httpRuntime maxRequestLength=”2097150″ requestLengthDiskThreshold=”2048″ maxUrlLength=”10240″ useFullyQualifiedRedirectUrl=”true” />
connectionString=”Data Source=.;Initial Catalog=db;User ID=sa;Password=123;pooling=true;Max Pool Size=512

网站异常点击过多就没有任何反应了
既然是网站,访问数据库就不要使用长连接,应该每次访问数据库之前open,操作完释放
否则用户不点击退出,直接关闭IE,你连接永远也没法释放
网站异常点击过多就没有任何反应了
10分
get
            {
                string conStr = ConfigurationManager.ConnectionStrings["CCWNConnectionString"].ToString();
                //上面这个必须添加引用System.configuartion
                conn = new SqlConnection(conStr);
                conn.Open();
                return conn;
 
            }
/pre>
这个get函数完全是在坑爹呢,每次获取都创建一个新的连接,然后永远也不释放,这不是不用几下连接池就满了
网站异常点击过多就没有任何反应了
10分
conn只开不管,能不死吗?关Command不代表关Connection
话说你直接用petshop的sqlhelper不就可以了,有必要自己写个么?
网站异常点击过多就没有任何反应了
string conStr = ConfigurationManager.ConnectionStrings[“CCWNConnectionString”].ToString();
这东西完全可以使用get获取
pre class=”brush: csharp”>
using(SqlConnection Conn=new SqlConnection(conStr))
{conn.Open();
using (SqlCommand sqlCmd = new SqlCommand(sql, Conn))
            {
                int result = sqlCmd.ExecuteNonQuery();
                return result;
            }
}
/pre>
网站异常点击过多就没有任何反应了
引用 3 楼 starfd 的回复:

conn只开不管,能不死吗?关Command不代表关Connection
话说你直接用petshop的sqlhelper不就可以了,有必要自己写个么?

用了 using() 这个不是用完之后就自动释放的吗?

网站异常点击过多就没有任何反应了
引用 4 楼 Z65443344 的回复:

string conStr = ConfigurationManager.ConnectionStrings[“CCWNConnectionString”].ToString();
这东西完全可以使用get获取
pre class=”brush: csharp”>
using(SqlConnection Conn=new SqlConnection(conStr))
{conn.Open();
using (SqlCommand sqlCmd = new SqlCommand(sql, Conn))
            {
                int result = sqlCmd.ExecuteNonQuery();
                return result;
            }
}
/pre>

请问下get?

网站异常点击过多就没有任何反应了
引用 2 楼 Z65443344 的回复:
get
            {
                string conStr = ConfigurationManager.ConnectionStrings["CCWNConnectionString"].ToString();
                //上面这个必须添加引用System.configuartion
                conn = new SqlConnection(conStr);
                conn.Open();
                return conn;
 
            }
/pre>
这个get函数完全是在坑爹呢,每次获取都创建一个新的连接,然后永远也不释放,这不是不用几下连接池就满了

这是别人以前写的,我接手她的

网站异常点击过多就没有任何反应了
10分
public static string conStr
        {
            get
            {
                      return ConfigurationManager.ConnectionStrings["CCWNConnectionString"].ToString(); 
            }
        }

用using包起来,出了{}块就会自动释放了

网站异常点击过多就没有任何反应了
10分
引用 5 楼 kimuji 的回复:
Quote: 引用 3 楼 starfd 的回复:

conn只开不管,能不死吗?关Command不代表关Connection
话说你直接用petshop的sqlhelper不就可以了,有必要自己写个么?

用了 using() 这个不是用完之后就自动释放的吗?

using你关的是SqlCommand,不是SqlConnection,你看下红孩儿给你写的代码


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明网站异常点击过多就没有任何反应了
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!