在ASP.NET页面中使用一个SqlConnection实例 [英] Using one instance of SqlConnection in an ASP.NET page

查看:84
本文介绍了在ASP.NET页面中使用一个SqlConnection实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们新的.NET Web应用程序中,我们尝试将SqlConnection

的使用限制为每页只有一个实例,即使有多次访问

各种查询。背后的想法是,如果页面有多次调用数据库,并且每个人都管理自己的数据,这就减少了重复获取和返回池连接的需要。连接。但是,

这需要更多刻意编码,比如在页面的Dispose()方法中调用

SqlConnection.Close()方法,所以它是

保证在页面处理完成后关闭连接,

并且也没有过早关闭。我在想的是这是否实际上是必要的b $ b因为将Connection对象传递给子控件

当有多个用户控件或自定义时会变得相当麻烦

控制页面。如果连接池管理非常有效

那么在代码中重复打开和关闭连接(这实际上是获得并返回到池的连接的
)将不会是一个大问题,和保持代码简单的
更重要。任何人都可以给出一些

的建议吗?

In our new .NET web applications, we try to limit the use of SqlConnection
to just one instance per page, even if there are multiple accesses to
various queries. The thinking behind is that this reduces the need to
getting and returning connections to the pool repeatedly if a page has
multiple calls to the DB, and each one manages its own connection. However,
this does requires more deliberate coding, like calling the
SqlConnection.Close() method in the page''s Dispose() method so it''s
garanteed that the connection is closed when the page processing is done,
and also is not closed too early. What I''m thinking is whether this is
actually necessary because passing the Connection object into child controls
becomes a pretty big hassle when there are several user controls or custom
controls on the page. If the connection pool management is very efficient
then opening and closing connections repeatly in the code (which is really
getting and returning connections to the pool) wouldn''t be a big deal, and
keeping the code simple would be more important. Could anyone give some
suggestions?

推荐答案

Bob,IIRC,只要你用相同的连接打开连接

字符串,它将被汇集。


Alex Papadimoulis

"鲍勃" <博******* @ yahoo.com>在消息中写道

新闻:Og ************* @ TK2MSFTNGP11.phx.gbl ...
Bob, IIRC, so long as you open the connection with the same connection
string, it''ll be pooled.

Alex Papadimoulis
" Bob" <bo*******@yahoo.com> wrote in message
news:Og*************@TK2MSFTNGP11.phx.gbl...
在我们的新.NET网站中应用程序,我们尝试将SqlConnection的使用限制为每页只有一个实例,即使有多个访问来进行各种查询。背后的想法是,如果页面有多次调用DB,并且每个调用管理自己的连接,这就减少了重复获取和返回池连接的需要。
但是,这确实需要更有意识的编码,比如在页面的Dispose()方法中调用
SqlConnection.Close()方法,这样就可以保证连接是在页面处理完成时关闭,
并且也不会过早关闭。我在想的是这是否真的是必要的,因为当页面上有多个用户控件或自定义
控件时,将Connection对象传递给子
控件会变得非常麻烦。如果连接池管理非常高效,那么在代码中重复打开和关闭连接(这实际上是获取并返回到池的连接)并不是什么大问题,并且
保持代码简单会更重要。任何人都可以提出一些建议吗?
In our new .NET web applications, we try to limit the use of SqlConnection
to just one instance per page, even if there are multiple accesses to
various queries. The thinking behind is that this reduces the need to
getting and returning connections to the pool repeatedly if a page has
multiple calls to the DB, and each one manages its own connection. However, this does requires more deliberate coding, like calling the
SqlConnection.Close() method in the page''s Dispose() method so it''s
garanteed that the connection is closed when the page processing is done,
and also is not closed too early. What I''m thinking is whether this is
actually necessary because passing the Connection object into child controls becomes a pretty big hassle when there are several user controls or custom
controls on the page. If the connection pool management is very efficient
then opening and closing connections repeatly in the code (which is really
getting and returning connections to the pool) wouldn''t be a big deal, and
keeping the code simple would be more important. Could anyone give some
suggestions?



Alex,感谢您的回复。我知道相同的连接字符串

在同一个池中连接。我的问题是,获取和返回池中的连接是否在ADP.NET中变得如此高效以及是否需要在bql数据提供程序中添加额外的代码命令

减少从3到1的获取和返回次数。


谢谢

Bob


" Alex Papadimoulis" <人*** @ papadimoulis.com>在消息中写道

新闻:10 ************* @ corp.supernews.com ...
Alex, thanks for your reply. I understand that the same connection string
reults in connections in the same pool. My question is whether getting and
returning connections to the pool have become so efficient in ADP.NET and
Sql Data Provider that there is no need to put in extra code in order to
reduce the number of getting and returning from say, 3 to 1.

Thanks
Bob

"Alex Papadimoulis" <al***@papadimoulis.com> wrote in message
news:10*************@corp.supernews.com...
鲍勃,IIRC,这么久当你用相同的连接打开连接时,它会被合并。

Alex Papadimoulis
鲍勃" <博******* @ yahoo.com>在消息中写道
新闻:Og ************* @ TK2MSFTNGP11.phx.gbl ...
Bob, IIRC, so long as you open the connection with the same connection
string, it''ll be pooled.

Alex Papadimoulis
" Bob" <bo*******@yahoo.com> wrote in message
news:Og*************@TK2MSFTNGP11.phx.gbl...
在我们新的.NET Web应用程序中,我们尝试将
SqlConnection的使用限制为每页只有一个实例,即使有多个访问来进行各种查询。背后的想法是,如果页面有多次调用DB,并且每个调用管理自己的连接,这就减少了重复获取和返回池连接的需要。
In our new .NET web applications, we try to limit the use of SqlConnection to just one instance per page, even if there are multiple accesses to
various queries. The thinking behind is that this reduces the need to
getting and returning connections to the pool repeatedly if a page has
multiple calls to the DB, and each one manages its own connection.

这确实需要更多刻意的编码,比如在页面的Dispose()方法中调用
SqlConnection.Close()方法,所以它是
保证在页面处理完成
时关闭连接,并且也不会过早关闭。我在想的是这是否真的是必要的,因为将Connection对象传递给子
this does requires more deliberate coding, like calling the
SqlConnection.Close() method in the page''s Dispose() method so it''s
garanteed that the connection is closed when the page processing is done, and also is not closed too early. What I''m thinking is whether this is
actually necessary because passing the Connection object into child


控件

当有几个用户时会变得非常麻烦页面上的控件或
自定义控件。如果连接池管理的效率非常高,那么在代码中重复打开和关闭连接(这是
真正获得并返回到池的连接)并不是什么大问题,
保持代码简单会更重要。任何人都可以给出一些建议吗?
becomes a pretty big hassle when there are several user controls or custom controls on the page. If the connection pool management is very efficient then opening and closing connections repeatly in the code (which is really getting and returning connections to the pool) wouldn''t be a big deal, and keeping the code simple would be more important. Could anyone give some
suggestions?




"鲍勃" <博******* @ yahoo.com>在消息中写道

新闻:Og ************* @ TK2MSFTNGP11.phx.gbl ...
" Bob" <bo*******@yahoo.com> wrote in message
news:Og*************@TK2MSFTNGP11.phx.gbl...
在我们的新.NET网站中应用程序,我们尝试将SqlConnection的使用限制为每页只有一个实例,即使有多个访问来进行各种查询。背后的想法是,如果页面有多次调用数据库,那么这就减少了重复获取和返回池连接的需要
In our new .NET web applications, we try to limit the use of SqlConnection
to just one instance per page, even if there are multiple accesses to
various queries. The thinking behind is that this reduces the need to
getting and returning connections to the pool repeatedly if a page has
multiple calls to the DB




如果我错了,有人可以纠正我,但我相信从托管连接池获得并返回

连接是非常有效的 - 否则它将不会是b $ b不会是一个汇集/缓存机制!


Brad Williams



Someone can correct me if I''m wrong, but I believe getting and returning
connections from managed connection pool is very efficient -- otherwise it
wouldn''t be much of a pooling/caching mechanism!

Brad Williams


这篇关于在ASP.NET页面中使用一个SqlConnection实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆