在构造函数中打开数据库连接? [英] open a db connection in a constructor ?

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

问题描述

<。> .net 1.1应用程序有一个类,其构造函数打开一个数据库连接到
sql svr两千。这个类有十几种方法。

他们中的大多数都不做db的东西。


我想知道这个设计是否会是一个问题,bcoz每次

这个类被实例化,db conn是开放的。最糟糕的是

我没有看到db conn关闭的代码中的任何地方。


我写的关于这个bcoz我看到这个应用程序在数据库中留下不止一个

百个开放连接,该应用程序抱怨

conn池已满。


我认为首先,每个连接都需要关闭。第二,

我不认为在类的构造函数中打开db conn是个好主意,除非类的每个方法都需要访问

db。我想听听OO大师的意见。谢谢。


- 从我的手机输入。

解决方案

开7月1日上午5:38,作者< gnewsgr ... @ gmail.com写道:


< snip>


我想首先,每个连接都需要关闭。第二,

我不认为在类的构造函数中打开db conn是个好主意,除非类的每个方法都需要访问

db。我想听听OO大师的意见。谢谢。



听起来不错。我几乎从未以数据库连接作为

字段结束 - 它总是一个局部变量,在

中引入使用声明(因此自动关闭)。如果它是*在
a字段中*,该类型实现IDisposable并在对象本身被处置时处理连接




Jon


看起来像开发人员的另一个例子是他们自己太聪明了

good。


从不关闭数据库连接=可怕。


在构造中打开数据库连接,我强烈的个人观点是

这不是一个好习惯,即使你关闭它们。


我的建议(我相信这很典型)

OPEN LATE

快速使用

关闭很快


...........


您可以查看这个1.1示例对于Controller / Manager类的想法,

从业务对象中分离对DAL的调用。

http://sholliday.spaces.live.com/Blog /cns!A68482B9628A842A!139.entry

但是在另外1个误导之外(没有关闭你的IDataReader' s $ / b $ b顺便说一下,你已经列出了编写db调用IMHO的3种最差方法中的2种。


...

app抱怨conn pool已经满了

是的。这正是特征。以前的开发人员编码为

你。


" Author" < gn ******** @ gmail.com写信息

新闻:e3 ********************** ************ @ z66g2000 hsc.googlegroups.com ...


> a .net 1.1 app有一个类的构造函数打开一个数据库连接到

sql svr两千。这个类有十几种方法。

他们中的大多数都不做db的东西。


我想知道这个设计是否会是一个问题,bcoz每次

这个类被实例化,db conn是开放的。最糟糕的是

我没有看到db conn关闭的代码中的任何地方。


我写的关于这个bcoz我看到这个应用程序在数据库中留下不止一个

百个开放连接,该应用程序抱怨

conn池已满。


我认为首先,每个连接都需要关闭。第二,

我不认为在类的构造函数中打开db conn是个好主意,除非类的每个方法都需要访问

db。我想听听OO大师的意见。谢谢。


- 用我的手机输入。



7月1日,2:21 * am,Jon Skeet [C#MVP]" < sk ... @ pobox.comwrote:


7月1日上午5:38,作者< gnewsgr ... @ gmail.comwrote:


< snip>


我认为首先,每个连接都需要关闭。 *其次,

我不认为在构造函数中打开db conn是个好主意

类,除非每个类的方法都是如此需要访问

db。我想听听OO大师的意见。 *谢谢。



听起来不错。我几乎从未以数据库连接作为

字段结束 - 它总是一个局部变量,在

中引入使用声明(因此自动关闭)。如果它是*在
a字段中*,该类型实现IDisposable并在对象本身被处置时处理连接




Jon



非常感谢。是的,db连接对象/被定义为

类的私有字段。现在,关于你的上一个声明


< quote>

如果*是*在一个字段中,该类型实现IDisposable并处理

当对象本身被处理时的连接。

< / quote>


与不实现IDisposable有什么不同但是

明确处理/关闭连接?


BTW,下面是该类的构造函数(用VB编写)我已经

谈论。


...... ...

私人骗局作为SqlConnection


Public Sub New( )

Me.cntString = ConfigurationSettings.AppSettings.Item(" cntString")

Me.con = New SqlConnection

Me.con。 ConnectionString = Me.cntString

Me.cmd =新的SqlCommand

Me.cmd.Connection = Me.con

Me.con.Open / /看,数据库连接在这里打开。

结束子

......


a .net 1.1 app has a class whose constructor opens a db connection to
sql svr two thousand. this class has more than a dozen of methods.
most of them don''t do db stuff.

I am wondering if this design is going to be a problem, bcoz each time
this class is instantiated, a db conn is open. The worst thing is that
I haven''t seen anywhere in the code the db conn is closed.

I write about this bcoz I see that this app leaves more than one
hundred open connections in the db, and the app complains that the
conn pool is full.

I think first of all, each connection needs to be closed. secondly,
I don''t think it is a good idea to open a db conn in the constructor
of a class, unless each and every method of the class needs to visit
the db. I would like to hear you OO gurus'' opinion. thank you.

-- typed up from my cell phone.

解决方案

On Jul 1, 5:38 am, Author <gnewsgr...@gmail.comwrote:

<snip>

I think first of all, each connection needs to be closed. secondly,
I don''t think it is a good idea to open a db conn in the constructor
of a class, unless each and every method of the class needs to visit
the db. I would like to hear you OO gurus'' opinion. thank you.

Sounds about right. I almost never end up with a db connection as a
field - it''s alost always always a local variable, introduced in a
"using" statement (and therefore automatically closed). If it *is* in
a field, the type implements IDisposable and disposes the connection
when the object itself is disposed.

Jon


Looks like another example of a developer being too clever for their own
good.

never closing a db connection = HORRIBLE.

opening a db connection in a construction , my strong personal opinion is
that this is not a good practice, even if you close them.

my advice (which is pretty typical I believe)
OPEN LATE
USE Quickly
Close SOON

...........

You can check this 1.1 example for the idea of a Controller/Manager class,
which seperates calls to the DAL from the business objects.
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!139.entry
But outside of the 1 other miscue (which is not closing your IDataReader''s
btw), you''ve listed 2 of the 3 worst ways to code up db calls IMHO.

...
"app complains that the conn pool is full"
Yep. That is exactly the "feature" the previous developer has coded for
you.

"Author" <gn********@gmail.comwrote in message
news:e3**********************************@z66g2000 hsc.googlegroups.com...

>a .net 1.1 app has a class whose constructor opens a db connection to
sql svr two thousand. this class has more than a dozen of methods.
most of them don''t do db stuff.

I am wondering if this design is going to be a problem, bcoz each time
this class is instantiated, a db conn is open. The worst thing is that
I haven''t seen anywhere in the code the db conn is closed.

I write about this bcoz I see that this app leaves more than one
hundred open connections in the db, and the app complains that the
conn pool is full.

I think first of all, each connection needs to be closed. secondly,
I don''t think it is a good idea to open a db conn in the constructor
of a class, unless each and every method of the class needs to visit
the db. I would like to hear you OO gurus'' opinion. thank you.

-- typed up from my cell phone.



On Jul 1, 2:21*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:

On Jul 1, 5:38 am, Author <gnewsgr...@gmail.comwrote:

<snip>

I think first of all, each connection needs to be closed. * secondly,
I don''t think it is a good idea to open a db conn in the constructor
of a class, unless each and every method of the class needs to visit
the db. I would like to hear you OO gurus'' opinion. *thank you.


Sounds about right. I almost never end up with a db connection as a
field - it''s alost always always a local variable, introduced in a
"using" statement (and therefore automatically closed). If it *is* in
a field, the type implements IDisposable and disposes the connection
when the object itself is disposed.

Jon

Thank you very much. Yes, the db connection object was/is defined as
a private field of the class. Now, regarding your last statement

<quote>
If it *is* in a field, the type implements IDisposable and disposes
the connection when the object itself is disposed.
</quote>

How is this different from not implementing IDisposable but do
explicitly dispose/close the connection?

BTW, below is the constructor of the class (written in VB) I''ve been
talking about.

... ...
Private con As SqlConnection

Public Sub New()
Me.cntString = ConfigurationSettings.AppSettings.Item("cntString" )
Me.con = New SqlConnection
Me.con.ConnectionString = Me.cntString
Me.cmd = New SqlCommand
Me.cmd.Connection = Me.con
Me.con.Open // Look, the db connection is open here.
End Sub
......


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

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