DAAB, the best approach to using database instances

Guys, I'm going to use Enterprise Library (4.1) and especially DAAB. Here I have questions:

  • What is the best approach and why:

    • Every time I need to start DbCommand I create a database instance using DatabaseFactory.CreateDatabase ();

    • I have a base class with an instanced Database (using the same Static CreateDatabase () method) and something like a public property that returns an instance database.

  • How is it "heavy" or fast / slow to create an instance of a database class? What if I do it every time DbCommand is required?

Thanks.

+4
source share
1 answer

It's not a problem. Creating a database class has a small overhead.

However, actually creating a database connection has a lot of overhead, which is why Windows does a connection pool. In short, the first time a process creates a connection to a database, it scans the connection pool for an existing connection with exactly the same connection string. If he does not find it, he creates a new one (expensive operation). When a process closes it and allows it to go out of scope, it does not actually close the connection to the database, it places it in the Connection Pool. Remains until the same process creates another connection with the same connection string. Then it gives you an existing one from the connection pool.

You can disable the connection pool (using the settings in the connection string), but this is usually a very bad idea.

0
source

All Articles