任何人都知道在常规ASP中关闭连接的最佳做法是什么,我应该在每次sql查询后立即执行,还是只在页面底部?
例如,这样就可以了:
sql = "SELECT COUNT(*) AS num FROM tblUSER"
set rstemp = connTemp.execute(sql)
theCount = rstemp("num")
sql = "SELECT COUNT(*) AS num2 FROM tblCUSTOMER"
set rstemp = connTemp.execute(sql)
theCount2 = rstemp("num2")
rstemp.close
set rstemp = nothing
或者我应该在每次连接后关闭连接,如下所示:
sql = "SELECT COUNT(*) AS num FROM tblUSER"
set rstemp = connTemp.execute(sql)
theCount = rstemp("num")
rstemp.close
set rstemp = nothing
sql = "SELECT COUNT(*) AS num2 FROM tblCUSTOMER"
set rstemp = connTemp.execute(sql)
theCount2 = rstemp("num2")
rstemp.close
set rstemp = nothing
(如果我们在每次查询后关闭连接,它会使用更多或更少的资源,它会增加或减少锁定等)
答案 0 :(得分:8)
一般的经验法则是尽可能多地重复使用。关闭并重新打开每个查询的连接将不必要地增加您的开销,并且还可能产生连接池问题(如果您在很短的时间内运行了大量的查询。)
希望这会有所帮助。 戴夫
答案 1 :(得分:0)
对于我编写的每个页面,我打开一个连接对象,然后将所有记录集的相同连接对象用作记录集的“ActiveConnection”属性,然后在我完成工作时关闭连接对象。
示例:
Dim cn
Set cn = CreateObject("Adodb.Connection")
cn.Open MyDsnString
Dim rs1
Set rs1 = CreateObject("Adodb.Recordset")
rs1.ActiveConnection = cn
rs1.source = "some query"
rs1.Open
'... (stuff happens here) '
rs1.Close
Set rs1 = Nothing
'... (stuff happens here) '
Dim rs2
Set rs2 = CreateObject("Adodb.Recordset")
rs2.ActiveConnection = cn
rs2.source = "select something from sometable"
rs2.Open
'... (stuff happens here) '
rs2.Close
Set rs2 = Nothing
'... (stuff happens here) '
cn.Close
Set cn = Nothing
这样,整个页面只有一个连接打开,而且非常有效。