Solved

Do I need Connection Pooling with Entity Framework 4.0?

Posted on 2010-11-12
7
1,517 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change Format on Data when exporting to Excel 4 23
SSL https .net web site force redirect 3 36
SQL Exceptions 3 39
use of unassigned local variable 'id' 6 27
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

15 Experts available now in Live!

Get 1:1 Help Now