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#
  4. Handling with multiple SQL-DataReader

Handling with multiple SQL-DataReader

Scheduled Pinned Locked Moved C#
databasehelptutorialquestion
14 Posts 5 Posters 0 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
    softwarejaeger
    wrote on last edited by
    #1

    Hello, i have a application, where parallel running threads have access to my database. In most cases, that's no problem, but if a thread has a open DataReader and another Thread wants to access the DB too i get a problem, because there's only one open DataReader allowed. Now, i wanted to make a Method (lets call it "dbRead()" ) which asks the DB and returns the Data to the specific Thread or Method, which executes dbRead(). My problem is now, that this could become a bottleneck if i have a lot of Reader-Transactions. There's the possibility to make a list of classes (lets call it "dbOperations") which contains public the dbRead(), put maybe 5 of them in a List and if i want to make a transaction i use randomly one of the classes and start "dbOperationsList[randomNr].dbRead()". This would work, but isn't there maybe a tutorial or has somebody such an piece of code? Because i don't think, that i'm the first with this problem ;) Many thanks for your help Patrick

    P E S 3 Replies Last reply
    0
    • S softwarejaeger

      Hello, i have a application, where parallel running threads have access to my database. In most cases, that's no problem, but if a thread has a open DataReader and another Thread wants to access the DB too i get a problem, because there's only one open DataReader allowed. Now, i wanted to make a Method (lets call it "dbRead()" ) which asks the DB and returns the Data to the specific Thread or Method, which executes dbRead(). My problem is now, that this could become a bottleneck if i have a lot of Reader-Transactions. There's the possibility to make a list of classes (lets call it "dbOperations") which contains public the dbRead(), put maybe 5 of them in a List and if i want to make a transaction i use randomly one of the classes and start "dbOperationsList[randomNr].dbRead()". This would work, but isn't there maybe a tutorial or has somebody such an piece of code? Because i don't think, that i'm the first with this problem ;) Many thanks for your help Patrick

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

      Create more connections; one for each concurrent request.

      S 1 Reply Last reply
      0
      • S softwarejaeger

        Hello, i have a application, where parallel running threads have access to my database. In most cases, that's no problem, but if a thread has a open DataReader and another Thread wants to access the DB too i get a problem, because there's only one open DataReader allowed. Now, i wanted to make a Method (lets call it "dbRead()" ) which asks the DB and returns the Data to the specific Thread or Method, which executes dbRead(). My problem is now, that this could become a bottleneck if i have a lot of Reader-Transactions. There's the possibility to make a list of classes (lets call it "dbOperations") which contains public the dbRead(), put maybe 5 of them in a List and if i want to make a transaction i use randomly one of the classes and start "dbOperationsList[randomNr].dbRead()". This would work, but isn't there maybe a tutorial or has somebody such an piece of code? Because i don't think, that i'm the first with this problem ;) Many thanks for your help Patrick

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        My applications always seem to allow multiple readers to a database without issue.

        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

        P 1 Reply Last reply
        0
        • E Ennis Ray Lynch Jr

          My applications always seem to allow multiple readers to a database without issue.

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

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

          Provided they each have their own connection?

          1 Reply Last reply
          0
          • P PIEBALDconsult

            Create more connections; one for each concurrent request.

            S Offline
            S Offline
            softwarejaeger
            wrote on last edited by
            #5

            OK, that was my first problem and so that i need more Connections to the database, i'll need such an "RequestManager". I've made a simple piece of code and i'll test it if it works. It's very simple. A class, which a standardized Method which gives me a List back (it's not a Object[][], because i don't now how much entries i'll get back i get only the amount of columns back), this method makes the request and reads the data and closes the DataReader. This class has a own connection to the database. So... i put n-instances in a List at the start of my application and make a simple integer-value, which gives me the index of the next RequestManager-Class back. So the first request goes to List[0], the second to List[1] and so on... a simple and effective LoadBalancing and Transaction-Management for my Database. And if i see, that i need more connections, i could realize this if i only make more instances of the RequestManager Class. Many thanks for your ideas. :thumbsup: :-D

            N P 2 Replies Last reply
            0
            • S softwarejaeger

              OK, that was my first problem and so that i need more Connections to the database, i'll need such an "RequestManager". I've made a simple piece of code and i'll test it if it works. It's very simple. A class, which a standardized Method which gives me a List back (it's not a Object[][], because i don't now how much entries i'll get back i get only the amount of columns back), this method makes the request and reads the data and closes the DataReader. This class has a own connection to the database. So... i put n-instances in a List at the start of my application and make a simple integer-value, which gives me the index of the next RequestManager-Class back. So the first request goes to List[0], the second to List[1] and so on... a simple and effective LoadBalancing and Transaction-Management for my Database. And if i see, that i need more connections, i could realize this if i only make more instances of the RequestManager Class. Many thanks for your ideas. :thumbsup: :-D

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              Doesn't sounds like a good idea to me. I'd suggest you to use separate connection and command instances for each threads. ADO.NET has inbuilt connection pooling and you don't need to pool it manually. :)

              Navaneeth How to use google | Ask smart questions

              P 1 Reply Last reply
              0
              • S softwarejaeger

                OK, that was my first problem and so that i need more Connections to the database, i'll need such an "RequestManager". I've made a simple piece of code and i'll test it if it works. It's very simple. A class, which a standardized Method which gives me a List back (it's not a Object[][], because i don't now how much entries i'll get back i get only the amount of columns back), this method makes the request and reads the data and closes the DataReader. This class has a own connection to the database. So... i put n-instances in a List at the start of my application and make a simple integer-value, which gives me the index of the next RequestManager-Class back. So the first request goes to List[0], the second to List[1] and so on... a simple and effective LoadBalancing and Transaction-Management for my Database. And if i see, that i need more connections, i could realize this if i only make more instances of the RequestManager Class. Many thanks for your ideas. :thumbsup: :-D

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

                In one of my database access schemes I have a Dictionary<string,connection>, the first connection is named "" and is used by default, but when necessary I can use:

                dbc.AddConnection ( "name" )

                dbc [ "name" ].Execute...

                dbc.RemoveConnection ( "name" )

                So I can be reading with the default connection and use another to perform other actions as needed.

                S 1 Reply Last reply
                0
                • N N a v a n e e t h

                  Doesn't sounds like a good idea to me. I'd suggest you to use separate connection and command instances for each threads. ADO.NET has inbuilt connection pooling and you don't need to pool it manually. :)

                  Navaneeth How to use google | Ask smart questions

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

                  N a v a n e e t h wrote:

                  Doesn't sounds like a good idea to me.

                  Indeed.

                  N a v a n e e t h wrote:

                  separate connection and command instances for each threads

                  Yes, but a thread may still need more than one connection to the same database.

                  N a v a n e e t h wrote:

                  ADO.NET has inbuilt connection pooling

                  But I don't think it will detect when you need an additional connection to a database and create it. The pooling only pools the connections that have been created by the application. And of course, only if the connection has been closed. So the developer still has to do that.

                  N a v a n e e t h wrote:

                  you don't need to pool it manually

                  Not pool, but create.

                  N 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    N a v a n e e t h wrote:

                    Doesn't sounds like a good idea to me.

                    Indeed.

                    N a v a n e e t h wrote:

                    separate connection and command instances for each threads

                    Yes, but a thread may still need more than one connection to the same database.

                    N a v a n e e t h wrote:

                    ADO.NET has inbuilt connection pooling

                    But I don't think it will detect when you need an additional connection to a database and create it. The pooling only pools the connections that have been created by the application. And of course, only if the connection has been closed. So the developer still has to do that.

                    N a v a n e e t h wrote:

                    you don't need to pool it manually

                    Not pool, but create.

                    N Offline
                    N Offline
                    N a v a n e e t h
                    wrote on last edited by
                    #9

                    PIEBALDconsult wrote:

                    The pooling only pools the connections that have been created by the application

                    My understanding is: each pool will have a minimum connection object limit and when a pool is initiated, these objects will be created and ready to use. When SqlConnection object is requested, it will be served from the pool if available.

                    PIEBALDconsult wrote:

                    So the developer still has to do that.

                    Caching the connection objects? I still believe developer shouldn't cache the connection objects. All they can do is to wrap the connection in a using statement, so it gets disposed properly and return back to the pool. Let framework's pooling do it's job. :)

                    Navaneeth How to use google | Ask smart questions

                    P 1 Reply Last reply
                    0
                    • N N a v a n e e t h

                      PIEBALDconsult wrote:

                      The pooling only pools the connections that have been created by the application

                      My understanding is: each pool will have a minimum connection object limit and when a pool is initiated, these objects will be created and ready to use. When SqlConnection object is requested, it will be served from the pool if available.

                      PIEBALDconsult wrote:

                      So the developer still has to do that.

                      Caching the connection objects? I still believe developer shouldn't cache the connection objects. All they can do is to wrap the connection in a using statement, so it gets disposed properly and return back to the pool. Let framework's pooling do it's job. :)

                      Navaneeth How to use google | Ask smart questions

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

                      N a v a n e e t h wrote:

                      these objects will be created and ready to use

                      With what settings? How will the pool know what the user wants to connect to until asked?

                      N a v a n e e t h wrote:

                      When SqlConnection object is requested, it will be served from the pool if available.

                      If the connection string matches, yes.

                      N a v a n e e t h wrote:

                      gets disposed properly and return back to the pool

                      I understand that happens at Close. So close the connection, but retain the Connection object. When you reopen the connection, the pool tries to use an existing connection. http://msdn.microsoft.com/en-us/library/8xx3tyca(VS.85).aspx[^]

                      N 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        N a v a n e e t h wrote:

                        these objects will be created and ready to use

                        With what settings? How will the pool know what the user wants to connect to until asked?

                        N a v a n e e t h wrote:

                        When SqlConnection object is requested, it will be served from the pool if available.

                        If the connection string matches, yes.

                        N a v a n e e t h wrote:

                        gets disposed properly and return back to the pool

                        I understand that happens at Close. So close the connection, but retain the Connection object. When you reopen the connection, the pool tries to use an existing connection. http://msdn.microsoft.com/en-us/library/8xx3tyca(VS.85).aspx[^]

                        N Offline
                        N Offline
                        N a v a n e e t h
                        wrote on last edited by
                        #11

                        PIEBALDconsult wrote:

                        With what settings? How will the pool know what the user wants to connect to until asked?

                        My bad, you are correct. Pool will be initiated only when a connection is first opened.

                        PIEBALDconsult wrote:

                        I understand that happens at Close

                        You can call dispose as well. It happens at dispose too. Here is what MSDN says, We strongly recommend that you always close the connection when you are finished using it so that the connection will be returned to the pool. You can do this using either the Close or Dispose methods of the Connection object, or by opening all connections inside a using statement in C#, or a Using statement in Visual Basic. :)

                        Navaneeth How to use google | Ask smart questions

                        1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          In one of my database access schemes I have a Dictionary<string,connection>, the first connection is named "" and is used by default, but when necessary I can use:

                          dbc.AddConnection ( "name" )

                          dbc [ "name" ].Execute...

                          dbc.RemoveConnection ( "name" )

                          So I can be reading with the default connection and use another to perform other actions as needed.

                          S Offline
                          S Offline
                          softwarejaeger
                          wrote on last edited by
                          #12

                          Well a lot of answers from you thanks =) So to the question, "why doesn't get every thread a db-connection?", the answer is, that i make a server-application and for each user-connection i have on thread, but i don't have a huge amount of requests from each user, so it doesn't make sense to give each user a own db-connection, and the second is i don't know how many users will use the server. So the idea with this RequestManager. I make maybe 5 or 10 (or how many i need) instances from it, stored in a List. Each RequestManager has an own DB-Connection and handles the Reader for itself. If a user now makes a request, the class of the User-Connection starts the Method "GetData()" (give the baby a name ;) ) from one of this RequestManagers. So i can make a few requests parallel and if i need more i can easily put even more RequestManager in this List<>. How does my application get the information to put more RequestManager-instances into this list? Well that's easy, over a configuration-file. I let my application reload them every 5 minutes and it checks how many RequestManager i need and so i'm able to configure them while my system is running. Your examples, had the nearly the same idea as mine. But i only handle with the "RequestManager" and not with Reader and Connections seperately. I can only make failures on one place (the RequestManager). Thanks for your help, you gave me a lot of inspiration for this problem. :thumbsup:

                          1 Reply Last reply
                          0
                          • S softwarejaeger

                            Hello, i have a application, where parallel running threads have access to my database. In most cases, that's no problem, but if a thread has a open DataReader and another Thread wants to access the DB too i get a problem, because there's only one open DataReader allowed. Now, i wanted to make a Method (lets call it "dbRead()" ) which asks the DB and returns the Data to the specific Thread or Method, which executes dbRead(). My problem is now, that this could become a bottleneck if i have a lot of Reader-Transactions. There's the possibility to make a list of classes (lets call it "dbOperations") which contains public the dbRead(), put maybe 5 of them in a List and if i want to make a transaction i use randomly one of the classes and start "dbOperationsList[randomNr].dbRead()". This would work, but isn't there maybe a tutorial or has somebody such an piece of code? Because i don't think, that i'm the first with this problem ;) Many thanks for your help Patrick

                            S Offline
                            S Offline
                            senguptaamlan
                            wrote on last edited by
                            #13

                            MARS (Multiple Active Result Set) can give you a quick solution in this respect (if you are using ADO.NET 2.0 and SQL Server 2005).

                            S 1 Reply Last reply
                            0
                            • S senguptaamlan

                              MARS (Multiple Active Result Set) can give you a quick solution in this respect (if you are using ADO.NET 2.0 and SQL Server 2005).

                              S Offline
                              S Offline
                              softwarejaeger
                              wrote on last edited by
                              #14

                              Thanks, but i don't use SQL Server 2005, but i read the Article about MARS on MSDN, this is not exactly that, what i want, because i want the possibility to have more parallel running transactions. But many thanks to you all for your answers, but i have now the solution (or at least the ideas for my solution)

                              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