是否可以在页面生命周期内保持数据库连接打开?

时间:2022-10-03 15:29:58

Everybody knows that you should close a connection immediately after you finish using it.

每个人都知道你应该在使用完毕后立即关闭连接。

Due to a flaw in my domain object model design, I've had to leave the connection open for the full page life cycle. Essentially, I have a Just In Time property which opens a connection on first call, and then on Page.Unload (..) it would check if a db connection was ever open, and then close it if it was. Since it only takes a second, I’ve had the opinion its not too much different than closing it immediately.

由于我的域对象模型设计存在缺陷,我不得不在整个页面生命周期中保持连接打开状态。本质上,我有一个Just In Time属性,它在第一次调用时打开一个连接,然后在Page.Unload(..)上它将检查数据库连接是否打开,如果是,则关闭它。由于它只需要一秒钟,我认为它与立即关闭它没有太大的不同。

Is this ok? Or should it still be closed immediately after every single use?

这个可以吗?或者它应该在每次使用后立即关闭吗?

Thanks in advance.

提前致谢。

10 个解决方案

#1


1  

It's not ideal but I wouldn't re-write my application over it. Unless your page is doing a large amount of time-consuming work in various methods, the whole page lifecycle should execute quickly. In practice it may just mean that your connection object is open a few milliseconds longer than it would have been otherwise. That might be significant in some scenarios, but it doesn't sound like it would be in your case.

这不是理想的,但我不会重写我的应用程序。除非您的页面在各种方法中执行大量耗时的工作,否则整个页面生命周期应该快速执行。在实践中,它可能只意味着您的连接对象比其他情况下打开的时间长几毫秒。在某些情况下这可能很重要,但听起来并不像你的情况。

#2


7  

No, it is not OK.

不,不行。

If your application will ever need to grow or scale, you'll want to fix this issue. By holding that connection open you're reducing your ability to scale. Keep in mind that open connections take up memory on the server, memory on the client, hold open locks, etc.

如果您的应用程序需要增长或扩展,您将需要解决此问题。通过保持该连接打开,您将降低您的扩展能力。请记住,打开的连接会占用服务器上的内存,客户端上的内存,保持打开的锁等。

#3


3  

What if you page crashes before reaching the Page.Unload event? You will have a opened connection. For me it is better to always close the connection as soon as possible.

如果在到达Page.Unload事件之前页面崩溃怎么办?您将打开一个连接。对我来说,最好始终尽快关闭连接。

#4


2  

Yes, it is ok.

是的,没关系。

Closing the connection as soon as you can is a best practice for preventing orphan open connections, but if you are sure that the connection is being close, there is nothing wrong with that.

尽快关闭连接是防止孤立打开连接的最佳做法,但如果您确定连接正在关闭,则没有任何问题。

#5


2  

Every decent ASP.NET app uses connection pooling nowadays, and a pool is basically a bunch of open connections. In your case that would mean that the connection you're holding on to is "occupied" and can't be used to serve other requests.

现在每个体面的ASP.NET应用程序都使用连接池,而池基本上是一堆打开的连接。在您的情况下,这意味着您持有的连接被“占用”并且不能用于提供其他请求。

As far as I see it would be a scalability issue depending on the amount of time your page needs to do work/render. If you expect only 100 users, like you say, then probably it's not an issue - unless it's 100 req/sec of course.

据我所知,这将是一个可扩展性问题,具体取决于您的页面需要进行工作/渲染的时间。如果您只想要100个用户,那么可能不是问题 - 除非它当然是100 req / sec。

From the technological perspective it's OK. As far as I remember most client-server applications (web- and non-web), including classic ASP-code used to work like that, e.g you declare one connection for the entire page and work with it.

从技术角度来看,没关系。据我所知,大多数客户端 - 服务器应用程序(网络和非网络),包括曾经这样工作的经典ASP代码,例如,您为整个页面声明一个连接并使用它。

#6


1  

page crashes? this is what using and finally are for

页面崩溃?这就是使用和最终的用途

that said, for the sake of DB performance (i.e. scaling)* it's best to keep connections open as short a period as possible allowing only that you don't want to open close open close open close for rapidly sequential and predictable work

也就是说,为了DB性能(即缩放)*,最好保持连接打开的时间尽可能短,只允许你不想打开近距离打开关闭,以便快速顺序和可预测的工作

* I was told this by a mentor early in my career, I must say I've not actually tested this myself but it sounds right theoretically

*在我的职业生涯早期,我被一名导师告知过这一点,我必须说我自己没有亲自测试过,但理论上听起来不错

#7


1  

Of course you can keep them open, but no no. Close it after use in finally blocks. A fair trade off from "after every single use" is to close it after every block of use, if you're apt to run a stored proc, update a column, then delete some other row, you could open/close around those three operations, presuming they're all wrapped in a try/catch/finally.

当然你可以让它们保持开放,但不是没有。在finally块中使用后关闭它。从“每次使用后”的公平交易是在每个使用块之后关闭它,如果你倾向于运行存储过程,更新列,然后删除其他行,你可以打开/关闭这三个操作,假设它们全部包含在try / catch / finally中。

#8


1  

You should certainly keep the connection open across the lifetime of the page, if you're doing multiple queries during it. Generally, one re-uses connections across many pages, actually.

如果您在页面的生命周期中进行多次查询,则应确保在页面生命周期内保持连接打开状态。通常,实际上,重用多个页面的连接。

#9


1  

I think a better question with much more informed and productive feedback would be possibly providing some snippets of what you're doing (code) and expanding on the reasons why you've made this choice. There is most likely a better solution that doesn't require keeping the connection open so long, but at least, for pragmatic reasons, you could get some feedback on whether it's worth revamping.

我认为一个更好的问题,更有见地和更有成效的反馈,可能会提供你正在做的事情(代码)的一些片段,并扩展你做出这个选择的原因。最有可能是一个更好的解决方案,不需要保持连接打开这么长时间,但至少,出于实际的原因,你可以得到一些关于它是否值得改进的反馈。

In future, you definitely want to move away from data access in your code-behind.

将来,您肯定希望摆脱代码隐藏中的数据访问。

#10


1  

I find it convenient to keep the connection open when using ORM (Open Session in View) so that after an initial eager fetch, other data can be lazily loaded as needed. This works well when page response times are reasonable so as not to tie up connections.

我发现在使用ORM(在View中打开会话)时保持连接打开很方便,这样在初始急切获取后,可以根据需要延迟加载其他数据。当页面响应时间合理时,这种方法很有效,以免束缚连接。

#1


1  

It's not ideal but I wouldn't re-write my application over it. Unless your page is doing a large amount of time-consuming work in various methods, the whole page lifecycle should execute quickly. In practice it may just mean that your connection object is open a few milliseconds longer than it would have been otherwise. That might be significant in some scenarios, but it doesn't sound like it would be in your case.

这不是理想的,但我不会重写我的应用程序。除非您的页面在各种方法中执行大量耗时的工作,否则整个页面生命周期应该快速执行。在实践中,它可能只意味着您的连接对象比其他情况下打开的时间长几毫秒。在某些情况下这可能很重要,但听起来并不像你的情况。

#2


7  

No, it is not OK.

不,不行。

If your application will ever need to grow or scale, you'll want to fix this issue. By holding that connection open you're reducing your ability to scale. Keep in mind that open connections take up memory on the server, memory on the client, hold open locks, etc.

如果您的应用程序需要增长或扩展,您将需要解决此问题。通过保持该连接打开,您将降低您的扩展能力。请记住,打开的连接会占用服务器上的内存,客户端上的内存,保持打开的锁等。

#3


3  

What if you page crashes before reaching the Page.Unload event? You will have a opened connection. For me it is better to always close the connection as soon as possible.

如果在到达Page.Unload事件之前页面崩溃怎么办?您将打开一个连接。对我来说,最好始终尽快关闭连接。

#4


2  

Yes, it is ok.

是的,没关系。

Closing the connection as soon as you can is a best practice for preventing orphan open connections, but if you are sure that the connection is being close, there is nothing wrong with that.

尽快关闭连接是防止孤立打开连接的最佳做法,但如果您确定连接正在关闭,则没有任何问题。

#5


2  

Every decent ASP.NET app uses connection pooling nowadays, and a pool is basically a bunch of open connections. In your case that would mean that the connection you're holding on to is "occupied" and can't be used to serve other requests.

现在每个体面的ASP.NET应用程序都使用连接池,而池基本上是一堆打开的连接。在您的情况下,这意味着您持有的连接被“占用”并且不能用于提供其他请求。

As far as I see it would be a scalability issue depending on the amount of time your page needs to do work/render. If you expect only 100 users, like you say, then probably it's not an issue - unless it's 100 req/sec of course.

据我所知,这将是一个可扩展性问题,具体取决于您的页面需要进行工作/渲染的时间。如果您只想要100个用户,那么可能不是问题 - 除非它当然是100 req / sec。

From the technological perspective it's OK. As far as I remember most client-server applications (web- and non-web), including classic ASP-code used to work like that, e.g you declare one connection for the entire page and work with it.

从技术角度来看,没关系。据我所知,大多数客户端 - 服务器应用程序(网络和非网络),包括曾经这样工作的经典ASP代码,例如,您为整个页面声明一个连接并使用它。

#6


1  

page crashes? this is what using and finally are for

页面崩溃?这就是使用和最终的用途

that said, for the sake of DB performance (i.e. scaling)* it's best to keep connections open as short a period as possible allowing only that you don't want to open close open close open close for rapidly sequential and predictable work

也就是说,为了DB性能(即缩放)*,最好保持连接打开的时间尽可能短,只允许你不想打开近距离打开关闭,以便快速顺序和可预测的工作

* I was told this by a mentor early in my career, I must say I've not actually tested this myself but it sounds right theoretically

*在我的职业生涯早期,我被一名导师告知过这一点,我必须说我自己没有亲自测试过,但理论上听起来不错

#7


1  

Of course you can keep them open, but no no. Close it after use in finally blocks. A fair trade off from "after every single use" is to close it after every block of use, if you're apt to run a stored proc, update a column, then delete some other row, you could open/close around those three operations, presuming they're all wrapped in a try/catch/finally.

当然你可以让它们保持开放,但不是没有。在finally块中使用后关闭它。从“每次使用后”的公平交易是在每个使用块之后关闭它,如果你倾向于运行存储过程,更新列,然后删除其他行,你可以打开/关闭这三个操作,假设它们全部包含在try / catch / finally中。

#8


1  

You should certainly keep the connection open across the lifetime of the page, if you're doing multiple queries during it. Generally, one re-uses connections across many pages, actually.

如果您在页面的生命周期中进行多次查询,则应确保在页面生命周期内保持连接打开状态。通常,实际上,重用多个页面的连接。

#9


1  

I think a better question with much more informed and productive feedback would be possibly providing some snippets of what you're doing (code) and expanding on the reasons why you've made this choice. There is most likely a better solution that doesn't require keeping the connection open so long, but at least, for pragmatic reasons, you could get some feedback on whether it's worth revamping.

我认为一个更好的问题,更有见地和更有成效的反馈,可能会提供你正在做的事情(代码)的一些片段,并扩展你做出这个选择的原因。最有可能是一个更好的解决方案,不需要保持连接打开这么长时间,但至少,出于实际的原因,你可以得到一些关于它是否值得改进的反馈。

In future, you definitely want to move away from data access in your code-behind.

将来,您肯定希望摆脱代码隐藏中的数据访问。

#10


1  

I find it convenient to keep the connection open when using ORM (Open Session in View) so that after an initial eager fetch, other data can be lazily loaded as needed. This works well when page response times are reasonable so as not to tie up connections.

我发现在使用ORM(在View中打开会话)时保持连接打开很方便,这样在初始急切获取后,可以根据需要延迟加载其他数据。当页面响应时间合理时,这种方法很有效,以免束缚连接。