处置SqlConnection [英] Disposing SqlConnection

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

问题描述



只是想确保我做对了:考虑这个课程:


// ========= ==开始代码=============

类测试

{

private SqlConnection con = null;

public void Connect()

{

con = new SqlConnection(SomeConnectionString);

<做点什么这个连接在这里>

}

}

// ===========结束代码==== =========

如果我理解正确,我不必明确断开连接,如果我使用这个类的对象作为

本地参考,如下:


// ===========开始代码=============

void SomeFunc()

{

测试t =新测试();

t.Connect();

}

// ===========结束代码=============

" T"应该由GC处理,因为对象在t行后被取消引用。连接()

所以从技术上讲,我不需要实现Dispose或创建/调用t.Disconnect,对吗? />

建议将受到高度赞赏!

谢谢,

Andrey

Hi,
Just wanted to make sure i get it right: consider this class:

// =========== START CODE =============
class Test
{
private SqlConnection con = null;
public void Connect()
{
con = new SqlConnection(SomeConnectionString);
<Do something with this connection here>
}
}
// =========== END CODE =============
If i understand right, i don''t HAVE TO disconnect explicitly, if i use an object of this class as a
local reference, like here:

// =========== START CODE =============
void SomeFunc()
{
Test t = new Test();
t.Connect();
}
// =========== END CODE =============
"t" should be disposed by GC as the object gets dereferenced right after the line t.Connect()
So technically i don''t need to implement Dispose or create/call t.Disconnect, right?

Suggestions would be highly appreciated!
Thank you,
Andrey

推荐答案

有什么理由你不想明确关闭你的连接

并在你的班级处理它吗?


没有明确说明关闭连接可能会导致绑定连接

池连接的时间超过了等待GC处理对象时所需的时间。

处理对象。始终明确关闭您的连接。在

中,要关闭连接,您应该考虑通过在使用块中创建它们来控制连接的生命周期和范围:

using(SqlConnection con = new SqlConnection(" connection string))

{

try

{

//在这里做一些关于连接的东西

}

catch(例外情况)

{

}

终于

{

con.Close();

}

}


DalePres

MCAD,MCDBA,MCSE

" MuZZy" <乐******* @ yahoo.com>在留言中写道

新闻:Xu ******************** @ comcast.com ...
Is there any reason you would not want to explicitly close your connection
and dispose of it in your class?

Not explicitly closing your connection can result in tying up connection
pool connections longer than is necessary while you wait for the GC to
dispose of your objects. Always explicitly close your connection. In
addition, to closing your connection, you should consider controlling the
lifetime and scope of your connections by creating them in a using block:

using (SqlConnection con = new SqlConnection("connection string"))
{
try
{
// do some stuff with the connection here
}
catch (Exception ex)
{
}
finally
{
con.Close();
}
}

DalePres
MCAD, MCDBA, MCSE
"MuZZy" <le*******@yahoo.com> wrote in message
news:Xu********************@comcast.com...
你好,
只是想确保我做对了:考虑这个课程:

// ===========开始代码======== =====
类测试
{私有SqlConnection con = null;
public void Connect()
{
con = new SqlConnection(SomeConnectionString );
<在此处使用此连接执行某些操作>
}
}
// =========== END CODE ====== =======

如果我理解正确,我不必明确断开连接,如果我使用此类的
对象作为
本地参考,像这里:

// ===========开始代码=============
void SomeFunc()
{
测试t =新测试();
t.Connect();
}
// =========== END CODE = ============
t应该由GC处理,因为对象在线路t后立即被解除引用。连接()
所以从技术上讲,我不需要实现Dispose或创建/调用
t.Disconnect,对吗?

建议将受到高度赞赏!
谢谢,
Andrey
Hi,
Just wanted to make sure i get it right: consider this class:

// =========== START CODE =============
class Test
{
private SqlConnection con = null;
public void Connect()
{
con = new SqlConnection(SomeConnectionString);
<Do something with this connection here>
}
}
// =========== END CODE =============
If i understand right, i don''t HAVE TO disconnect explicitly, if i use an
object of this class as a
local reference, like here:

// =========== START CODE =============
void SomeFunc()
{
Test t = new Test();
t.Connect();
}
// =========== END CODE =============
"t" should be disposed by GC as the object gets dereferenced right after
the line t.Connect()
So technically i don''t need to implement Dispose or create/call
t.Disconnect, right?

Suggestions would be highly appreciated!
Thank you,
Andrey



DalePres< don-t-spa-m-me@lea-ve-me-a-lone--.com>写道:
DalePres <don-t-spa-m-me@lea-ve-me-a-lone--.com> wrote:
有什么理由你不想明确关闭你的连接
并在你的班级处理它?<​​br />
没有明确关闭你的连接可能会导致在等待GC处理对象时,连接池连接的时间超过了必要的时间。始终明确关闭您的连接。


否 - 如果您不想再次打开连接,那么处理连接就足够了。

In此外,要关闭连接,您应该考虑通过在使用块中创建它们来控制连接的生命周期和范围:
Is there any reason you would not want to explicitly close your connection
and dispose of it in your class?

Not explicitly closing your connection can result in tying up connection
pool connections longer than is necessary while you wait for the GC to
dispose of your objects. Always explicitly close your connection.
No - disposing the connection is good enough, if you don''t want to
reopen it later.
In addition, to closing your connection, you should consider controlling the
lifetime and scope of your connections by creating them in a using block:




我会使用它优先于手动调用Close。


-

Jon Skeet - < sk *** @ pobox.com>
< a rel =nofollowhref =http://www.pobox.com/~skeettarget =_ blank> http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



I''d use that in preference to manually calling Close.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


DalePres写道:
DalePres wrote:
有什么理由你不想明确关闭你的连接
并在你的班级处理它?<​​br />
没有明确关闭你的连接会导致连接比你等待GC时所需的池连接更长处理你的目标TS。始终明确关闭您的连接。另外,要关闭连接,您应该考虑通过在使用块中创建它们来控制连接的生命周期和范围:

使用(SqlConnection con = new SqlConnection) (连接字符串))
{
尝试
{
//用连接做一些事情
}
catch(Exception ex)
{
}
终于
{
con.Close();
}
}

DalePres < MCAD,MCDBA,MCSE

" MuZZy" <乐******* @ yahoo.com>在消息中写道
新闻:Xu ******************** @ comcast.com ...
Is there any reason you would not want to explicitly close your connection
and dispose of it in your class?

Not explicitly closing your connection can result in tying up connection
pool connections longer than is necessary while you wait for the GC to
dispose of your objects. Always explicitly close your connection. In
addition, to closing your connection, you should consider controlling the
lifetime and scope of your connections by creating them in a using block:

using (SqlConnection con = new SqlConnection("connection string"))
{
try
{
// do some stuff with the connection here
}
catch (Exception ex)
{
}
finally
{
con.Close();
}
}

DalePres
MCAD, MCDBA, MCSE
"MuZZy" <le*******@yahoo.com> wrote in message
news:Xu********************@comcast.com...
你好,
只是想确保我做对了:考虑这个课程:

// ===========开始代码======== =====
类测试
{私有SqlConnection con = null;
public void Connect()
{
con = new SqlConnection(SomeConnectionString );
<在此处使用此连接执行某些操作>
}
}
// =========== END CODE ====== =======

如果我理解正确,我不必明确断开连接,如果我使用此类的
对象作为
本地参考,像这里:

// ===========开始代码=============
void SomeFunc()
{
测试t =新测试();
t.Connect();
}
// =========== END CODE = ============
t应该由GC处理,因为对象在线路t后立即被解除引用。连接()
所以从技术上讲,我不需要实现Dispose或创建/调用
t.Disconnect,对吗?

建议将不胜感激!
谢谢,
安德烈
Hi,
Just wanted to make sure i get it right: consider this class:

// =========== START CODE =============
class Test
{
private SqlConnection con = null;
public void Connect()
{
con = new SqlConnection(SomeConnectionString);
<Do something with this connection here>
}
}
// =========== END CODE =============
If i understand right, i don''t HAVE TO disconnect explicitly, if i use an
object of this class as a
local reference, like here:

// =========== START CODE =============
void SomeFunc()
{
Test t = new Test();
t.Connect();
}
// =========== END CODE =============
"t" should be disposed by GC as the object gets dereferenced right after
the line t.Connect()
So technically i don''t need to implement Dispose or create/call
t.Disconnect, right?

Suggestions would be highly appreciated!
Thank you,
Andrey




嗯,故事是我接管了一个已经开发/维护/出售的项目,为两年半的b $ b。现在除了做一些热修复和项目的一些新开发之外,我还要调查一些内存问题。


我在这里看到的一件事是在不同的类中创建了多个连接。

以某种方式复合这个我将tose类的对象移动到本地范围,但是没有办法我可以添加一个b $ b处理它们 - 太多的地方要添加它,这需要我大约一年

这样做,这是显而易见的,这是不可接受的。我只需要确保我已经做到了最好的




所以问这个问题我想确保不应该''离开GC的'

连接会有很多问题。

我不认为如果连接池将会很重要捆绑,因为我看到在任何时间点从我的机器到SQL服务器的并发连接不超过5-10个 - 我想SQL服务器

可以管理多个连接很好。



Well, the story is that i took over a project which is already being developed/maintained/SOLD for
two and a half years. Right now besides doing hot fixes and some new development on the project, i
also investigate some memory problems.

And one thing that i see here is that multiple connections are being created in different classes.
To somehow compinsate this i move objects of tose classes to the local scope, but there is no way i
could be adding a Dispose for them - too many places to add it to, and it would take me about a year
to do that, which is obviousely not acceptable. I just need to make sure that i''ve done the best
possible.

So asking this question i wanted to make sure that there shouldn''t be much problems with leaving the
connections on GC''s will :)
I don''t think it''s big deal if th econnections pool will be tied, as i see no more than 5-10
concurrent connections from my machine to the SQL server at any point of time - i guess SQL server
can manage multiple connections fine.


这篇关于处置SqlConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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