469,956 Members | 2,623 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,956 developers. It's quick & easy.

Is it better to open connection with DB at beginning and close at end, or after every "object run"?

Sorry for the English.
I mean, when building my applications, I often need to do several
database actions, and I'm thinking, what's better?

1) Open connection with DB, for instance mySQL, at beginning and close
at the end, so every "unit" in the code knows that THIS IS the
connection, and its opened.

OR

2) Every "unit" in the code opens and closes DB connection
independantly.

Jan 11 '06 #1
2 1466
3) The database object/connection is created as soon as it is needed and
passed to all units that need it. This allows reuse of the units.

Best regards

Rafi B. wrote:
Sorry for the English.
I mean, when building my applications, I often need to do several
database actions, and I'm thinking, what's better?

1) Open connection with DB, for instance mySQL, at beginning and close
at the end, so every "unit" in the code knows that THIS IS the
connection, and its opened.

OR

2) Every "unit" in the code opens and closes DB connection
independantly.

Jan 11 '06 #2
d
"Dikkie Dik" <no****@nospam.org> wrote in message
news:dq**********@news.cistron.nl...
3) The database object/connection is created as soon as it is needed and
passed to all units that need it. This allows reuse of the units.
Ideally, it should use persistent connections (if the server allows), and
the object shouldn't initiate its connection to the DB unless it's passed a
query (so don't do it when the object is constructed, but when it's actually
used).

Best regards

Rafi B. wrote:
Sorry for the English.
I mean, when building my applications, I often need to do several
database actions, and I'm thinking, what's better?

1) Open connection with DB, for instance mySQL, at beginning and close
at the end, so every "unit" in the code knows that THIS IS the
connection, and its opened.

OR

2) Every "unit" in the code opens and closes DB connection
independantly.

Jan 12 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by Hung Jung Lu | last post: by
7 posts views Thread by Bennett Haselton | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.