English 中文(简体)
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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签