For example, I need to fill lots of DataTables with SQLDataAdapter's Fill() method:
DataAdapter1.Fill(DataTable1);
DataAdapter2.Fill(DataTable2);
DataAdapter3.Fill(DataTable3);
DataAdapter4.Fill(DataTable4);
DataAdapter5.Fill(DataTable5);
....
....
Even all the dataadapter objects use the same SQLConnection, each Fill method will open and close the connection unless the connection state is already open before the method call.
What I want to know is how does unnecessarily opening and closing SQLConnections affect the performance of the application. How much does it need to scale to see the bad effects of this problem (100,000s of concurrent users?). In a mid-size website (daily 50000 users) does it worth bothering and searching for all the Fill() calls, keeping them together in the code and opening the connection before any Fill() call and closing afterwards?
ADO.NET has connection pooling, e.g. when you close a connection, it's not really completely closed, but "recycled" if you ask for a new connection with exactly the same connection string.
Nevertheless - if you already know ahead of time that you'll have to call these five Fill methods one by one, I would definitely recommend
It's accepted best practice to do it this way, it doesn't hurt you - so just do it! :-)
Marc
PS: Connection pooling in ADO.NET of course only works if you haven't turned it off ! :-) It's on by default - you'd have to explicitly disable it.
Key Point:
Why open, keep and reuse connections?
Performance
There are many reasons that you may want to open and close connections. You have to decide where the best trade-off is for your use. You can do both: use an open connection for a period of time and/or a set number of transactions, then close it and open a new one.
Opening and closing SQL connections is expensive when compared to other simple tasks in the database. But, if your actual task is already time consuming, the extra overhead may not be noticed (if you are already hiding the wait period of the actual task - so the user does not start randomly clicking things - like retry).
Test Case:
You can measure your difference by writing two versions of a test query. Select any simple SQL task (needs to be the same in each version).
In version one, do it with a single constant open connection outside the loop, looping thru your simple task X number of times.
In the second, do it with the opening and closing of the connection inside the loop.
Change the X number of times to match your usage and expectations. That should give you a real good feel for the impact on your system.
Hope that helps you understand the basics... Jack.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With