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. General Programming
  3. C / C++ / MFC
  4. Connection objects in multithreaded appliations

Connection objects in multithreaded appliations

Scheduled Pinned Locked Moved C / C++ / MFC
c++databasequestion
7 Posts 3 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.
  • M Offline
    M Offline
    mahade1
    wrote on last edited by
    #1

    Hi, I am presently working on a multithreaded VC++ application which consists of 3 threads each accessing the same database. Would it be more efficient to use 3 different connection objects or just a single connection object in this case? Looking forward to your reply.. Thanks, Mahadevan

    A 1 Reply Last reply
    0
    • M mahade1

      Hi, I am presently working on a multithreaded VC++ application which consists of 3 threads each accessing the same database. Would it be more efficient to use 3 different connection objects or just a single connection object in this case? Looking forward to your reply.. Thanks, Mahadevan

      A Offline
      A Offline
      Antony M Kancidrowski
      wrote on last edited by
      #2

      It all depends on whether you are bringing up the connection and closing it down on each thread when you need to use the database. Bring up connections it fairly time consuming. I would tend only to have one database connection and serialise the database access between the threads. This database connection would tend to be instanciated when the application initialises and destroyed on application exit. Ant. I'm hard, yet soft.
      I'm coloured, yet clear.
      I'm fruity and sweet.
      I'm jelly, what am I? Muse on it further, I shall return!
      - David Williams (Little Britain)

      M 1 Reply Last reply
      0
      • A Antony M Kancidrowski

        It all depends on whether you are bringing up the connection and closing it down on each thread when you need to use the database. Bring up connections it fairly time consuming. I would tend only to have one database connection and serialise the database access between the threads. This database connection would tend to be instanciated when the application initialises and destroyed on application exit. Ant. I'm hard, yet soft.
        I'm coloured, yet clear.
        I'm fruity and sweet.
        I'm jelly, what am I? Muse on it further, I shall return!
        - David Williams (Little Britain)

        M Offline
        M Offline
        mahade1
        wrote on last edited by
        #3

        Well, In all the threads, the database connection is opened and closed only once and the threads are alive as long as the application is alive. In this case, will my application work faster than an application which has only a single database connection object? I am asking this because in order to serialize a single database connection between the multiple threads would require mutex (correct me if I am wrong) which may degrade the performance? Do you foresee any problem (esp. performance degradation) in case I use multiple connection objects? Regds, Maha

        A 1 Reply Last reply
        0
        • M mahade1

          Well, In all the threads, the database connection is opened and closed only once and the threads are alive as long as the application is alive. In this case, will my application work faster than an application which has only a single database connection object? I am asking this because in order to serialize a single database connection between the multiple threads would require mutex (correct me if I am wrong) which may degrade the performance? Do you foresee any problem (esp. performance degradation) in case I use multiple connection objects? Regds, Maha

          A Offline
          A Offline
          Antony M Kancidrowski
          wrote on last edited by
          #4

          Depending on the type of database connection object used there can be deadlock problems. In the past I have used multiple threads with ADO each having its own database connection. In which I experienced deadlock problems within the ADO code (which is not under my control). I switched the system to a single connection and serialised access to get over this. Saying that, this may not be a problem any longer. Just bear it in mind when you are developing. To answer your question, theoretically it will be more efficient for each thread to have its own connection. Hope that helps with your descision. Ant. I'm hard, yet soft.
          I'm coloured, yet clear.
          I'm fruity and sweet.
          I'm jelly, what am I? Muse on it further, I shall return!
          - David Williams (Little Britain)

          L 1 Reply Last reply
          0
          • A Antony M Kancidrowski

            Depending on the type of database connection object used there can be deadlock problems. In the past I have used multiple threads with ADO each having its own database connection. In which I experienced deadlock problems within the ADO code (which is not under my control). I switched the system to a single connection and serialised access to get over this. Saying that, this may not be a problem any longer. Just bear it in mind when you are developing. To answer your question, theoretically it will be more efficient for each thread to have its own connection. Hope that helps with your descision. Ant. I'm hard, yet soft.
            I'm coloured, yet clear.
            I'm fruity and sweet.
            I'm jelly, what am I? Muse on it further, I shall return!
            - David Williams (Little Britain)

            L Offline
            L Offline
            Laing James
            wrote on last edited by
            #5

            Hi. Prehaps it would make more sense to have a 'manager' from whom you request access, so that if you require a fourth or more thread(s) then the scaling issue is not a problem. James.

            A 1 Reply Last reply
            0
            • L Laing James

              Hi. Prehaps it would make more sense to have a 'manager' from whom you request access, so that if you require a fourth or more thread(s) then the scaling issue is not a problem. James.

              A Offline
              A Offline
              Antony M Kancidrowski
              wrote on last edited by
              #6

              Yes, that is basically what I have with the serialised access. :) Ant. I'm hard, yet soft.
              I'm coloured, yet clear.
              I'm fruity and sweet.
              I'm jelly, what am I? Muse on it further, I shall return!
              - David Williams (Little Britain)

              M 1 Reply Last reply
              0
              • A Antony M Kancidrowski

                Yes, that is basically what I have with the serialised access. :) Ant. I'm hard, yet soft.
                I'm coloured, yet clear.
                I'm fruity and sweet.
                I'm jelly, what am I? Muse on it further, I shall return!
                - David Williams (Little Britain)

                M Offline
                M Offline
                mahade1
                wrote on last edited by
                #7

                Thanks for your help. I still have a small query, What do you generally prefer for connecting to Database, I use the CDatabase object to connect with the SQL server and I am just not able to avoid the deadlocks in both cases, that is single connection object or multiple connection objects. I seem to be getting this problem when a thread is executing a query and the SQL server goes down. Any suggestions? Thanks a lot, Mahadevan.

                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