DAAB, the best approach to use Database instances is

坚强是说给别人听的谎言 提交于 2019-12-22 08:25:32

问题


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

  1. What is the best approach and why:

    • Every time when I need to run a DbCommand I create Database instance using DatabaseFactory.CreateDatabase();

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

  2. How it is “heavy” or fast/slow to create an instance of Database class? What if I do it every time when a DbCommand is needed?

Thank you.


回答1:


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

However, actually creating the database connection has high overhead, which is why Windows does Connection Pooling. Briefly, the first time a process creates a DB Connection, it looks in the connection pool for an existing connection with exactly the same connection string. If it doesn't find one, it creates a new one (an expensive operation). When th process closes it and lets it go out of scope, it doesn't actually close the connection to the DB, it puts it in the Connection Pool. There is stays until the same process creates another connection with the same connection string. Then it gives you the already existing one from the Connection Pool.

You can turn off connection pooling (via a setting in the connection string) but that's usually a very bad idea.



来源:https://stackoverflow.com/questions/359028/daab-the-best-approach-to-use-database-instances-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!