使用语句connection.open [英] using statement with connection.open

查看:227
本文介绍了使用语句connection.open的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一些code和与同事讨论这个问题。

的code,看起来像这样特别的一节。

  [测试]
    公共无效TestNormalWay()
    {
        使用(VAR CN =的getConnection())
        {
            cn.Open();
            //做的东西
        }
    }
 

现在的问题上来:

  

为什么不将cn.Open到getConnection方法。

我说,如果打开抛出一个异常处置不会被调用。他的回答是

  

那么,连接没有被打开,为什么会需要得到   关闭(或处置)?

对于我来说,是不想知道如果还是不行,我需要处理/关闭,所以我会重申其移入共享功能的cn.Open在code,而不是只是一个问题。

这是有趣的...所以我做了一些读数 SQL服务器连接池(ADO.NET)

要我并不清楚,如果存在这样一种情况:要求cn.Open并抛出和异常的处置需要被调用。

因此​​,在我下面的例子是有什么区别真正的TestNormalWay和WhyNotDoItThisWay

之间

 保护静态的DbConnection的getConnection()
    {
        的DbConnection CN =新的SqlConnection(SomeConnecitonstring ......);
        返回CN;
    }

    保护静态的DbConnection GetConnectionDangerousVersion()
    {
        的DbConnection CN =新的SqlConnection(SomeConnecitonstring ......);
        cn.Open(); //这将引发.. .dispose不叫
        返回CN;
    }

    [测试]
    公共无效TestNormalWay()
    {
        使用(VAR CN =的getConnection())
        {
            cn.Open();
            //做的东西
        }
    }

    [测试]
    公共无效WhyNotDoItThisWay()
    {
        使用(VAR CN = GetConnectionDangerousVersion())
        {
            //做的东西
        }
    }
 

解决方案

你写你的code的方式,你永远的需要的尽快,因为它是创造所以打开连接没有什么区别。

不过,你可以打开和关闭连接好几次,在code专做是有很大的区别。

我可能需要写一些code,我有一个长期运行的程序,需要一个连接对象,并随着时间的推移打开和关闭它。该程序可能不会怎么在意连接对象被创建。因此,这是一个优势,创造分开与打开动作的连接,并关闭它的行为。

至于资源管理问题,我会同意这不是一个问题。本身就是创建一个SQL连接对象没有锁定任何资源,它是打开它,其获取连接池的行为。如果打开返回例外,我认为这是合理的假设,该连接未打开。

I was looking at some code and discussing it with co-workers.

Specifically a section of code that looks like this.

    [Test]
    public void TestNormalWay()
    {
        using(var cn = GetConnection())
        {
            cn.Open();
            // do stuff
        }
    }

The question came up:

"why not move the cn.Open into the GetConnection method."

I said that if "Open" throws an exception dispose would not get called. His response was

"So what. The connection wasn't opened so why would it need to get closed (or disposed)?"

For me it is just a matter of not wanting to know if or if not I NEED to dispose/close so I would repeat the cn.Open in the code instead of moving it into the shared function.

BUT it is interesting... so I did some reading at SQL Server Connection Pooling (ADO.NET)

To me it isn't clear if there exists a scenario in which calling cn.Open and it throws and exception where dispose would need to be called.

So in my example below is there any difference really between "TestNormalWay" and "WhyNotDoItThisWay"

    protected static DbConnection GetConnection()
    {
        DbConnection cn = new SqlConnection("SomeConnecitonstring... ");
        return cn; 
    }

    protected static DbConnection GetConnectionDangerousVersion()
    {
        DbConnection cn = new SqlConnection("SomeConnecitonstring... ");
        cn.Open();  // this will throw.. .dispose not called
        return cn; 
    }

    [Test]
    public void TestNormalWay()
    {
        using(var cn = GetConnection())
        {
            cn.Open();
            // do stuff
        }
    }

    [Test]
    public void WhyNotDoItThisWay()
    {
        using(var cn = GetConnectionDangerousVersion())
        {
            // do stuff
        }
    }

解决方案

The way you're writing your code you always want to open the connection as soon as it's created so there is no difference.

However you can open and close a connection several times and in code designed to do that there is a big difference.

I might want to write some code where I have a long running routine that takes a connection object and over time opens and closes it. The routine might not care how the connection object was created. Therefore it's an advantage to separate the act of creating the connection with the act of opening and closing it.

With regards the resource management question I'd agree it's not an issue. Creating an SQL connection object in itself doesn't lock any resources, it's the act of opening it that acquires a pooled connection. If the open returns an exception I think it's reasonable to assume that the connection wasn't opened.

这篇关于使用语句connection.open的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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