一个人应该如何保持一个ASP.NET MVC应用程序数据库连接? [英] How should one maintain a database connection in an ASP.NET MVC application?

查看:141
本文介绍了一个人应该如何保持一个ASP.NET MVC应用程序数据库连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不使用LINQ到SQL或实体框架位的web应用程序,并且当前已经使用这样的事情(这是一类项目):

 使用System.Data这;
使用System.Data.SqlClient的;命名空间StackOverflowClone.Models
{
    公共类数据库
    {
        公共静态的SqlConnection的ActiveConnection {搞定;私人集; }        静态数据库()
        {
            的ActiveConnection =新的SqlConnection(
                数据源= ******** database.windows.net。 +
                初始目录= EECS341; UID = *****; PWD = *******; +
                MultipleActiveResultSets = TRUE;);
            ActiveConnection.Open();
        }
    }
}

不过,这似乎是因为静态初始化每个服务器进程运行一次,而不是每一次的要求导致线程问题。

该框架是否提供了一个建在处理此方法,或者我应该只是有咳嗽起来,每次new'd了数据库连接功能?


解决方案

  

或者我应该只是有咳嗽起来,每次new'd了数据库连接功能?


是的,做到这一点。让 ADO.NET连接池处理细节为您服务。你的目标应该是保持连接打开尽可能短的一段时间内成为可能。


  

连接池减少新的连接次数
  必须打开。池程序保持身体的所有权
  连接。它通过保活了一套主动管理连接
  对于每个给定连接配置的连接。每当用户
  调用打开一个连接,池查找可用
  连接池中。如果一个池的连接可用,
  它返回给调用者,而不是打开一个新的连接。当。。。的时候
  应用程序调用连接关闭,池返回到
  汇集集活动连接而不是关闭它。一旦
  连接返回到池中,它已准备好就可以重复使用
  下次打开电话。


所以,创建返回一个新的开放连接的静态的getConnection()方法。一个使用语句中使用此,因此它可以被关闭,并尽快返回到连接池。

 使用(VAR CN = Database.GetConnection())
{
    //查询你的数据在这里,小巧玲珑下面的例子
    cn.Execute(更新MyTable的设置MyField的= @newValue,新的{}为newValue);
}

I'm not using LINQ-to-SQL or Entity Framework bits in a web app, and have currently been using something like this (this is for a class project):

using System.Data;
using System.Data.SqlClient;

namespace StackOverflowClone.Models
{
    public class Database
    {
        public static SqlConnection ActiveConnection { get; private set; }

        static Database()
        {
            ActiveConnection = new SqlConnection(
                "Data Source=********.database.windows.net;" +
                "Initial Catalog=EECS341;Uid=*****;Pwd=*******;" + 
                "MultipleActiveResultSets=True;");
            ActiveConnection.Open();
        }
    }
}

However this seems to cause threading issues because the static initializer runs once per server process, rather than once per request.

Does the framework provide a built in method of handling this or should I just have a function that coughs up database connections new'd up each time?

解决方案

or should I just have a function that coughs up database connections new'd up each time?

Yes, do this. Let ADO.NET connection pooling handle the details for you. Your goal should be to keep the connection open for as short a period of time as possible.

Connection pooling reduces the number of times that new connections must be opened. The pooler maintains ownership of the physical connection. It manages connections by keeping alive a set of active connections for each given connection configuration. Whenever a user calls Open on a connection, the pooler looks for an available connection in the pool. If a pooled connection is available, it returns it to the caller instead of opening a new connection. When the application calls Close on the connection, the pooler returns it to the pooled set of active connections instead of closing it. Once the connection is returned to the pool, it is ready to be reused on the next Open call.

So, create a static GetConnection() method that returns a new open connection. Use this within a using statement so it can be closed and returned to the connection pool as soon as possible.

using(var cn = Database.GetConnection())
{
    //query your data here, Dapper example below
    cn.Execute("update MyTable set MyField = @newValue", new {newValue});
}

这篇关于一个人应该如何保持一个ASP.NET MVC应用程序数据库连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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