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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rafi B.

    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.

  • Dikkie Dik

    #2
    Re: Is it better to open connection with DB at beginning and closeat end, or after every "object run"?

    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:[color=blue]
    > 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.
    >[/color]

    Comment

    • d

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

      "Dikkie Dik" <nospam@nospam. org> wrote in message
      news:dq3k3s$5io $1@news.cistron .nl...[color=blue]
      > 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.[/color]

      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).
      [color=blue]
      >
      > Best regards
      >
      > Rafi B. wrote:[color=green]
      >> 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.
      >>[/color][/color]


      Comment

      Working...