DAAB,使用数据库实例的最佳方法是 [英] DAAB, the best approach to use Database instances is

查看:238
本文介绍了DAAB,使用数据库实例的最佳方法是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我要使用企业库(4.1),特别是DAAB。这里有我的问题:

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


  1. 什么是最好的方法和原因:

  1. What is the best approach and why:


  • 每当我需要运行
    DbCommand时,我使用
    创建数据库实例
    DatabaseFactory.CreateDatabase();

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

我有一个基类实例
数据库(使用相同的
CreateDatabase()静态方法)和
类似public property
返回实例化数据库。

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

创建一个Database类的实例?如果我每次在需要DbCommand时都这样做呢?

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?

谢谢。

推荐答案

这不是问题。创建数据库类的开销低。

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

但是,实际创建数据库连接的开销很大,这就是Windows做连接池的原因。简而言之,进程创建数据库连接时,它首先在连接池中查找具有完全相同的连接字符串的现有连接。如果它没有找到一个,它创建一个新的(一个昂贵的操作)。当进程关闭它并让它超出范围时,它实际上并不关闭到DB的连接,它将它放在连接池中。存在停留,直到相同的进程使用相同的连接字符串创建另一个连接。然后它从连接池中给你已经存在的一个。

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.

这篇关于DAAB,使用数据库实例的最佳方法是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆