473,394 Members | 1,703 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,394 software developers and data experts.

Connection Question

I was suggested by a member of this group not to use global connection
object. Instead of it I was suggested to create a new one in every procedure
and close it at the end of that procedure.
What is the advantage of this approach?
From my beginners point of view it will require some extra code (creation,
opening and closing) and some extra time. What's wrong with the global
connection object which is created at the start of the application and is
closed at the end?
I'm learning Windows Form applications now.

Thank you
Esha
Sep 15 '06 #1
8 1409
esha wrote:
I was suggested by a member of this group not to use global connection
object. Instead of it I was suggested to create a new one in every procedure
and close it at the end of that procedure.
What is the advantage of this approach?
From my beginners point of view it will require some extra code (creation,
opening and closing) and some extra time. What's wrong with the global
connection object which is created at the start of the application and is
closed at the end?
I'm learning Windows Form applications now.
The global connection is will either not work in a multithreaded
context or will perform very poorly.

..NET uses a connection pool, so the overhead of open and close
is not that big.

The extra code you write is worth it due to the increased
robustness and reliability.

Arne
Sep 15 '06 #2
Thank you.
If I'm not using multithreading (at least for now because to be able to run
I need to learn how to walk first) then what is a preferable approach? -
still local connection for every procedure? Or it will be better to use a
global one?

Esha
"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:T2nOg.37909$_q4.27082@dukeread09...
esha wrote:
>I was suggested by a member of this group not to use global connection
object. Instead of it I was suggested to create a new one in every
procedure and close it at the end of that procedure.
What is the advantage of this approach?
From my beginners point of view it will require some extra code
(creation, opening and closing) and some extra time. What's wrong with
the global connection object which is created at the start of the
application and is closed at the end?
I'm learning Windows Form applications now.

The global connection is will either not work in a multithreaded
context or will perform very poorly.

.NET uses a connection pool, so the overhead of open and close
is not that big.

The extra code you write is worth it due to the increased
robustness and reliability.

Arne

Sep 15 '06 #3
esha wrote:
Thank you.
If I'm not using multithreading (at least for now because to be able to run
I need to learn how to walk first) then what is a preferable approach? -
still local connection for every procedure? Or it will be better to use a
global one?
I can think of a few cases where a global database
connection will work.

I find it hard to come up with an example where it
is the best solution.

The general rule is:
- open as late as possible
- close as soon as possible

Arne
Sep 15 '06 #4
Esha,

There is nothing wrong with, however every not in a method placed variable
makes it less easier to review a program. However, you have the choose to
use it global, static or just in the method, just as you prefer. As long as
you open and close it and not dispose it. If you dispose it, than it is in
my opinion easier to place it in a method.

Cor

"esha" <es**@newsgroups.comschreef in bericht
news:%2***************@TK2MSFTNGP06.phx.gbl...
>I was suggested by a member of this group not to use global connection
object. Instead of it I was suggested to create a new one in every
procedure and close it at the end of that procedure.
What is the advantage of this approach?
From my beginners point of view it will require some extra code (creation,
opening and closing) and some extra time. What's wrong with the global
connection object which is created at the start of the application and is
closed at the end?
I'm learning Windows Form applications now.

Thank you
Esha


Sep 15 '06 #5
Arne Vajhøj a écrit :
esha wrote:
>Thank you.
If I'm not using multithreading (at least for now because to be able
to run I need to learn how to walk first) then what is a preferable
approach? - still local connection for every procedure? Or it will be
better to use a global one?

I can think of a few cases where a global database
connection will work.

I find it hard to come up with an example where it
is the best solution.

The general rule is:
- open as late as possible
- close as soon as possible

Arne
Hi,

You can have a "global" Connection instance and open it every time you
need it, and close it right after. I think it is the same as creating a
new connection from scratch every time, but perhaps slightly more
efficient as well as convenient.

But what if you open your connection and call a method that closes it
though you still want to work on it ? Well, you can open it again...
But what if you do not (or do not want to) know if the methods you call
will close your connection ? Well, you can use the DbConnectionAdapter
class (cf. end of the post). Then, EVERY time you want to open your
connection do the following :
using (DbConnectionAdapter c=new DbConnectionAdapter(myConnection))
{
c.Open();

// Do some work here
}
At the end of the scope, your connection will be in the state you found
it, i.e. opened or closed.

Mathieu

<code>
public class DbConnectionAdapter:
IDisposable
{
public DbConnectionAdapter(IDbConnection connection)
{
_Connection=connection;
_ConnectionState=connection.State;
}

~DbConnectionAdapter()
{
Dispose(false);
}

public void Close()
{
if (_Connection.State!=ConnectionState.Closed)
_Connection.Close();
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

public void Open()
{
if (_Connection.State!=ConnectionState.Open)
_Connection.Open();
}

protected void Dispose(bool disposing)
{
if (_Disposed)
return;

if (_Connection.State!=_ConnectionState)
switch (_ConnectionState)
{
case ConnectionState.Closed:
_Connection.Close();
break;
case ConnectionState.Open:
_Connection.Open();
break;
}

_Disposed=true;
}

private bool _Disposed;
private IDbConnection _Connection;
private ConnectionState _ConnectionState;
}
</code>
Sep 15 '06 #6
Mathieu Cartoixa wrote:
You can have a "global" Connection instance and open it every time
you need it, and close it right after. I think it is the same as
creating a new connection from scratch every time, but perhaps slightly
more efficient as well as convenient.
And not thread safe.

Making it a hidden bomb in your code waiting
to create havoc.

Arne

Sep 15 '06 #7
Arne Vajhøj a écrit :
Mathieu Cartoixa wrote:
> You can have a "global" Connection instance and open it every time
you need it, and close it right after. I think it is the same as
creating a new connection from scratch every time, but perhaps
slightly more efficient as well as convenient.

And not thread safe.
Agreed. But taking this into account :
esha wrote:
>If I'm not using multithreading (at least for now because to be able
to run I need to learn how to walk first) then what is a preferable
approach? - still local connection for every procedure? Or it will
be >better to use a global one?

Beginner, client application (?), single-threaded : I still think a
global connection is well suited. You cannot grab all the difficulties
at once.
Making it a hidden bomb in your code waiting
to create havoc.
Unless you know what you are (and have been) doing.
I (maybe wrongly) think that, unless the specifications are really
unclear (and are therefore un-specifications, which happens), you will
know very early whether your code is likely to run in a multithreaded
environment or not, and it is not worth (and often by far) writing
thread-safe code when you do not need to. But the fact that a piece of
code is or is not thread-safe should be clear (it is in MSDN). Thanks
for making it clear here.

Mathieu
Sep 18 '06 #8
Mathieu Cartoixa wrote:
Arne Vajhøj a écrit :
>And not thread safe.

Agreed. But taking this into account :
esha wrote:
>If I'm not using multithreading (at least for now because to be able
>to run I need to learn how to walk first) then what is a preferable
>approach? - still local connection for every procedure? Or it will be
>better to use a global one?

Beginner, client application (?), single-threaded : I still think a
global connection is well suited. You cannot grab all the difficulties
at once.
But it is not more difficult to do it right.

I can really not see any reason to learn bad habbits.
>Making it a hidden bomb in your code waiting
to create havoc.

Unless you know what you are (and have been) doing.
I (maybe wrongly) think that, unless the specifications are really
unclear (and are therefore un-specifications, which happens), you will
know very early whether your code is likely to run in a multithreaded
environment or not, and it is not worth (and often by far) writing
thread-safe code when you do not need to. But the fact that a piece of
code is or is not thread-safe should be clear (it is in MSDN). Thanks
for making it clear here.
Code has a tendency to stay around for ever and be used in
different contexts.

In a language like C# there are really very little reason to
not make the code thread safe.

Arne
Sep 19 '06 #9

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

Similar topics

8
by: Jozef | last post by:
Hello, When an Access program connects to a database, is there a unique connection id? I've used the User Roster to check connections, but just wondering if there was two machines with the same...
6
by: charliewest | last post by:
I have developed an application for WM 2003, which frequently transacts with a sql server ce 2.0 database. I have several procedures which utilize the following code: cn = new...
4
by: Phil Townsend | last post by:
While we have switched to the Application.Data block to improve connection management, we are still experiencing connection pool errors on occasions. I have been careful to explicitly close...
3
by: R Reyes | last post by:
Hi, I'm trying to modularize my database connections a little better and get more out of my project with less code. First check out this common dbOpen() function inside class clsDatabase. I...
1
by: Mike | last post by:
Hi We are new to the world of ASP Development and I have a simple question - we are starting a test development in ASP 2.0 Beta and we are building an application using a Direct Connection to...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
16
by: crbd98 | last post by:
Hello All, Some time ago, I implemented a data access layer that included a simple connectin pool. At the time, I did it all by myself: I created N connections, each connection associated with...
20
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.