ASP .NET Application_Start Event + Connection to a DB

跟風遠走 提交于 2019-12-12 04:46:02

问题


I have started developing a full-web application by using the ASP .NET MVC 3 framework. I am a MVC 3 beginner developer.

On the server-side I am going to execute some queries to a DB. Consequently I will have to make a connection to the DB.

I would like to know if it is better to make a connection in the beginning of each action method or in an event handler. I have thought about making the connection in the Application_Start event handler and then storing an object representing my connection into the application state. Does this way present disadvantages ?

Thanks in advance for your future answer


回答1:


Open and close the connection as required (rather than keeping one open). Let connection pooling work for you.

10 Tips for Writing High-Performance Web Applications (ASP.NET but contains relevant advice):

Tip 3—Connection Pooling

Setting up the TCP connection between your Web application and SQL Server™ can be an expensive operation. Developers at Microsoft have been able to take advantage of connection pooling for some time now, allowing them to reuse connections to the database. Rather than setting up a new TCP connection on each request, a new connection is set up only when one is not available in the connection pool. When the connection is closed, it is returned to the pool where it remains connected to the database, as opposed to completely tearing down that TCP connection.



来源:https://stackoverflow.com/questions/8800696/asp-net-application-start-event-connection-to-a-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!