一、連接池
     1、特點(diǎn):
①、優(yōu)點(diǎn):性能
②、缺點(diǎn):可能存在多個(gè)沒有被使用的連接,資源浪費(fèi)
2、ado.net連接池
①包含在ado.net中的每個(gè).net數(shù)據(jù)提供程序都可實(shí)現(xiàn)連接池。
②每個(gè)連接池都與一個(gè)獨(dú)立的連接字符串及其事務(wù)上下文關(guān)聯(lián)。每次打開一個(gè)新的連接,數(shù)據(jù)提供者會(huì)嘗試將指定的連接字符串與連接池的字符串進(jìn)行匹配。如果失敗,則創(chuàng)建新連接并加入連接池。
③連接池創(chuàng)建之后,系統(tǒng)會(huì)創(chuàng)建一些連接對(duì)象并將它們加入連接池,直至達(dá)到額定的最小連接對(duì)象數(shù)量。以后根據(jù)需要?jiǎng)?chuàng)建新的連接,直到達(dá)到最大連接數(shù)量。
④.net默認(rèn)是使用連接池。如果想禁用,則可以使用以下方式:
ⅰ、使用sqlconnection對(duì)象時(shí),在連接字符串加入:pooling = false
ⅱ、使用oledbconnection對(duì)象時(shí),在連接字符串加入:ole db services = -4
     3、提示和技巧
①打開連接應(yīng)遲,關(guān)閉連接應(yīng)早
②在關(guān)閉數(shù)據(jù)庫連接前確保關(guān)閉了所有用戶定義的事務(wù)
③至少保證連接池中有一個(gè)連接可用
二、緩存
     1、特點(diǎn)
①、優(yōu)點(diǎn):提高性能,穩(wěn)定性,可用性
②、asp.net緩存
ⅰ、在asp.net中,提供了專門用于緩存數(shù)據(jù)的cache對(duì)象,它的應(yīng)用范圍是應(yīng)用程序域。生存期是和應(yīng)用程序緊密相關(guān)的,每當(dāng)應(yīng)用程序啟動(dòng)的時(shí)候就重新創(chuàng)建cache對(duì)象,每當(dāng)應(yīng)用程序啟動(dòng)的時(shí)候就重新創(chuàng)建cache對(duì)象。它與application對(duì)象的主要區(qū)別就是提供了專門用于緩存管理的性能,比如依賴和過期策略。
ⅱ、cache對(duì)象定義在system.web.caching命名空間,可以使用httpcontext類的cache屬性或page對(duì)象的cache屬性來得到cache的引用,cache對(duì)象除了存儲(chǔ)鍵值以外,還可以存儲(chǔ).net框架的對(duì)象。
2、依賴和過期策略
①文件策略:當(dāng)硬盤上的某個(gè)(某些)文件更改時(shí),強(qiáng)制移除緩存數(shù)據(jù)
    cachedependency cdependency = new cachedependency(server.mappath(“authors.xml”));
    cache.insert(“cacheditem”,item,cdependency);
②鍵值依賴:指定緩存中的某個(gè)數(shù)據(jù)項(xiàng)更改時(shí)移除。
    //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);
③基于時(shí)間的過期策略:絕對(duì)和相對(duì)
    //absolute expiration
    cache.insert(“cacheditem”,item,null,datetime.now,addseconds(5),cache.noslidingexpiration);
    //sliding expiration
    cache.insert(“”,item,null,cache.noabsoluteexpiration,timespan.fromseconds(5));
④數(shù)據(jù)庫依賴(建議不要使用):數(shù)據(jù)庫中相關(guān)的數(shù)據(jù)發(fā)生變化,則緩存失效
    3、使用緩存:由于數(shù)據(jù)會(huì)過期,使用緩存時(shí)必須檢查數(shù)據(jù)的有效性
        string data = (string)cache[“myitem”];     
        if(data==null)
        {
            data=getdata();
            cache.insert(“myitem”,data);
        }
    4、緩存回調(diào):當(dāng)緩存失效時(shí),自動(dòng)調(diào)用
        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、緩存優(yōu)先級(jí):當(dāng)運(yùn)行應(yīng)用程序服務(wù)器的內(nèi)存不足時(shí),會(huì)自動(dòng)清楚緩存中的數(shù)據(jù),稱為“清除scavenging”
        cache.insert(“dsn”,connectionstring,null,d,t,cacheitempriority.high,onremove);
    6、在非web項(xiàng)目中使用asp.net緩存
        httpruntime.cache對(duì)象可以在aspnet_wp.exe之外的每個(gè)應(yīng)用程序域中存在。
        httpruntime httprt = new httpruntime();
        cache cache = httpruntime.cache;
三、事務(wù)
1、直接寫入到sql中 :在存儲(chǔ)過程中使用begin trans,commit trans,rollback trans實(shí)現(xiàn)。
    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
優(yōu)點(diǎn):所有的事務(wù)邏輯包含在一個(gè)單獨(dú)的調(diào)用中
      擁有運(yùn)行一個(gè)事務(wù)的最佳性能
      獨(dú)立于應(yīng)用程序
限制:事務(wù)上下文僅存在于數(shù)據(jù)庫調(diào)用中
      數(shù)據(jù)庫代碼與數(shù)據(jù)庫系統(tǒng)有關(guān)
2、使用ado.net實(shí)現(xiàn):可以在中間層來管理事務(wù)。sqlconnection和oledbconnection對(duì)象有一個(gè)begintransaction方法,可以返回sqltransaction或oledbtransaction對(duì)象。
    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();
優(yōu)點(diǎn):簡單性;和數(shù)據(jù)庫事務(wù)差不多的快;獨(dú)立于數(shù)據(jù)庫
缺點(diǎn):事務(wù)不能跨越多個(gè)數(shù)據(jù)庫連接;
      事務(wù)執(zhí)行在數(shù)據(jù)庫連接層上,所以需要在事務(wù)過程中維護(hù)一個(gè)數(shù)據(jù)庫連接;
四、分布式事務(wù)
    1、特點(diǎn):
    要參與com+事務(wù),.net類必須是從system.enterpriseservices.servicedcomponent類繼承。
    通過使用system.enterpriseservices.contextutil類可以獲取有關(guān)com+對(duì)象上下文信息,它提供setcomplete和setabort方法,以便分別顯示提交和回滾事務(wù)。
    優(yōu)點(diǎn):在需要事務(wù)跨msmq和其他可識(shí)別事務(wù)的資源運(yùn)行系統(tǒng)中,只能使用dtc或com+事務(wù)。dtc協(xié)調(diào)參與分布式事務(wù)的所有資源管理器,也管理與事務(wù)相關(guān)的操作。
    缺點(diǎn):由于存在dtc和com互相操作性開銷,導(dǎo)致性能降低。
2、事務(wù)類型:
①、自動(dòng)事務(wù):使用system.enterpriseservices.autocomplete屬性
    [transaction(transactionoption.required)]
    public class class1:servicedcomponent
    {
        [autocomplete]
        public void example()
        {}
    }
②、手動(dòng)事務(wù)
public string transfermoneyfrombtoa(double m)
    {
        try
        {
            contextutil.enablecommit();
            this.transferoutfromb(m);
            this.transferintoa(m);
            contextutil.setcomplete();
        }
        catch(exception err)
        {
            contextutil.setabort();
        }
    }
3、方式選擇
對(duì)于下面的情況,需使用手工事務(wù)處理:對(duì)單個(gè)數(shù)據(jù)庫執(zhí)行事務(wù)處理;
對(duì)于下面的情況,則宜使用自動(dòng)事務(wù)處理:
①、需要將單個(gè)事務(wù)處理擴(kuò)展到多個(gè)遠(yuǎn)程數(shù)據(jù)庫時(shí);
②、需要單個(gè)事務(wù)處理擁有多個(gè)資源管理器(如數(shù)據(jù)庫和windows2000消息隊(duì)列資源管理器)時(shí);
          注意:避免混用事務(wù)處理模型,最好只使用其中一個(gè)。
國內(nèi)最大的酷站演示中心!