Connection to the bank is open in static method?

Asked

Viewed 339 times

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 one example, but my class must necessarily be static.

The question is:
Because it is a static class and has a database connection, this connection is open at all times or is only opened when I call MinhaClasse.Salvar(Item); ?

I’m using Entity Framework.

  • Based on experience with another Framework I think it’s redundant db.Dispose() since using would automatically do this job. db.Dispose() how much using close the connection.

3 answers

4


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

Life cycle of the connection

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

Physical connection to the database is managed by ADO Net, that maintains a pool of connections to avoid creating a new connection every time the consumer needs one, so it offers better performance since creating connection to the database has high cost.

Keep the life of the context short and let the management of connections 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 already get rid of it.

Relationship between the class being static and the connection life cycle.

There is no relationship. The scope of the context or fact that you discard it (either by invoking the method Disposis that is to instantiate it with the using) is that will affect the life cycle of the connection. The fact that the class consuming the context 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 method call from your code Disposis for the using serves precisely to ensure this call.

  • 1

    Great answer. MeuEntities is inheriting yes from DbContext. Thank you very much, it was very clear!

3

I would question whether it really needs that the class needs to be static. Often we think it needs but in reality it doesn’t. But I’m not saying that this is a problem in itself.

I don’t really know how this class you’re using works, but I’m going to trust that it opens the connection. I know she opens some resource that needs to be closed. And you used it the right way, with using. The language is in charge of disposing the resource when it is no longer needed without your code having to worry about this.

Then the Dispose() used is unnecessary.

If the class MeuEntities was written correctly, the connection will be closed automatically. As it must be based on something written in the EF, it must be doing correctly.

1

Because it is a static class and has a database connection, this connection is open at all times or is only opened when I call MinhaClasse.Salvar(Item);?

Not.

The Dispose of DbContext calls the Dispose of DbConnection associated. It means that at the end of the using, 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 lifecycle of an ASP.NET MVC request does not have persistent connections, be they to databases or services. Entity Framework only follows the same premise, closing the connection to the database when it is not used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.