Solved

Do I need Connection Pooling with Entity Framework 4.0?

Posted on 2010-11-12
7
1,505 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 375 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 375 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 375 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 125 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now