471,084 Members | 974 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,084 software developers and data experts.

Single Linq Context in WinForms application

For winforms application with multiple related forms it is reasonable to
create Linq database context object in start of application.
Context object is released only when application exits.

So connection to database may remain open for all application run time.
For such a long time, some routers close connection. This causes Connection
is broken error in application.

How to fix this ?

Andrus.
Jan 13 '08 #1
4 4518
Andrus,

Generally speaking, a context is not meant to be open for the life of an
application. It implies that you want to keep track of all the objects that
were ever created, or ever will be created in the scope of your app, and
generally, that is way too broad.

Rather, when you have a specific group of operations that you are going
to perform, you should create a new instance of the context, use it, and
then dispose of it. Your app is not one giant context, rather, you have
multiple sub-functions which really are their own contexts. You need to
identify these and then work from there.

Also, I seriously doubt that the context is keeping the database
connections open. That's just horrible practice. While I am not positive,
I am guessing that there is some connection pooling going on and that those
connections are what are actually being dropped. The actual connections in
code are closed correctly. Keeping them open would provide no benefits to
the context.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrus" <ko********@hot.eewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
For winforms application with multiple related forms it is reasonable to
create Linq database context object in start of application.
Context object is released only when application exits.

So connection to database may remain open for all application run time.
For such a long time, some routers close connection. This causes
Connection
is broken error in application.

How to fix this ?

Andrus.
Jan 13 '08 #2
Also, I seriously doubt that the context is keeping the
database connections open.
Actually, based on the other problems Andrus has seen with DbLinq
(i.e. not LINQ-to-SQL), it wouldn't surprise me in the least...

Marc
Jan 14 '08 #3

Nicholas,
Generally speaking, a context is not meant to be open for the life of
an application. It implies that you want to keep track of all the objects
that were ever created, or ever will be created in the scope of your app,
and generally, that is way too broad.

Rather, when you have a specific group of operations that you are going
to perform, you should create a new instance of the context, use it, and
then dispose of it. Your app is not one giant context, rather, you have
multiple sub-functions which really are their own contexts. You need to
identify these and then work from there.
I'm creating multi form winforms application working with server over
internet.
I need to cache data so that every form does not read data from database.

Only way in Linq to do this is use Context object.
So I need to use single global context in application as global cache.

However I have no idea how to edit data for some form.

Should I create new context for every form for data editing and keep this
context open until form is closed ?
If so how to refresh global context when changes to form temporary context
are submitted ?

Andrus.
Jan 14 '08 #4
Andrus,

Caching the data on the app level is just too broad. Generally, if your
individual forms are going to handle a single operation (not a single data
operation, but it is a logical context), then you should load the data when
the form opens and close the context when the form is closed (or saved,
depending on the workflow of your app).

Generally, you cache data which is not going to be modified very often.
That's the whole point of caching. If you were to keep a cache using the
context of all the items in the database, you are going to have a nightmare
when it comes to concurrency checking, since you would probably have to
re-load the data anyways anytime you open a new form (depending on the
frequency of changes).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andrus" <ko********@hot.eewrote in message
news:O9**************@TK2MSFTNGP04.phx.gbl...
>
Nicholas,
> Generally speaking, a context is not meant to be open for the life of
an application. It implies that you want to keep track of all the
objects that were ever created, or ever will be created in the scope of
your app, and generally, that is way too broad.

Rather, when you have a specific group of operations that you are
going to perform, you should create a new instance of the context, use
it, and then dispose of it. Your app is not one giant context, rather,
you have multiple sub-functions which really are their own contexts. You
need to identify these and then work from there.

I'm creating multi form winforms application working with server over
internet.
I need to cache data so that every form does not read data from database.

Only way in Linq to do this is use Context object.
So I need to use single global context in application as global cache.

However I have no idea how to edit data for some form.

Should I create new context for every form for data editing and keep this
context open until form is closed ?
If so how to refresh global context when changes to form temporary context
are submitted ?

Andrus.

Jan 14 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by David Veeneman | last post: by
28 posts views Thread by Marc Gravell | last post: by
3 posts views Thread by TisMe | last post: by
reply views Thread by =?Utf-8?B?RXJpYyBGYWxza2Vu?= | last post: by
1 post views Thread by Dean Slindee | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.