Username: Password:

数据库高级应用-数据库专栏,SQL Server
来源:作者: 发布时间:2007-12-25 13:44:05


一、连接池

     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实现:能够在中间层来管理事务。sqlconnection和oledbconnection对象有一个begintransaction方法,能够返回sqltransaction或oledbtransaction对象。

    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+对象上下文信息,他提供setcomplete和setabort方法,以便分别显示提交和回滚事务。

    长处:在需要事务跨msmq和其他可识别事务的资源运行系统中,只能使用dtc或com+事务。dtc协调参和分布式事务的任何资源管理器,也管理和事务相关的操作。

    缺点:由于存在dtc和com互相操作性开销,导致性能降低。

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消息队列资源管理器)时;

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

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅