Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Opening/Closing Connections - Efficiency

Opening/Closing Connections - Efficiency

Scheduled Pinned Locked Moved Database
databasequestionannouncement
5 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    stormydaniels
    wrote on last edited by
    #1

    Hello :) I have a program which must read a directory of files and update a database. I can open my connection at the program start and run the process but it may take a couple of minutes or so. Is is worth opening and closing the database connection for each record I update or can I just hold the connection open for minutes at a time and close it at the end of the process? Thanks :)

    M W P 3 Replies Last reply
    0
    • S stormydaniels

      Hello :) I have a program which must read a directory of files and update a database. I can open my connection at the program start and run the process but it may take a couple of minutes or so. Is is worth opening and closing the database connection for each record I update or can I just hold the connection open for minutes at a time and close it at the end of the process? Thanks :)

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You should use connection pooling when you create the connection (creating is the slow bit) and close the connection after each write. This will put the connection into the pool and the next write operation will retrieve and use the same connection. You could also put all the filenames into a table and bulkcopy them into the database.

      Never underestimate the power of human stupidity RAH

      J 1 Reply Last reply
      0
      • M Mycroft Holmes

        You should use connection pooling when you create the connection (creating is the slow bit) and close the connection after each write. This will put the connection into the pool and the next write operation will retrieve and use the same connection. You could also put all the filenames into a table and bulkcopy them into the database.

        Never underestimate the power of human stupidity RAH

        J Offline
        J Offline
        Jon_Boy
        wrote on last edited by
        #3

        Agreed with Mycroft. By default connection pooling should be enabled, unless specified otherwise.

        Any suggestions, ideas, or 'constructive criticism' are always welcome. "There's no such thing as a stupid question, only stupid people." - Mr. Garrison

        1 Reply Last reply
        0
        • S stormydaniels

          Hello :) I have a program which must read a directory of files and update a database. I can open my connection at the program start and run the process but it may take a couple of minutes or so. Is is worth opening and closing the database connection for each record I update or can I just hold the connection open for minutes at a time and close it at the end of the process? Thanks :)

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          The answer Mycroft Holmes gave is a good and correct way to go. The only reason I see to keep the connection open in the program is if you have transactional needs over separate writes. For example: if you need a logic where every record is written succesfully to the database or no records at all (commit point is after all separate writes).

          The need to optimize rises from a bad design. My articles[^]

          1 Reply Last reply
          0
          • S stormydaniels

            Hello :) I have a program which must read a directory of files and update a database. I can open my connection at the program start and run the process but it may take a couple of minutes or so. Is is worth opening and closing the database connection for each record I update or can I just hold the connection open for minutes at a time and close it at the end of the process? Thanks :)

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Yeah, what they said. I definitely wouldn't open the connection any sooner than necessary, but I also wouldn't close it any sooner either. I prefer to get all the data collected and ready to go, then open, process all the stuff, and close. If holding all the data in memory at once isn't an option, then opening and closing for each may be a good idea.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups