Is it acceptable to keep a db connection open for the life of the page?

前端 未结 10 1947
执念已碎
执念已碎 2021-01-18 07:11

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 conn

相关标签:
10条回答
  • 2021-01-18 07:47

    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.

    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.

    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.

    0 讨论(0)
  • 2021-01-18 07:47

    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

    * 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

    0 讨论(0)
  • 2021-01-18 07:47

    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.

    0 讨论(0)
  • 2021-01-18 07:48

    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.

    0 讨论(0)
提交回复
热议问题