关闭类析构函数中的数据库连接 [英] Closing database connections in a class destructor

查看:65
本文介绍了关闭类析构函数中的数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:


我的基类中有一个析构函数,它应该在发布类时关闭数据库

连接。


~BaseTable()

{

this.Conn.Close();

}


但是,连接永远不会关闭。它似乎没有出现析构函数。为什么?


谢谢,

查理

Hi:

I have a destructor in my base class that is supposed to close a database
connection when class is released.

~BaseTable()
{
this.Conn.Close();
}

However, connection is never closed. It doesn''t appear destructor is
firing. Why?

Thanks,
Charlie

推荐答案

" Charlie" < CF ******* @ radiustravel.com>在留言中写道

新闻:e1 ************* @ TK2MSFTNGP09.phx.gbl ...
"Charlie" <cf*******@radiustravel.com> wrote in message
news:e1*************@TK2MSFTNGP09.phx.gbl...
我有我的基类中的析构函数,应该在发布类时关闭数据库
连接。

~BaseTable()
这个.Conn.Close( );
}
然而,连接永远不会关闭。它似乎没有出现析构函数。为什么?
I have a destructor in my base class that is supposed to close a database
connection when class is released.

~BaseTable()
{
this.Conn.Close();
}

However, connection is never closed. It doesn''t appear destructor is
firing. Why?




1)在你完成课程实例后,你是否将它设置为
为空?如果是这样,没有别的东西正在使用它,GC应该收集它,

导致你的析构函数被解雇......



1) After you''ve finished with the instance of the class, are you setting it
to null? If so, and nothing else is using it, the GC should collect it,
causing your destructor to fire...


为什么你认为它没有关闭? AFAIK析构函数仅在

垃圾收集器执行时运行。你不确定它什么时候运行。

如果你需要确保它及时关闭,请直接致电Conn.Close

。 br />

克里斯


" Charlie" < CF ******* @ radiustravel.com>在消息中写道

新闻:e1 ************* @ TK2MSFTNGP09.phx.gbl ...
Why do you think it isn''t closed? AFAIK the destructor only runs when the
garbage collector does its thing. You won''t know for sure when it gets run.
If you need to be sure it closes in a timely manner, call the Conn.Close
directly.

Chris

"Charlie" <cf*******@radiustravel.com> wrote in message
news:e1*************@TK2MSFTNGP09.phx.gbl...
嗨:

我的基类中有一个析构函数,它应该在发布类时关闭数据库
连接。

~BaseTable()
{
this.Conn.Close();
}
然而,连接永远不会关闭。它似乎没有出现析构函数。为什么?

谢谢,
查理
Hi:

I have a destructor in my base class that is supposed to close a database
connection when class is released.

~BaseTable()
{
this.Conn.Close();
}

However, connection is never closed. It doesn''t appear destructor is
firing. Why?

Thanks,
Charlie



查理,


运行时控制它何时调用析构函数。关闭资源时需要考虑

。也许你可以实现

IDisposable接口并关闭那里的连接。


" Charlie"写道:
Charlie,

The runtime controls when it calls the destructor. You need to take that
into consideration when closing your resources. Maybe you can implement
IDisposable interface and close the connection there.

"Charlie" wrote:
嗨:

我的基类中有一个析构函数,它应该在释放类时关闭数据库
连接。 br />
~BaseTable()
{
this.Conn.Close();


然而,连接永远不会关闭。它似乎没有出现析构函数。为什么?

谢谢,
查理
Hi:

I have a destructor in my base class that is supposed to close a database
connection when class is released.

~BaseTable()
{
this.Conn.Close();
}

However, connection is never closed. It doesn''t appear destructor is
firing. Why?

Thanks,
Charlie



这篇关于关闭类析构函数中的数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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