数据库高级应用

一、连接池

1 、特点:

> > ① 、优点:性能 >> >> ② 、缺点:可能存在多个没有被使用的连接,资源浪费 > > 2 、 ** ADO ** . NET 连接池 > >> ① 包含在 ADO.NET 中的每个 .NET 数据提供程序都可实现连接池。 >> >> ②每个连接池都与一个独立的连接字符串及其事务上下文关联。每次打开一个新的连接,数据提供者会尝试将指定的连接字符串与连接池的字符串进行匹配。如果失败,则创建新连接并加入连接池。 >> >> ③连接池创建之后,系统会创建一些连接对象并将它们加入连接池,直至达到额定的最小连接对象数量。以后根据需要创建新的连接,直到达到最大连接数量。 >> >> ④.NET 默认是使用连接池。如果想禁用,则可以使用以下方式: >> >>> Ⅰ、使用 SqlConnection 对象时,在连接字符串加入: Pooling = false >>> >>> Ⅱ、使用 OleDBConnection 对象时,在连接字符串加入: OLE DB Services = -4

3 、提示和技巧

> > ①打开连接应迟,关闭连接应早 >> >> ②在关闭数据库连接前确保关闭了所有用户定义的事务 >> >> ③至少保证连接池中有一个连接可用

二、缓存

1 、特点

> > ① 、优点:提高性能,稳定性,可用性 >> >> ② 、 ASP.NET 缓存 >> >>> Ⅰ、在 ASP.NET 中,提供了专门用于缓存数据的 Cache 对象,它的应用范围是应用程序域。生存期是和应用程序紧密相关的,每当应用程序启动的时候就重新创建 Cache 对象,每当应用程序启动的时候就重新创建 Cache 对象。它与 Application 对象的主要区别就是提供了专门用于缓存管理的性能,比如依赖和过期策略。 >>> >>> Ⅱ 、 Cache 对象定义在 System.Web.Caching 命名空间,可以使用 HttpContext 类的 Cache 属性或 Page 对象的 Cache 属性来得到 Cache 的引用, Cache 对象除了存储键值以外,还可以存储 .NET 框架的对象。 > > 2 、依赖和过期策略 > >> ①文件策略:当硬盘上的某个 ( 某些 )文件更改时,强制移除缓存数据 >> >> CacheDependency cDependency = new CacheDependency ( Server . MapPath ( “ authors.xml ” )); >> >> Cache . Insert ( “ CachedItem ” ,item,cDependency); >> >> ②键值依赖:指定缓存中的某个数据项更改时移除。 >> >> //Create a cache entry >> >> Cache [ “ key1 ”] = “ Value1 ” ; >> >> //Make key2 dependent on key1 >> >> String [] dependencyKey = new String [1]; >> >> dependencyKey[0] = “ Key1 ” ; >> >> CacheDependency dependency = new CacheDependency (null,dependencyKey); >> >> Cache . Insert ( “ key2 ”,“ Value2 ”, dependency); >> >> ③基于时间的过期策略:绝对和相对 >> >> //Absolute expiration >> >> Cache . Insert ( “ CachedItem ” ,item,null,DateTime.Now,AddSeconds(5),Cache.NoSlidingExpiration); >> >> //Sliding expiration >> >> Cache . Insert ( “” ,item,null,Cache.NoAbsoluteExpiration,TimeSpan.FromSeconds(5)); >> >> ④数据库依赖 ( 建议不要使用 ): 数据库中相关的数据发生变化,则缓存失效

3 、使用缓存 : 由于数据会过期,使用缓存时必须检查数据的有效性

string data = ( string ) Cache [ “ MyItem ” ];

if(data==null)

{

data=GetData();

Cache . Insert ( “ MyItem ”, data);

}

4 、缓存回调:当缓存失效时,自动调用

CacheItemRemoveCallback onRemove = new CacheItemRemovedCallack (this.RemoveCallback);

Cache . Insert ( “ CachedItem ” ,item,null,Cache.NoAbsoluteExpiration,Cache.NoSlidingExpiration,CacheItemPriority.Default,onRemove);

//Implement the function to handle the expiration of the cache.

public void RemovedCallback(string key,obejct value,CacheItemRemonvedReason r)

{

//Test whether the item is expired and reinsert it into the cache.

if(r==CacheItemRemovedReason.Expired)

{

//Reinsert it into the cache again.

** CacheItemRemovedCallback ** onRemove == null;

onRemove = new CacheItemRemoveCallback (this.RemovedCallback);

** Cache ** . Insert (key,value,null,Cache.NoAbsoluteExpiration,Cache.NoSlidingExpiration,CacheItemPriority.Default,onRemove);

}

}

5 、缓存优先级:当运行应用程序服务器的内存不足时,会自动清楚缓存中的数据,称为“清除 scavenging ”

Cache . Insert ( “ DSN ” ,connectionString,null,d,t,CacheItemPriority.High,onRemove);

6 、在非 Web 项目中使用 ASP.NET 缓存

** HttpRuntime ** .Cache 对象可以在 Aspnet_wp.exe 之外的每个应用程序域中存在。

HttpRuntime httpRT = new HttpRuntime ();

Cache cache = HttpRuntime . Cache ;

三、事务

> 1 、直接写入到 sql 中 :在存储过程中使用 Begin Trans,Commit Trans,RollBack Trans 实现。 > > Begin Trans > > Declare @orderDetailsError int,@productError int > > Delete from "Order Details" where ProductID = 42 > > Select @orderDetailsError = @@Error > > Delete from Products where ProductID = 42 > > Select @productError = @@Error > > if @orderDetailsError = 0 and @productError = 0 > > Commit Trans > > else > > RollBack Trans > > 优点:所有的事务逻辑包含在一个单独的调用中 > > 拥有运行一个事务的最佳性能 > > 独立于应用程序 > > 限制:事务上下文仅存在于数据库调用中 > > 数据库代码与数据库系统有关 > > 2、使用 ADO.NET 实现:可以在中间层来管理事务。 SqlConnectionOleDBConnection 对象有一个 BeginTransaction 方法,可以返回 SqlTransactionOleDbTransaction 对象。 > > cn.Open(); > > SqlTransaction trans = cn.BeginTransaction(); > > SqlCommand cmd = new SqlCommand(); > > try > > { > > cmd.CommandText = "Delete [Order Details] where ProductID = 23"; > > cmd.ExecuteNonQuery(); > > cmd.CommandText = "Delete Products where ProductID =23"; > > cmd.ExecuteNonQuery(); > > Trans.Commit(); > > } > > Catch(Exception e) > > trans.Rollback(); > > Finally > > cn.Close(); > > 优点:简单性;和数据库事务差不多的快;独立于数据库 > > 缺点:事务不能跨越多个数据库连接; > > 事务执行在数据库连接层上,所以需要在事务过程中维护一个数据库连接;

四、分布式事务

1、特点:

> 要参与 COM+ 事务, .NET 类必须是从 System.EnterpriseServices.ServicedComponent 类继承。 > > 通过使用 System.EnterpriseServices.ContextUtil 类可以获取有关 COM+ 对象上下文信息,它提供 SetCompleteSetAbort 方法,以便分别显示提交和回滚事务。 > > 优点:在需要事务跨 MSMQ 和其他可识别事务的资源运行系统中,只能使用 DTCCOM+ 事务。 DTC 协调参与分布式事务的所有资源管理器,也管理与事务相关的操作。 > > 缺点:由于存在 DTCCOM 互相操作性开销,导致性能降低。 > > 2、事务类型 : > >> ① 、自动事务 : 使用 System.EnterpriseServices.AutoComplete 属性 >> >> [Transaction(TransactionOption.Required)] >> >> public class Class1:ServicedComponent >> >> { >> >> [AutoComplete] >> >> public void Example() >> >> {} >> >> } >> >> ② 、手动事务 >> >>> public string TransferMoneyFromBToA(double m) >>> >>> { >>> >>> try >>> >>> { >>> >>> ContextUtil.EnableCommit(); >>> >>> this.TransferOutFromB(m); >>> >>> this.TransferInToA(m); >>> >>> ContextUtil.SetComplete(); >>> >>> } >>> >>> catch(Exception err) >>> >>> { >>> >>> ContextUtil.SetAbort(); >>> >>> } >>> >>> } > > 3 、方式选择 > >> 对于下面的情况,需使用手工事务处理:对单个数据库执行事务处理; >> >> 对于下面的情况,则宜使用自动事务处理: >> >>> ① 、 需要将单个事务处理扩展到多个远程数据库时; >>> >>> ②、 需要单个事务处理拥有多个资源管理器 ( 如数据库和 Windows2000 消息队列资源管理器 ) 时 ;

注意:避免混用事务处理模型,最好只使用其中一个。

Published At
Categories with 数据库类
comments powered by Disqus