[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Do I need Connection Pooling with Entity Framework 4.0?

Posted on 2010-11-12
7
Medium Priority
?
1,580 Views
Last Modified: 2012-05-10
My MVC2 application has an entity name RDDB.edmx and I created a repository object to use an an accessor by my program called RDDBDataStore.  I find that I create instances of this object throughout my program but have had trouble creating instances of it with "using" statements as shown below.

1) Is there a way I can create one instance that gets used throughout my application? Could I do this in the Glaboal.asax file? One instance would solve the problem I have.

If I must create new instances of RDDBDataStore as needed:
2) Would Connection Pooling be useful to me?

The problem I have with the multiple "using" statement route is that sometimes I need a returned list to persist so that a view, for example, can display that list.  When I use a "using" statement, the object is gone and I get a null pointer exception.

I need to understand the best practices solution for this, so please feel free to make suggestions.

Thanks,
newbieweb

public class RDDBDataStore : IDisposable
{
    private RDDB entities = null;
    public RDDBDataStore()
    {
       entities = new RDDB();
    }

    public void Dispose()
    {
       Dispose(true);
       GC.SuppressFinalize(this);
    }
}

using (RDDBDataStore ds = new RDDBDataStore())
{
    // access ds here...
}

Open in new window

0
Comment
Question by:newbieweb
  • 3
  • 3
7 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1500 total points
ID: 34120401
Why is creating multiple instances an issue? Can you post a sample of something you are doing that is causing the problem? It doesn't sound like it is an issue the mechanics of ther repository itself.

Connection pooling, by the way, is seperate to your code and is based on the connection string being used, not on the object instantiation.
0
 

Author Comment

by:newbieweb
ID: 34120605
>Why is creating multiple instances an issue?
I thought that if I created lots and lots of instances, and did not encapsulate them in a "using" block, I may run into resource constraints since the Garbage Collector seems to take its sweet time before clean-up.  Also, since I would then be relying on accessing objects which are out of scope, that's bad practice.

So my solution was to encapsulate each instance of RDDBDataStore in a "using" block to force the GC to dispose of them.

> Connection pooling, by the way, is seperate to your code ...
If it's not something to be used in association with EF4, then fine. I just heard the term "Connection pooling" and wondered if it might help me solve the issue I have described. Since I let EF4 handle the connection string (right?), Connection pooling is not relevant, true?

Thanks for the help,
newbieweb

0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 1500 total points
ID: 34120654
Each instance of your repository should be creating its own instance of the EF entities object, there shouldn't be any need to persist a single copy across the application lifetime.

If you are performing multiple operations against the repository in a single call then you should simply be wrapping them all in a single "using" block.

Connection pooling is a mechanism for reducing the number of connections to the database, and reducing the overhead that is inherent with creating connection objects. What happens is that a pool of connections get created, held in memory, and every time you contact the database you take a connection from the pool that is not currently being used. This all happens behind the scenes so you don't need to do anything in code.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:newbieweb
ID: 34120836
> Each instance of your repository should be creating its own instance...
Good. I have been coding this way and have almost a hundred instances that get created thusly:

using (RDDBDataStore ds = new RDDBDataStore())
{
    // access ds here...
}

My problem is that certain times I have properties which return a list from the RDDBDataStore, and I need to make that list persist so that a View can iterate over that list. But the list is null since the RDDBDataStore is out of scope.  That actually is my core problem. How do I handle that?


> Connection pooling is a mechanism...
Can this be helpful to me using EF4? Or does EF4 manage the Connection pool itself?

0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 1500 total points
ID: 34120908
Not quite sure how you are handling things that would cause that issue. The repository is just a route through to the database. You shuold be returning data to the View, via your Model. The View itself should be completely unaware of the repository, which is why i'm not quite clear on how it's causing a problem for you.

>> Can this be helpful to me using EF4? Or does EF4 manage the Connection pool itself?

It's more of a performace thing for the database side of things. The pool is managed by the database server, not by EF. EF is just an abstraction over your database objects, it isn't providing any of these services itself.
0
 
LVL 8

Assisted Solution

by:cubaman_24
cubaman_24 earned 500 total points
ID: 34134855
Hello:
Connection pooling is a feature of your Ado.Net provider, not EF.
Find some best practices for MVC in here:
http://aspnetmvcbook.s3.amazonaws.com/aspnetmvc-nerdinner_v1.pdf
Best regards
0
 

Author Closing Comment

by:newbieweb
ID: 34164477
Thanks.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question