473,378 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

open a db connection in a constructor ?

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.

Jul 1 '08 #1
20 5534
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
Jul 1 '08 #2
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.

Jul 1 '08 #3
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
......
Jul 1 '08 #4
On Jul 1, 5:04*am, "sloan" <sl...@ipass.netwrote:
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.
Thank you for the reference, I'll definitely take a look.

I whole-heartedly agree with your principles.
OPEN LATE
USE Quickly
Close SOON
I would like to add one more to it, but I am not sure if it is
considered a good practice. That is, retrieve as much data as needed
in smallest possible number of db trips.

For example, if I know that I will populate 3 different controls on a
web page with data from 3 different queries, shouldn't I execute these
3 queries in a batch and put the result sets in a DataSet object,
close the connection and then only work with the in-memory dataset?
Or should I make 3 different db trips, each of which only executes 1
query?

Jul 1 '08 #5
On Jul 1, 5:04*am, "sloan" <sl...@ipass.netwrote:
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.
I just looked into the compiled dll (because I don't have the source
code of the class) using Reflector.Net, and I did find that the class
overrides the Finalize method of the ultimate ancestor Object, like
so:

Protected Overrides Sub Finalize()
Try
MyBase.Finalize
If Not Information.IsNothing(Me.dataReader) Then
Me.dataReader = Nothing
End If
If Not Information.IsNothing(Me.cmd) Then
Me.cmd = Nothing
End If
If Not Information.IsNothing(Me.con) Then
Me.con.Close
Me.con = Nothing
End If
Catch exception1 As Exception
ProjectData.SetProjectError(exception1)
Dim exception As Exception = exception1
ProjectData.ClearProjectError
End Try
End Sub

So, it does attempts to close the connection in this Finalize method.
However, if the garbage collector isn't quick enough to collect the
instantiated object of this class, the connection won't be closed in a
timely manner, thus making the application shaky.
Jul 1 '08 #6
On Jul 1, 2:05*pm, Author <gnewsgr...@gmail.comwrote:
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?
If the database connection is in a field, I'd expect it to stay open
while the object is usable. What's the point of keeping it around if
it's going to be closed? On the other hand, I suppose that's sort of
what LINQ to SQL does... but in that case it shouldn't be opened in
the constructor.

Jon
Jul 1 '08 #7
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
I prefer the first option, the connection is declared inside the
method surrounded in a using statement. This assure you that the
conenction will be closed ASAP
Jul 1 '08 #8
On Jul 1, 10:04*am, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jul 1, 2:05*pm, Author <gnewsgr...@gmail.comwrote:
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?

If the database connection is in a field, I'd expect it to stay open
while the object is usable. What's the point of keeping it around if
it's going to be closed? On the other hand, I suppose that's sort of
what LINQ to SQL does... but in that case it shouldn't be opened in
the constructor.

Jon
So, even if we have a class that implements IDisposable, we still need
to close the connection, right?

In other words, it is *not* the case that once a class implements
IDisposable, then the db connection wherein is automatically closed
when the object of the class is disposed. Correct?
Jul 1 '08 #9
On Jul 1, 10:40*am, "Ignacio Machin ( .NET/ C# MVP )"
<ignacio.mac...@gmail.comwrote:
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

I prefer the first option, the connection is declared inside the
method surrounded in a using statement. This assure you that the
conenction will be closed ASAP
Talking about the using statement block, we normally have a quite a
few disposable objects to cooperate in doing db work. For example,
SqlConnection, SqlCommand, SqlDataAdapter or SqlDataReader, DataSet
etc.

So, is it pretty common to do the following with all 4 using
statements?

using (SqlConnection connection = new
SqlConnection(myConnectionString))
using (SqlCommand command = new SqlCommand(mySqlQuery, connection))
using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
using (DataSet dataSet = new DataSet())
{
command.CommandType = CommandType.Text;
connection.Open();
dataAdapter.Fill(dataSet);
}
Jul 1 '08 #10
Author <gn********@gmail.comwrote:
If the database connection is in a field, I'd expect it to stay open
while the object is usable. What's the point of keeping it around if
it's going to be closed? On the other hand, I suppose that's sort of
what LINQ to SQL does... but in that case it shouldn't be opened in
the constructor.

So, even if we have a class that implements IDisposable, we still need
to close the connection, right?

In other words, it is *not* the case that once a class implements
IDisposable, then the db connection wherein is automatically closed
when the object of the class is disposed. Correct?
You would make the Dispose method close the connection.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 1 '08 #11
sloan wrote:
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
Hi - I have looked at the above link, and I have a couple of questions
about his use of IDataReader.

For example, he has a Data-Access-Layer class like this:
public class CustomerData
{
...
public IDataReader CustomersGetAllReader()
{
return
Microsoft.ApplicationBlocks.Data.SqlHelper.Execute Reader(m_connectionStr
ing, this.PROC_CUSTOMERS_GET_ALL, null);
}
...
}

Is it good practice to return an IDataReader like this?

One thing is, a "data-reader" is a database-centric object isn't it? So
if a business-layer class calls this data-layer class you suddenly have
database specific things being returned up to the business layer.

Also, does an IDataReader hold a connection open to the database?
Thanks,
Peter
Jul 2 '08 #12

Well, it is the DataAccessLayer, which talks to a database.

You notice I return an IDataReader, not a specific one. Thus I can swap it
out to a
SqlDataReader
OracleDataReader
MySqlDataReader
OleDBDataReader
and it doesn't affect the business layer code at at all.

See my other blog entry "multiple rdbms with the factory pattern" if you
want to see how to have the DAL support multiple databases in a non hacky
fashion.

Back to your question.
The IDataReader NEEDS TO BE CLOSED AFTER ITS CONSUMED. Thus you'll see my
BAL calling the dataReader.Close() method.
The EnterpriseLibrary and/or the DAAB 2.0 sets up a automatic "close the
connection when the datareader is closed" call for you.
But you need to use/consume the IDataReader at some point. If you close it
in the DAL, it becomes worthless to you in the BAL.
This open IDataReader from the DAL is the only hole that the
EnterpriseLibrary.Data (or DAAB 2.0 ) cannot protect you from. You gotta
handle that yourself.

I think its a good practice. Since I've went almost 100% BusinessObjects,
the IDataReader gives you the best performance.
That code is tweaked to give the best performance. I even use the
"Layout"'s to have hard ordinal constants to avoid a look up penalty like
int i = sqlDataReader["EmpID"]; //
As in I don't incur the penalty of the EmpID lookup. Ordinal numbers
(0,1,2,etc) have been faster for lookup even back in ADO days.
But my method with the Layout....gives you readability as well. I thank an
old colleague for this idea.

You could return back (strong)DataSets from the DAL. There is nothing wrong
about that. But DataSets are slightly less performance abled then an
IDataReader.
Just google "DataReader, DataSet Performance".

.........

I suggest that you CAN return IDataReaders to the BAL. Let the BAL
use/consume them ... as fast as it can...and close them.
I do NOT like the BAL exposing IDataReaders to the PresentationLayer. Why?
Your page developers are more likely to forget to close them.
And it's better to give them the BusinessObjects and BusinessObject
collections to work with I believe.

...


"Peter" <xd****@hotmail.comwrote in message
news:OG**************@TK2MSFTNGP05.phx.gbl...
sloan wrote:
>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

Hi - I have looked at the above link, and I have a couple of questions
about his use of IDataReader.

For example, he has a Data-Access-Layer class like this:
public class CustomerData
{
...
public IDataReader CustomersGetAllReader()
{
return
Microsoft.ApplicationBlocks.Data.SqlHelper.Execute Reader(m_connectionStr
ing, this.PROC_CUSTOMERS_GET_ALL, null);
}
...
}

Is it good practice to return an IDataReader like this?

One thing is, a "data-reader" is a database-centric object isn't it? So
if a business-layer class calls this data-layer class you suddenly have
database specific things being returned up to the business layer.

Also, does an IDataReader hold a connection open to the database?
Thanks,
Peter

Jul 2 '08 #13
sloan wrote:
>
Well, it is the DataAccessLayer, which talks to a database.

You notice I return an IDataReader, not a specific one. Thus I can
swap it out to a SqlDataReader
OracleDataReader
MySqlDataReader
OleDBDataReader
and it doesn't affect the business layer code at at all.

See my other blog entry "multiple rdbms with the factory pattern" if
you want to see how to have the DAL support multiple databases in a
non hacky fashion.

Back to your question.
The IDataReader NEEDS TO BE CLOSED AFTER ITS CONSUMED. Thus you'll
see my BAL calling the dataReader.Close() method. The
EnterpriseLibrary and/or the DAAB 2.0 sets up a automatic "close the
connection when the datareader is closed" call for you. But you need
to use/consume the IDataReader at some point. If you close it in the
DAL, it becomes worthless to you in the BAL. This open IDataReader
from the DAL is the only hole that the EnterpriseLibrary.Data (or
DAAB 2.0 ) cannot protect you from. You gotta handle that yourself.

I think its a good practice. Since I've went almost 100%
BusinessObjects, the IDataReader gives you the best performance.
That code is tweaked to give the best performance. I even use the
"Layout"'s to have hard ordinal constants to avoid a look up penalty
like int i = sqlDataReader["EmpID"]; // As in I don't incur the
penalty of the EmpID lookup. Ordinal numbers (0,1,2,etc) have been
faster for lookup even back in ADO days. But my method with the
Layout....gives you readability as well. I thank an old colleague
for this idea.

You could return back (strong)DataSets from the DAL. There is
nothing wrong about that. But DataSets are slightly less performance
abled then an IDataReader. Just google "DataReader, DataSet
Performance".

........

I suggest that you CAN return IDataReaders to the BAL. Let the BAL
use/consume them ... as fast as it can...and close them. I do NOT
like the BAL exposing IDataReaders to the PresentationLayer. Why?
Your page developers are more likely to forget to close them. And
it's better to give them the BusinessObjects and BusinessObject
collections to work with I believe.
Ok. I've always looked at it from the point of view that the Data
Access Layer communicates with the database (or whatever the datasource
is), and it is only in this layer that database specific classes and
interfaces are used. I consider IDataReader and the concrete
implementations you mention to be database specific, and they have
therefore no business showing up in the Business Layer.

I would usually transform the IDataReader into specific business data
classes in the Data Access Layer, and return them up to the Business
Layer.

I can see you do it differently - transforming the database classes to
business data classes in the Business Layer.

I am still not convinced it is a good idea to rely on the Business
Layer to close the connections/readers opened in the Data Access
Layer...
/Peter
Jul 3 '08 #14
Author wrote:
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.
The open + do + close paradigm by others is the way to go.

If you keep connections open, then your app will not scale well,
because you will run out of connections at the database server.

As another general rule try not to do too much in the constructor. A
constructor is expected to bring the object in a consistent state. It
is not expected to do a lot of things besides that.

Arne
Jul 3 '08 #15
sloan wrote:
Well, it is the DataAccessLayer, which talks to a database.

You notice I return an IDataReader, not a specific one. Thus I can swap it
out to a
SqlDataReader
OracleDataReader
MySqlDataReader
OleDBDataReader
and it doesn't affect the business layer code at at all.
True. But you can only switch database - you can not switch to
XML files or some custom native stuff.

Return a collection of a custom class is a level higher at
the encapsulation scale.
See my other blog entry "multiple rdbms with the factory pattern" if you
want to see how to have the DAL support multiple databases in a non hacky
fashion.
That comes with .NET 2.0 ...

Arne
Jul 3 '08 #16
Arne Vajhøj wrote:
sloan wrote:
Well, it is the DataAccessLayer, which talks to a database.

You notice I return an IDataReader, not a specific one. Thus I can
swap it out to a SqlDataReader
OracleDataReader
MySqlDataReader
OleDBDataReader
and it doesn't affect the business layer code at at all.

True. But you can only switch database - you can not switch to
XML files or some custom native stuff.
This is correct of course, but I thought the bigger "problem" was that
the data layer relies on the business layer to close the connection to
the database.

However, this is also recommended practice from Microsoft. For example,
at
http://msdn.microsoft.com/en-us/library/ms978496.aspx
one can read:

<quote>
Returning Data Readers As Outputs

The advantage of this option is as follows:

Performance. There is a performance benefit when you need to render
data quickly and you can deploy your Data Access Logic Component with
the presentation tier code.

The disadvantage of this option is as follows:

Remoting. It is inadvisable to use data readers in remoting scenarios,
because of the potential for client applications to hold the database
connection open for lengthy periods.
</quote>

/Peter

Jul 4 '08 #17
Peter wrote:
Arne Vajhøj wrote:
>sloan wrote:
>>Well, it is the DataAccessLayer, which talks to a database.

You notice I return an IDataReader, not a specific one. Thus I can
swap it out to a SqlDataReader
OracleDataReader
MySqlDataReader
OleDBDataReader
and it doesn't affect the business layer code at at all.
True. But you can only switch database - you can not switch to
XML files or some custom native stuff.

This is correct of course, but I thought the bigger "problem" was that
the data layer relies on the business layer to close the connection to
the database.
Not in my opinion.
However, this is also recommended practice from Microsoft. For example,
at
http://msdn.microsoft.com/en-us/library/ms978496.aspx
one can read:

<quote>
Returning Data Readers As Outputs

The advantage of this option is as follows:

Performance. There is a performance benefit when you need to render
data quickly and you can deploy your Data Access Logic Component with
the presentation tier code.

The disadvantage of this option is as follows:

Remoting. It is inadvisable to use data readers in remoting scenarios,
because of the potential for client applications to hold the database
connection open for lengthy periods.
</quote>
I disagree with it being best practice for the reasons mentioned
previously.

I agree that data readers perform well.

I am not quite sure about their remoting remark. Data readers
are not serializable, so they can not be send to the client. And
it is not obvious to me why the BLL-DAL-DB-DAL-BLL trip should
take longer depending on whether the PL is remote or local.

Arne

Jul 5 '08 #18
Arne Vajhøj wrote:
Peter wrote:
Arne Vajhøj wrote:
sloan wrote:
Well, it is the DataAccessLayer, which talks to a database.

You notice I return an IDataReader, not a specific one. Thus I
can swap it out to a SqlDataReader
OracleDataReader
MySqlDataReader
OleDBDataReader
and it doesn't affect the business layer code at at all.
True. But you can only switch database - you can not switch to
XML files or some custom native stuff.
This is correct of course, but I thought the bigger "problem" was
that the data layer relies on the business layer to close the
connection to the database.

Not in my opinion.
In your opinion is it a "problem" at all, or is it ok to do this?

What I don't like about it is that (1) the connection to the database
is now open until the business layer closes it; and (2) the business
layer has to have knowledge of the data layer (and data source) to be
able to extract the data, either by use of column names or indices -
meaning the business layer has to have knowledge of the sql which
extracted the data in the first place, and my opinion is that this
knowledge belongs in the data layer, not the business layer.

But it *is* recommeded by Microsoft - so it could be it is a good way
to get data. Seems pointless to have a business layer though.
Jul 9 '08 #19
Peter wrote:
Arne Vajhøj wrote:
>Peter wrote:
>>Arne Vajhøj wrote:
sloan wrote:
Well, it is the DataAccessLayer, which talks to a database.
>
You notice I return an IDataReader, not a specific one. Thus I
can swap it out to a SqlDataReader
OracleDataReader
MySqlDataReader
OleDBDataReader
and it doesn't affect the business layer code at at all.
True. But you can only switch database - you can not switch to
XML files or some custom native stuff.
This is correct of course, but I thought the bigger "problem" was
that the data layer relies on the business layer to close the
connection to the database.
Not in my opinion.

In your opinion is it a "problem" at all, or is it ok to do this?
I would be willing to do it for smaller projects. Not for the
big stuff.

But remember: free advice is worth every cent you pay for it ! :-)
What I don't like about it is that (1) the connection to the database
is now open until the business layer closes it;
Not a serious problem in my opinion.
and (2) the business
layer has to have knowledge of the data layer (and data source) to be
able to extract the data, either by use of column names or indices -
meaning the business layer has to have knowledge of the sql which
extracted the data in the first place, and my opinion is that this
knowledge belongs in the data layer, not the business layer.
But I agree that this is a problem !
>
But it *is* recommeded by Microsoft - so it could be it is a good way
to get data.
MS is not perfect. And some things has been done giving priority to
being simple. The inheritance from Mort !
Seems pointless to have a business layer though.
That is another point. Someone preaches 3 layers as the only way. In
reality the number of layers should reflect the problem to be solved.
Some simple apps is actually fine with 2. But 3 to 4 are more common.

Arne
Jul 10 '08 #20
>But it *is* recommeded by Microsoft - so it could be it is a good way
>to get data.
No, it's not. It can't be, that recommender should lose their job
immediately. That programming rubbish is the reason there are so many
resource leaks in web applications. I have yet to come across a reason why
returning an open cursor to a caller is valid.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------
"Peter" <xd****@hotmail.comwrote in message
news:#O**************@TK2MSFTNGP04.phx.gbl...
Arne Vajhøj wrote:
>Peter wrote:
Arne Vajhøj wrote:
sloan wrote:
Well, it is the DataAccessLayer, which talks to a database.

You notice I return an IDataReader, not a specific one. Thus I
can swap it out to a SqlDataReader
OracleDataReader
MySqlDataReader
OleDBDataReader
and it doesn't affect the business layer code at at all.
True. But you can only switch database - you can not switch to
XML files or some custom native stuff.

This is correct of course, but I thought the bigger "problem" was
that the data layer relies on the business layer to close the
connection to the database.

Not in my opinion.

In your opinion is it a "problem" at all, or is it ok to do this?

What I don't like about it is that (1) the connection to the database
is now open until the business layer closes it; and (2) the business
layer has to have knowledge of the data layer (and data source) to be
able to extract the data, either by use of column names or indices -
meaning the business layer has to have knowledge of the sql which
extracted the data in the first place, and my opinion is that this
knowledge belongs in the data layer, not the business layer.

But it *is* recommeded by Microsoft - so it could be it is a good way
to get data. Seems pointless to have a business layer though.
Jul 10 '08 #21

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: RD | last post by:
My application contains several Classes & Forms . From the Login form I will get the User ID and Password for the SqlConnection. I established the connection through another class and...
6
by: B B | last post by:
Okay, here is what's happening: I have a reasonably fast laptop (1.4 GHz Mobile M, so comparable to 2.5GHz P4) doing .net development. Running Windows XP pro, SP2 IIS is installed and running...
7
by: Drew | last post by:
I have a small VB.NET application that captures left clicks, right clicks and key presses and automatically logs them to a csv file every 1 minute. The issue is that if I want to look at the log...
4
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: ...
29
by: Bryce K. Nielsen | last post by:
Suddenly this week, I've started getting this error message: System.Data.SqlClient.SqlConnection(GetOpenConnection)ExecuteNonQuery requires an open and available Connection. The connection's...
8
by: Imran Aziz | last post by:
Hello All, Like in C++ I tried to use constructor to open a database connection and distructor to close the database connection, it now turns out that one cannot create distrutors in C# classes. ...
7
by: fniles | last post by:
I am using VB.Net 2003 and MS Access (connecting using OleDBConnection). I read using DataAdapter and DataSet, not DataReader. When many people try to access the database at the same time, I get...
5
by: Usman Jamil | last post by:
Hi I've a class that creates a connection to a database, gets and loop on a dataset given a query and then close the connection. When I use netstat viewer to see if there is any connection open...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.