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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. 'Best practices' for managing data base connections

'Best practices' for managing data base connections

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpquestion
8 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.
  • G Offline
    G Offline
    Gary Wheeler
    wrote on last edited by
    #1

    My application currently opens its data base connection when it starts, and closes it when it exits. The application runs for long periods (days or even weeks). The problem lies in that it accesses the data base infrequently. Accesses may be minutes or even hours apart in time. In other instances, there may be several accesses within a few milliseconds of each other. At some sites, it appears that the data base server (SQL Server 2005) is dropping the connection because my application is not keeping up a steady traffic of operations. One option would be for me to change my code to open the data base connection, perform the operation, and close the connection, for each and every data base transaction. This approach seems clumsy, however. Does anyone have any better suggestions?

    Software Zen: delete this;

    L I D P 4 Replies Last reply
    0
    • G Gary Wheeler

      My application currently opens its data base connection when it starts, and closes it when it exits. The application runs for long periods (days or even weeks). The problem lies in that it accesses the data base infrequently. Accesses may be minutes or even hours apart in time. In other instances, there may be several accesses within a few milliseconds of each other. At some sites, it appears that the data base server (SQL Server 2005) is dropping the connection because my application is not keeping up a steady traffic of operations. One option would be for me to change my code to open the data base connection, perform the operation, and close the connection, for each and every data base transaction. This approach seems clumsy, however. Does anyone have any better suggestions?

      Software Zen: delete this;

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Gary Wheeler wrote:

      Does anyone have any better suggestions?

      No. AFAIK open-use-close is the normal pattern. And connection pooling takes care of most performance issues that result from frequent opens/closes. [ADDED] The above applies when using .NET Framework.[/ADDED] :)

      Luc Pattyn


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      Local announcement (Antwerp region): Lange Wapper? Neen!


      modified on Thursday, October 8, 2009 7:47 AM

      G 1 Reply Last reply
      0
      • L Luc Pattyn

        Gary Wheeler wrote:

        Does anyone have any better suggestions?

        No. AFAIK open-use-close is the normal pattern. And connection pooling takes care of most performance issues that result from frequent opens/closes. [ADDED] The above applies when using .NET Framework.[/ADDED] :)

        Luc Pattyn


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        Local announcement (Antwerp region): Lange Wapper? Neen!


        modified on Thursday, October 8, 2009 7:47 AM

        G Offline
        G Offline
        Gary Wheeler
        wrote on last edited by
        #3

        Thanks, Luc! I had a feeling that was the case, but thought I would make sure.

        Software Zen: delete this;

        L 1 Reply Last reply
        0
        • G Gary Wheeler

          Thanks, Luc! I had a feeling that was the case, but thought I would make sure.

          Software Zen: delete this;

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          you're welcome. :)

          Luc Pattyn


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          Local announcement (Antwerp region): Lange Wapper? Neen!


          1 Reply Last reply
          0
          • G Gary Wheeler

            My application currently opens its data base connection when it starts, and closes it when it exits. The application runs for long periods (days or even weeks). The problem lies in that it accesses the data base infrequently. Accesses may be minutes or even hours apart in time. In other instances, there may be several accesses within a few milliseconds of each other. At some sites, it appears that the data base server (SQL Server 2005) is dropping the connection because my application is not keeping up a steady traffic of operations. One option would be for me to change my code to open the data base connection, perform the operation, and close the connection, for each and every data base transaction. This approach seems clumsy, however. Does anyone have any better suggestions?

            Software Zen: delete this;

            I Offline
            I Offline
            i i i
            wrote on last edited by
            #5

            will always prefer disconnected architecture means connect to db when their is needs to connect to db mostly in CRUD operations.

            Best Of Regards, SOFTDEV If you have knowledge, let others light their candles at it

            1 Reply Last reply
            0
            • G Gary Wheeler

              My application currently opens its data base connection when it starts, and closes it when it exits. The application runs for long periods (days or even weeks). The problem lies in that it accesses the data base infrequently. Accesses may be minutes or even hours apart in time. In other instances, there may be several accesses within a few milliseconds of each other. At some sites, it appears that the data base server (SQL Server 2005) is dropping the connection because my application is not keeping up a steady traffic of operations. One option would be for me to change my code to open the data base connection, perform the operation, and close the connection, for each and every data base transaction. This approach seems clumsy, however. Does anyone have any better suggestions?

              Software Zen: delete this;

              D Offline
              D Offline
              David Skelly
              wrote on last edited by
              #6

              You could consider some sort of connection pooling strategy. Open-use-close is not very efficient when you are accessing the database very frequently. It is more efficient to pool and reuse open connections in this circumstance. One technique that connection pooling libraries use is to test the connection before returning it from the pool. Then if the connection is bad (e.g. timed out by the database) you can throw it away and open a new one. That way, if your application is making lots of database accesses you avoid the overhead of opening and closing connections all the time. When your application idles and does not access the database for a long time, the connection can be dropped and a new one will be opened, "invisibly" from the point of view of the code using the connection. You don't say what language of framework your application uses, but there are connection pooling libraries available for .NET, Java, Python, Ruby and many others.

              G 1 Reply Last reply
              0
              • D David Skelly

                You could consider some sort of connection pooling strategy. Open-use-close is not very efficient when you are accessing the database very frequently. It is more efficient to pool and reuse open connections in this circumstance. One technique that connection pooling libraries use is to test the connection before returning it from the pool. Then if the connection is bad (e.g. timed out by the database) you can throw it away and open a new one. That way, if your application is making lots of database accesses you avoid the overhead of opening and closing connections all the time. When your application idles and does not access the database for a long time, the connection can be dropped and a new one will be opened, "invisibly" from the point of view of the code using the connection. You don't say what language of framework your application uses, but there are connection pooling libraries available for .NET, Java, Python, Ruby and many others.

                G Offline
                G Offline
                Gary Wheeler
                wrote on last edited by
                #7

                I'm using C++/MFC (stop laughing :rolleyes:). The data base activity is an 'add-on' to a process control application. I'm thinking of moving the data base handling off into a separate thread of its own, so that it can do the open/CRUD/close without affecting the other stuff that's going on.

                Software Zen: delete this;

                1 Reply Last reply
                0
                • G Gary Wheeler

                  My application currently opens its data base connection when it starts, and closes it when it exits. The application runs for long periods (days or even weeks). The problem lies in that it accesses the data base infrequently. Accesses may be minutes or even hours apart in time. In other instances, there may be several accesses within a few milliseconds of each other. At some sites, it appears that the data base server (SQL Server 2005) is dropping the connection because my application is not keeping up a steady traffic of operations. One option would be for me to change my code to open the data base connection, perform the operation, and close the connection, for each and every data base transaction. This approach seems clumsy, however. Does anyone have any better suggestions?

                  Software Zen: delete this;

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

                  Instantiate the connection once, but open and close it only as needed.

                  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