调用SMO服务器和数据库后清理 [英] Cleaning up after calls to SMO Server and Database

查看:193
本文介绍了调用SMO服务器和数据库后清理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么做SMO释放它的连接?

How do you make SMO release it's connections?

我有这样的code:

public static class SqlServerConnectionFactory
{
    public static Server GetSmoServer()
    {
        using (var c = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString))
        {
            var s = new ServerConnection(c);
            c.Close();
            return new Server(s);
        }
    }

    public static Database GetSmoDatabase(Server server)
    {
        var db = server.Databases[ConfigurationManager.AppSettings["Database"]];
        db.AutoClose = true;
        return db;
    }
}

这样调用从IIS中的ASP.Net MVC应用程序运行...

Called like this from an ASP.Net MVC app run in IIS...:

public ActionResult Index()
    {
        server = SqlServerConnectionFactory.GetSmoServer();
        database = SqlServerConnectionFactory.GetSmoDatabase(server);
        var vm = new SettingsIndexViewmodel(database);
        return View(vm);
    }

有关每一个电话我对这个指数方法连接起来纺 - 而不是再次释放

For every call I make to this index method a connection is spun up - and is not released again.

所以,20后调用的页面,我已经等待命令连接的20。这最终有一个例外,当我不能让新的连接结束,因为连接池已满。

So after 20 calls to the page, I have 20 of the connections awaiting command. This eventually ends up with an exception, when I cannot make new connections, because the connection pool is full.

我需要做什么来避免这种情况的发生?我似乎无法找到像处置,关闭或类似的SMO服务器对象的一个​​方法。

What do I need to avoid this happening? I cannot seem to find a method on the SMO Server object like Dispose, close or similar.

推荐答案

MSDN文章的 从SQL Server实例断开 可能会提供一些​​帮助。它指出:

The MSDN article Disconnecting from an Instance of SQL Server might offer some help. It states that:

在该<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.common.connectionmanager.connect%28v=SQL.100%29.aspx\">Connect方法被调用,所述
  连接不是自动
  释放。在<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.common.connectionmanager.disconnect%28v=SQL.100%29.aspx\">Disconnect方法必须
  显式调用以释放
  连接的连接池。
  此外,您还可以要求非池
  连接。您可以通过设置做到这一点
  <一href=\"http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.common.connectionsettings.nonpooledconnection%28v=SQL.100%29.aspx\">NonPooledConnection财产
  <一href=\"http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.server.connectioncontext%28v=SQL.100%29.aspx\">ConnectionContext属性,
  引用<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.common.serverconnection%28v=SQL.100%29.aspx\">ServerConnection对象

When the Connect method is called, the connection is not automatically released. The Disconnect method must be called explicitly to release the connection to the connection pool. Also, you can request a non-pooled connection. You do this by setting the NonPooledConnection property of the ConnectionContext property that references the ServerConnection object

这篇关于调用SMO服务器和数据库后清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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