ASP .NET Application_Start事件+连接到一个数据库 [英] ASP .NET Application_Start Event + Connection to a DB

查看:303
本文介绍了ASP .NET Application_Start事件+连接到一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始开发使用ASP .NET MVC 3框架的完整的Web应用程序。
我是一个MVC 3初学者开发者。

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.

我想知道这是否是更好地使每个操作方法的开头或事件处理程序的连接。
我曾经想过,使得在Application_Start事件处理程序的连接,然后存储对象重新presenting我连接到应用程序的状态。
请问这样present缺点是什么?

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

推荐答案

打开和关闭连接的要求(而不是保持一个打开的)。让连接池为你工作。

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

的10个技巧编写高性能Web应用程序(ASP。 NET但包含相关咨询):

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

提示3 - 连接池

设置您的Web应用程序和SQL之间的TCP连接
  服务器™可以是一个昂贵的操作。在微软开发人员
  能够充分利用连接池有一段时间了,
  允许它们重用到数据库的连接。而不是
  建立对每个请求新的TCP连接,新的连接
  成立只有当一个不是在连接池中使用。当。。。的时候
  连接是封闭的,其中,它保持其返回到池
  连接到数据库,而不是完全拆除该
  TCP连接。

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.

这篇关于ASP .NET Application_Start事件+连接到一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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