Is the connection to the bank open in static mode?

3

I have the following static class available for the entire application:

public static class MinhaClasse
{
   public static void Salvar(Item meuItem)
   {
       using (MeuEntities db = new MeuEntities())
       {
           db.Item.Add(meuItem);
           db.SaveChanges();
           db.Dispose();
       }
   }
}
  

This is just an example, but my class must necessarily be   static.

The question is:
Because it is a static class and has a connection to the database, is this connection open every time or only open when I call MinhaClasse.Salvar(Item); ? I'm using Entity Framework .

    
asked by anonymous 23.09.2015 / 22:44

3 answers

4

MeuEntities is a class generated by the Entity Framework , inheriting from DbContext, right?

Connection life cycle

Using the Entity Framework , you do not control the life cycle of the connection, but rather the context life cycle. At specific times the context will require a connection with the bank to seek or persist entities.

The physical connection to the database is managed by ADO.Net , which maintains a pool of connections to avoid creating a new connection every time the consumer needs one, offers better performance as creating a connection to the database has a high cost.

  

Keep context life short and leave the connection management with the Entity Framework and ADO.Net that it uses underneath.

And this you are already doing since you create the context just to persist and you undo it.

The relation between the class being static and the life cycle of the connection.

There is no relationship. The scope of the context or fact that you undo it (either by invoking the dispose method or by instantiating it with using ) is going to affect the connection life cycle. The fact that the context-consuming class is static has no relation.

Now, if you declare the context in a static variable and never get rid of it, then you can negatively affect the connection lifecycle.

Finally

Remove the call to the dispose method from your code as using method serves to secure this call.

    
23.09.2015 / 23:40
3

I would question if you really need the class to be static. Many times we think we need it, but we do not really need it. But I'm not saying this is a problem in itself.

I'm not sure how this class you're using will work but I'll trust it to open the connection. I know it opens some feature that needs to be closed. And you used it correctly, with using . The language will take care of the resource when it is no longer necessary without your code having to worry about it.

So the% w / o% used is unnecessary.

If the Dispose() class was spelled correctly, the connection will be closed automatically. Since it should be based on something written in EF, it should be doing it correctly.

    
23.09.2015 / 23:22
1
  

Because it is a static class and has a connection to the database, is this connection open every time or only open when I call MinhaClasse.Salvar(Item); ?

No.

O% with Dispose calls DbContext of Dispose associated . Means that, at the end of DbConnection , the connection is closed.

That is, with each call of your static method, a new connection is opened.

For Web systems, this is correct, since the life cycle of an ASP.NET MVC request does not rely on persistent connections, whether to databases or services. The Entity Framework only follows the same premise, closing the connection to the bank when it is not used.

    
23.09.2015 / 23:40