How to reconnect to a lost connection when it is lost using Linq to SQl automagically
i don't want to encapsulate every call in a try catch block
is there an event i can subscribe to, for when a connection is lost or timesout so that i can reconnect..
or some way to not check every linq query开发者_如何学编程.
and if not why not!
Edit: Does Linq to Entity's have this?
Edit: I'm using Microsoft SQL Azure, and it drops connections alot.
Edit: I open a new connection on every call to the db. so its not that I'm leaving the connection open.
Even if there were such event (not sure) you still could not rely on it. A network connection can suddenly get broken, database engine be busy doing some other query a timeout is always a possibility. Better watch directly a query if it's succeeded or failed, regardless of the reason.
You could consider writing your own LINQ provider (using much of the existing one's functionality) that adds exactly this feature. It might turn out to be less work than handling the exception each and every time.
Why are you getting so many drops? Are you hanging on to a data context object for a long time? It's cheap to construct one, so just construct a new one when you start a new unit of work.
From this msdn blog
It is better to err on the side of shorter lifetime - a unit of work or even a single request for stateless servers are good patterns to start with.
精彩评论