数据库连接问题。 [英] Database Connection questions.

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

问题描述

大家好,


我有几个问题,我一直想问。这些都是

都与ADO.net有关,特别是与数据库连接。


1)如果我通过Connection.open打开了与数据库的连接()

方法,我不使用Connection.close()方法,将垃圾收集器

收集连接对象只是因为我不再使用它了。


2)我正在使用数据集,其中我对数据进行了一些修改,并且

将修改后的数据提交回服务器。是否会再次向数据库服务器打开连接

,因为数据集是一个无连接对象。


3)当我说connection.close()时,是连接实际上是

关闭或连接信息是否保留在某个内存区域,只有

等待其他一些connection.open()方法来调用它。


提前感谢

pradeep TP

解决方案

普拉迪普。
< blockquote class =post_quotes>我有几个问题,我一直想问。这些都与ADO.net有关,特别是与数据库的连接。

1)如果我通过Connection.open()
方法打开了与数据库的连接,而且我不使用Connection.close()方法,垃圾收集器会收集连接对象,因为我不再使用它了。


只要有来自

的引用,连接对象就会存在。请参阅后面的答案。

2)我正在使用数据集,其中我对数据进行了一些修改

将修改后的数据提交回服务器。是否将连接再次打开到数据库服务器,因为数据集是一个无连接对象。


数据集无法发送到服务器。 dataadapter正在向服务器发送单独的

行。 dataadapter中的一个操作是打开并关闭连接属性中的连接,如果它是/ /

在启动时尚未打开。




连接关闭,连接对象被声明为globaly(或

,实例是一个dataadapter,它有一个引用它)比对象

留在内存中。


顺便说一句,这是新闻组的典型问题。


Microsoft.public.dotnet.framework.adonet


我希望这会有所帮助,


Cor


> 1)如果我通过Connection.open()

方法打开了与数据库的连接,并且我不使用Connection.close()方法,那么垃圾收集器会收集连接对象只是因为我不再使用它了。


垃圾收集器只会在程序中的任何其他地方没有任何引用

的情况下执行此操作。 GC也是非确定性的,因此在

GC运行之前,连接可能会打开很长时间。最好将你的连接用法放在一个使用语句

中,它会自动自动调用dispose(内部调用

close)或者总是使用finally语句,即


使用(SqlConnection连接=新的SqlConnection())

{

.......

}




SqlConnection connection = null;


试试

{

connection = new SqlConnection();

....

}

终于

{

if(connection!= null)

{

connection.Close();

}

}


3)当我说连接.close()时,连接实际上是否关闭
连接信息是否保持在一些内存区域,只等待其他一些connection.open()方法来调用它。


当您在连接上调用Close或者Dispose底层连接是

返回到连接池时,连接池组连接

一起基于连接字符串,所以当你下次尝试使用相同的连接字符串打开

连接时,你将在

池中获得连接(如果有的话)很快就没有表现出色。

Mark R Dawson
http://www.markdawson.org


" pradeep_TP"写道:

大家好,

我有几个问题,我一直想问。这些都与ADO.net有关,特别是与数据库的连接。

1)如果我通过Connection.open()
方法打开了与数据库的连接,并且我不使用Connection.close()方法,垃圾收集器会收集连接对象只是因为我不再使用它了。

2)我正在使用数据集,我对数据进行了一些修改,并将修改后的数据提交回服务器。是否会再次打开连接到数据库服务器,因为数据集是一个无连接对象。

3)当我说connection.close()时,连接实际上是否正在获得
关闭或连接信息是否保留在某个内存区域,只是等待其他一些connection.open()方法来调用它。

提前感谢
pradeep TP



垃圾收集器只会在你的任何其他地方没有引用任何其他连接的情况下执行此操作程序。 ...




是否引用意味着打开的连接对象保持未闭合状态。我b $ b的意思是,如果在我打开连接对象并且没有关闭

长之后,GC会认为这是未引用的并尝试收集

对象。


" Mark R. Dawson"写道:

1)如果我通过Connection.open()
方法打开了与数据库的连接,我就不用了Connection.close()方法,将垃圾收集器收集连接对象只是因为我不再使用它了。



垃圾收集器只会这样做,如果你这样做没有任何参考
您的程序中的任何其他地方的连接。 GC也是非确定性的,因此在GC运行之前,连接可能会打开很长时间。最好将你的连接用法放在一个using语句中,它将自动调用dispose(内部调用
close)或者总是使用finally语句即

使用(SqlConnection) connection = new SqlConnection())
{
......
}


SqlConnection connection = null;

尝试
{
连接=新的SqlConnection();
....
}
终于
{
if(connection!= null)
{
connection.Close();
}
}

3当我说connection.close()时,连接实际上是关闭还是连接信息保留在某个内存区域,只等待其他一些connection.open()方法来调用它。



当您在连接上调用Close或Dispose将基础连接返回到连接池时,连接池组连接基于连接字符串一起使用,所以下次尝试使用相同的连接字符串打开
连接时,您将在
池中获得连接(如果有可用的话)很快没有性能损失。

Mark R Dawson
http:/ /www.markdawson.org

" pradeep_TP"写道:

大家好,

我有几个问题,我一直想问。这些都与ADO.net有关,特别是与数据库的连接。

1)如果我通过Connection.open()
方法打开了与数据库的连接,并且我不使用Connection.close()方法,垃圾收集器会收集连接对象只是因为我不再使用它了。

2)我正在使用数据集,我对数据进行了一些修改,并将修改后的数据提交回服务器。是否会再次打开连接到数据库服务器,因为数据集是一个无连接对象。

3)当我说connection.close()时,连接实际上是否正在获得
关闭或连接信息是否保留在某个内存区域,只是等待其他一些connection.open()方法来调用它。

提前感谢
pradeep TP



Hi all,

I have a few questions that I have been wanting to ask for long. These are
all related to ADO.net and specifically to conenction to database.

1) If I have opened a connection to a database through Connection.open()
method, and I do not use Connection.close() method, will garbage collector
collect the connection object just because i am not using it any more.

2) I am using a dataset, in which i make some modifications to the data and
submit the modified data back to the server. Will the connection be opened
again to the database server, because dataset is a connectionless object.

3) When i m saying connection.close(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open() method to call it.

thanks in advance
pradeep T.P

解决方案

Pradeep.

I have a few questions that I have been wanting to ask for long. These are
all related to ADO.net and specifically to conenction to database.

1) If I have opened a connection to a database through Connection.open()
method, and I do not use Connection.close() method, will garbage collector
collect the connection object just because i am not using it any more.
The connection object will live as long as there is a reference to or from
it. See later answers as well about this.

2) I am using a dataset, in which i make some modifications to the data
and
submit the modified data back to the server. Will the connection be opened
again to the database server, because dataset is a connectionless object.
The dataset cannot sent to a server. The dataadapter is sending individual
rows to the server. One of the operations from a dataadapter is to open and
close the connection that is in his connection properties if that is/was
not already open when it started.
3) When i m saying connection.close(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open() method to call it.



The connection closes and when the connection object is declared globaly (or
by instance a dataadapter which has a reference to it) than the object
stays in memory.

By the way, this are typical questions for the newsgroup.

Microsoft.public.dotnet.framework.adonet

I hope this helps,

Cor


> 1) If I have opened a connection to a database through Connection.open()

method, and I do not use Connection.close() method, will garbage collector
collect the connection object just because i am not using it any more.
The garbage collector will only do this if you do not have any references to
the connection anywhere else in your program. Also the GC is
non-deterministic so the connection may lie open for a long time before the
GC runs. It is best to put your connection useage inside a using statement
that will automatically call dispose automatically (which internally calls
close) or always use a finally statement i.e.

using(SqlConnection connection = new SqlConnection())
{
.......
}

or
SqlConnection connection = null;

try
{
connection = new SqlConnection();
....
}
finally
{
if(connection != null)
{
connection.Close();
}
}

3) When i m saying connection.close(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open() method to call it.
When you call Close on a connection or Dispose the underlying connection is
returned to the connection pool, the connection pool group connections
together based on the Connection String, so the next time you try to open a
connection with the same connectionstring you will get a connection in the
pool (if there is one available) very quickly without a performance hit.
Mark R Dawson
http://www.markdawson.org

"pradeep_TP" wrote:
Hi all,

I have a few questions that I have been wanting to ask for long. These are
all related to ADO.net and specifically to conenction to database.

1) If I have opened a connection to a database through Connection.open()
method, and I do not use Connection.close() method, will garbage collector
collect the connection object just because i am not using it any more.

2) I am using a dataset, in which i make some modifications to the data and
submit the modified data back to the server. Will the connection be opened
again to the database server, because dataset is a connectionless object.

3) When i m saying connection.close(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open() method to call it.

thanks in advance
pradeep T.P



The garbage collector will only do this if you do not have any references to
the connection anywhere else in your program. ...



Does "references" mean opened connection object remaining unclosed. What i
mean is that if after i open the connection object and does not close it for
long, will the GC consider this as unreferenced and try to collect the
object.

"Mark R. Dawson" wrote:

1) If I have opened a connection to a database through Connection.open()
method, and I do not use Connection.close() method, will garbage collector
collect the connection object just because i am not using it any more.



The garbage collector will only do this if you do not have any references to
the connection anywhere else in your program. Also the GC is
non-deterministic so the connection may lie open for a long time before the
GC runs. It is best to put your connection useage inside a using statement
that will automatically call dispose automatically (which internally calls
close) or always use a finally statement i.e.

using(SqlConnection connection = new SqlConnection())
{
......
}

or
SqlConnection connection = null;

try
{
connection = new SqlConnection();
....
}
finally
{
if(connection != null)
{
connection.Close();
}
}

3) When i m saying connection.close(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open() method to call it.



When you call Close on a connection or Dispose the underlying connection is
returned to the connection pool, the connection pool group connections
together based on the Connection String, so the next time you try to open a
connection with the same connectionstring you will get a connection in the
pool (if there is one available) very quickly without a performance hit.
Mark R Dawson
http://www.markdawson.org

"pradeep_TP" wrote:

Hi all,

I have a few questions that I have been wanting to ask for long. These are
all related to ADO.net and specifically to conenction to database.

1) If I have opened a connection to a database through Connection.open()
method, and I do not use Connection.close() method, will garbage collector
collect the connection object just because i am not using it any more.

2) I am using a dataset, in which i make some modifications to the data and
submit the modified data back to the server. Will the connection be opened
again to the database server, because dataset is a connectionless object.

3) When i m saying connection.close(), is the connection actually getting
close or does the connection information stays in some memory area, only
waiting for some other connection.open() method to call it.

thanks in advance
pradeep T.P



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

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