multiple local connections or single connection in database class?

  • Thread starter Thread starter John B
  • Start date Start date
J

John B

Is is better to:

(1) Have a single connection object in a database class which is
instantiated when first used at start-up and used by other classes (opened
and closed as necessary - NOT kept open).

or

(2) Local variable connections created in the function in which they are
used (and maybe reference the connectionstring in the database class)?
 
John,

There is in this in my opinon no better, just think how much you have to
type (correct by a change) for your solutions and than you know the answer.

Just my thought,

Cor
 
¤ Is is better to:
¤
¤ (1) Have a single connection object in a database class which is
¤ instantiated when first used at start-up and used by other classes (opened
¤ and closed as necessary - NOT kept open).
¤
¤ or
¤
¤ (2) Local variable connections created in the function in which they are
¤ used (and maybe reference the connectionstring in the database class)?

Requirements can vary depending upon the type of application and database you are working with, but
the general recommendation is to open (and close) connection objects on an as needed basis rather
than attempt to maintain any type of single persistent and/or global connection.


Paul ~~~ (e-mail address removed)
Microsoft MVP (Visual Basic)
 
The first option, which tough seems very easy and less code to wirte,
might start giving you problems on the multi threaded client, service
or an ASP.Net app being accessed by multiple user. If you use one
connection object that is being re-used by many functions, in a multi
threaded enviroment you could have synchronizing issues. Id suggest (2)
using a shared connection string.

NuTcAsE
 
Back
Top