0

I'm looking for a way to improve my to access the database from WebParts.

Right now, each and every single time i need to get/set some data from the database (either with linq or with System.Data.SqlClient), I:

  • Retrieve the connection string from web config
  • Establish a new connection (time consuming)
  • Execute
  • Close connection (time consuming)
  • Release used objects

Is there a better way, e.g. establish a single connection on page load and "reuse" it on every calls, or use the same used by the sharepoint unlying application?

Thank you!

1
  • what about caching the connection? Commented Aug 18, 2015 at 10:21

1 Answer 1

0

If you create one per query / transaction, it is much easier to manage "closing" the connections.

I can see why common sense dictates that you should open one and use it throughout, but you will run into problems with dropped connections and multi threading. So your next step will be to open a pool, say of 50, connections and keep them all open, doling them out to different processes. And then you'll find out that this is exactly what the .NET framework does for you already.

If you open a connection when you need it and dispose of it when you've finished, that will not actually close the connection, it'll just return it to the connection pool to be used again.

Source: Accepted answer of: Creating database connections - Do it once or for each query?

2
  • thank you, I didn't know that was automated, in this specific case, the connection pools are managed at sharepoint "web application level"? Commented Aug 20, 2015 at 12:03
  • yes, for more info please refer msdn.microsoft.com/en-us/library/8xx3tyca(v=vs.110).aspx Commented Aug 21, 2015 at 4:55

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.