01-08-02【Nhibernate (版本3.3.1.4000) 出入江湖】二级缓存:NHibernate自带的HashtableProvider...
?
?
第一步驟:hibernate.cfg.xml文件補上如下配置:
<?xml version="1.0" encoding="utf-8"?> <!-- This template was written to work with NHibernate.Test. Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it for your own use before compile tests in VisualStudio. --> <!-- This is the System.Data.dll provider for SQL Server --> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" ><session-factory name="NHibernate.Test123456"><property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property><property name="connection.connection_string"><!--用于測試自動生成數據庫表(不自動生成數據庫)--><!--<property name="hbm2ddl.auto">update</property>-->Server=(local);initial catalog=NHibernateSampleAutoCreateTable;Integrated Security=SSPI<!--Server=(local);initial catalog=NHibernateSample;Integrated Security=SSPI--></property><property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property><!--輸出所有的SQL語句到控制臺,一般開發打開這個--><property name="show_sql">true</property><!--整齊的SQL輸出到控制臺--><property name="format_sql">true</property><!--自動生成數據庫表(不自動生成數據庫)--><property name="hbm2ddl.auto">update</property><!--在數據表設計中如果采用了 bit 類型的字段,并且對應了業務類中類型為 bool 值,一定要如上設置下--><property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> <!--=======加入Nhibernate自身的HashtabeCache的二級緩存配置=============================--><!--1.配置二級緩存提供程序--><property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider</property><!--2.顯式啟用二級緩存--><property name ="cache.use_second_level_cache">true</property><!--4.啟動查詢緩存--><property name="cache.use_query_cache">true</property><!--實體類所在的程序集--><mapping assembly="Model"/><!--3.配置映射的二級緩存--><class-cache class="Model.Customer,Model" usage="read-write"/><!--<collection-cache collection ="集合名稱" region="默認集合名稱"usage="read-write"/>--></session-factory> </hibernate-configuration>?
實體類上也可以配二級緩存策略,如:
Customer.hbm.xml
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Model" assembly="Model" default-lazy="true"><class name="Model.Customer, Model"table="Customer"discriminator-value="0" lazy="false"><!--1.這個不是必須的,因為在nhibernate.cfg.xml文件中已經有了一個總配置2.cache標簽必須在id標簽前面--><cache usage="read-write"/><!--unsaved-value="0" 主鍵表中不需要定義,而是需要在子表中定義--><id name="CustomerId"column="CustomerId"type="int" unsaved-value="0"><generator class="native" /><!-- unsaved-value used to be null and generator was increment in h2.0.3 --></id> 。。。。。。。。。。。。。。。<set name="Orders" table="Order" lazy="true"generic="true"inverse="false" cascade="all"><!--二級緩存策略--><cache usage="read-write"/><key column="CustomerId" foreign-key="FK_CustomerOrders"/> <one-to-many class="Model.Order,Model"/></set></class> </hibernate-mapping>?
測試1:
? ? ? ?二級緩存與Get方法+Lazy設置的關系:
測試圖解:
?
經測試,得出如下結論:
?
Get方法+Lazy配置+二級緩存測試結果:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Customer.hbm.xml的<Set name="Orders" ? ? ? ?Customer.hbm.xml的<Set name="Orders" ?? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lazy="true"> ?? ?lazy="false"> ??? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<cache usage="read-write"/> ? ? ? ? ? ? ? ? ? ? ?<cache usage="read-write"/>
?
Customer數據庫中Order個數等于零 ? ? ? ? ? ? ? ? ??Get方法從二級緩存獲取Customer ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??Get方法從二級緩存獲取Customer?
Customer數據庫中Order個數大于零 ? ?? ? ? ? ? ? ? ?Get方法從二級緩存獲取Customer? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Get方法從不從二級緩存獲取Customer?
?
?
?
------------------------更新二級緩存測試---------------------------------------------------------------
測試更新-1:
前置條件:
1.Customer沒Orders
2.
<class name="Model.Customer, Model"table="Customer"discriminator-value="0" lazy="true"><!--1.這個不是必須的,因為在nhibernate.cfg.xml文件中已經有了一個總配置2.cache標簽必須在id標簽前面--><cache usage="read-write"/>測試代碼:
?
[TestMethod]public void TestSessionFactoryCacheUpdateCustomerHavingNotOrders(){CustomerService customerService = new CustomerService();Customer customer = new Customer(){FirstName = "Test",LastName = "TestSessionFactoryCache",Age = 10};customerService.Add(customer);int customerId = customer.CustomerId;Console.WriteLine("第1次獲取數據并且更新------------------------------:");Customer customerGet1 = customerService.Get(customerId);Console.WriteLine("更新前:Id:{0}-->FirtstName:{1}", customerGet1.CustomerId, customerGet1.FirstName);customerGet1.FirstName = "我是更新后的FirstName"; //修改FirstNamecustomerService.Update(customerGet1); //保存更新得到數據庫。 Console.WriteLine("更新后:Id:{0}-->FirtstName:{1}", customerGet1.CustomerId, customerGet1.FirstName);Console.WriteLine("第2次獲取數據==============================:");Customer customerGet2 = customerService.Get(customerId);Console.WriteLine("Id:{0}-->FirtstName:{1}", customerGet2.CustomerId, customerGet2.FirstName);Console.WriteLine("第3次獲取數據==============================:");Customer customerGet3 = customerService.Get(customerId);Console.WriteLine("Id:{0}-->FirtstName:{1}", customerGet3.CustomerId, customerGet3.FirstName);} View Code?
測試結果:
NHibernate: INSERT INTOCustomer(Version, Firstname, Lastname, Age) VALUES(@p0, @p1, @p2, @p3);selectSCOPE_IDENTITY();@p0 = 1 [Type: Int32 (0)],@p1 = 'Test' [Type: String (4000)],@p2 = 'TestSessionFactoryCache' [Type: String (4000)],@p3 = 10 [Type: Int32 (0)] 第1次獲取數據并且更新------------------------------: NHibernate: SELECTcustomer0_.CustomerId as CustomerId0_0_,customer0_.Version as Version0_0_,customer0_.Firstname as Firstname0_0_,customer0_.Lastname as Lastname0_0_,customer0_.Age as Age0_0_ FROMCustomer customer0_ WHEREcustomer0_.CustomerId=@p0;@p0 = 198 [Type: Int32 (0)] NHibernate: SELECTorders0_.CustomerId as CustomerId1_,orders0_.OrderId as OrderId1_,orders0_.OrderId as OrderId1_0_,orders0_.Version as Version1_0_,orders0_.OrderDate as OrderDate1_0_,orders0_.CustomerId as CustomerId1_0_ FROM[Order] orders0_ WHEREorders0_.CustomerId=@p0;@p0 = 198 [Type: Int32 (0)] 更新前:Id:198-->FirtstName:Test NHibernate: UPDATECustomer SETVersion = @p0,Firstname = @p1,Lastname = @p2,Age = @p3 WHERECustomerId = @p4 AND Version = @p5;@p0 = 2 [Type: Int32 (0)], @p1 = '我是更新后的FirstName' [Type: String (4000)], @p2 = 'TestSessionFactoryCache' [Type: String (4000)], @p3 = 10 [Type: Int32 (0)], @p4 = 198 [Type: Int32 (0)], @p5 = 1 [Type: Int32 (0)] 更新后:Id:198-->FirtstName:我是更新后的FirstName 第2次獲取數據==============================: Id:198-->FirtstName:我是更新后的FirstName 第3次獲取數據==============================: Id:198-->FirtstName:我是更新后的FirstName View Code?
?
---------------------------------------------------
測試更新-2
前置條件:
1.Customer有Orders
2.
<set name="Orders" table="Order" lazy="false"generic="true"inverse="false" cascade="all"><!--二級緩存策略--><cache usage="read-write"/><key column="CustomerId" foreign-key="FK_CustomerOrders"/> <one-to-many class="Model.Order,Model"/></set>?
測試代碼和結果:表明2級緩存失效
[TestMethod]public void TestSessionFactoryCacheUpdateCustomerHavingOrders(){CustomerService customerService = new CustomerService();Customer customer = new Customer(){FirstName = "Test",LastName = "TestSessionFactoryCache",Age = 10};Order order1 = new Order(){OrderDate = DateTime.Now,Customer = customer};customer.Orders.Add(order1);customerService.Add(customer);int customerId = customer.CustomerId;Console.WriteLine("第1次獲取數據并且更新------------------------------:");Customer customerGet1 = customerService.Get(customerId);Console.WriteLine("更新前:Id:{0}-->FirtstName:{1}", customerGet1.CustomerId, customerGet1.FirstName);customerGet1.FirstName = "我是更新后的FirstName"; //修改FirstNamecustomerService.Update(customerGet1); //保存更新得到數據庫。 Console.WriteLine("更新后:Id:{0}-->FirtstName:{1}", customerGet1.CustomerId, customerGet1.FirstName);Console.WriteLine("第2次獲取數據==============================:");Customer customerGet2 = customerService.Get(customerId);Console.WriteLine("Id:{0}-->FirtstName:{1}", customerGet2.CustomerId, customerGet2.FirstName);Console.WriteLine("第3次獲取數據==============================:");Customer customerGet3 = customerService.Get(customerId);Console.WriteLine("Id:{0}-->FirtstName:{1}", customerGet3.CustomerId, customerGet3.FirstName);} View Code NHibernate: INSERT INTOCustomer(Version, Firstname, Lastname, Age) VALUES(@p0, @p1, @p2, @p3);selectSCOPE_IDENTITY();@p0 = 1 [Type: Int32 (0)],@p1 = 'Test' [Type: String (4000)],@p2 = 'TestSessionFactoryCache' [Type: String (4000)],@p3 = 10 [Type: Int32 (0)] NHibernate: INSERT INTO[Order] (Version, OrderDate, CustomerId) VALUES(@p0, @p1, @p2);selectSCOPE_IDENTITY();@p0 = 1 [Type: Int32 (0)],@p1 = 2014/5/29 23:15:57 [Type: DateTime (0)],@p2 = 199 [Type: Int32 (0)] 第1次獲取數據并且更新------------------------------: NHibernate: SELECTcustomer0_.CustomerId as CustomerId0_0_,customer0_.Version as Version0_0_,customer0_.Firstname as Firstname0_0_,customer0_.Lastname as Lastname0_0_,customer0_.Age as Age0_0_ FROMCustomer customer0_ WHEREcustomer0_.CustomerId=@p0;@p0 = 199 [Type: Int32 (0)] NHibernate: SELECTorders0_.CustomerId as CustomerId1_,orders0_.OrderId as OrderId1_,orders0_.OrderId as OrderId1_0_,orders0_.Version as Version1_0_,orders0_.OrderDate as OrderDate1_0_,orders0_.CustomerId as CustomerId1_0_ FROM[Order] orders0_ WHEREorders0_.CustomerId=@p0;@p0 = 199 [Type: Int32 (0)] 更新前:Id:199-->FirtstName:Test NHibernate: UPDATECustomer SETVersion = @p0,Firstname = @p1,Lastname = @p2,Age = @p3 WHERECustomerId = @p4 AND Version = @p5;@p0 = 2 [Type: Int32 (0)], @p1 = '我是更新后的FirstName' [Type: String (4000)], @p2 = 'TestSessionFactoryCache' [Type: String (4000)], @p3 = 10 [Type: Int32 (0)], @p4 = 199 [Type: Int32 (0)], @p5 = 1 [Type: Int32 (0)] NHibernate: UPDATE[Order] SETVersion = @p0,OrderDate = @p1,CustomerId = @p2 WHEREOrderId = @p3 AND Version = @p4;@p0 = 2 [Type: Int32 (0)], @p1 = 2014/5/29 23:15:57 [Type: DateTime (0)], @p2 = 199 [Type: Int32 (0)], @p3 = 34 [Type: Int32 (0)], @p4 = 1 [Type: Int32 (0)] 更新后:Id:199-->FirtstName:我是更新后的FirstName 第2次獲取數據==============================: NHibernate: SELECTorder0_.OrderId as OrderId1_0_,order0_.Version as Version1_0_,order0_.OrderDate as OrderDate1_0_,order0_.CustomerId as CustomerId1_0_ FROM[Order] order0_ WHEREorder0_.OrderId=@p0;@p0 = 34 [Type: Int32 (0)] Id:199-->FirtstName:我是更新后的FirstName 第3次獲取數據==============================: NHibernate: SELECTorder0_.OrderId as OrderId1_0_,order0_.Version as Version1_0_,order0_.OrderDate as OrderDate1_0_,order0_.CustomerId as CustomerId1_0_ FROM[Order] order0_ WHEREorder0_.OrderId=@p0;@p0 = 34 [Type: Int32 (0)] Id:199-->FirtstName:我是更新后的FirstName View Code?
將該測試(測試更新-2)的前置條件2改為:
lazy="true" ,即修改后的配置如下:<set name="Orders" table="Order" lazy="true"generic="true"inverse="false" cascade="all"><!--二級緩存策略--><cache usage="read-write"/><key column="CustomerId" foreign-key="FK_CustomerOrders"/> <one-to-many class="Model.Order,Model"/></set>
?
lazy="true" 時,2級緩存生效,測試結果如下所示: NHibernate: INSERT INTOCustomer(Version, Firstname, Lastname, Age) VALUES(@p0, @p1, @p2, @p3);selectSCOPE_IDENTITY();@p0 = 1 [Type: Int32 (0)],@p1 = 'Test' [Type: String (4000)],@p2 = 'TestSessionFactoryCache' [Type: String (4000)],@p3 = 10 [Type: Int32 (0)] NHibernate: INSERT INTO[Order] (Version, OrderDate, CustomerId) VALUES(@p0, @p1, @p2);selectSCOPE_IDENTITY();@p0 = 1 [Type: Int32 (0)],@p1 = 2014/5/29 22:55:43 [Type: DateTime (0)],@p2 = 193 [Type: Int32 (0)] 第1次獲取數據并且更新------------------------------: NHibernate: SELECTcustomer0_.CustomerId as CustomerId0_0_,customer0_.Version as Version0_0_,customer0_.Firstname as Firstname0_0_,customer0_.Lastname as Lastname0_0_,customer0_.Age as Age0_0_ FROMCustomer customer0_ WHEREcustomer0_.CustomerId=@p0;@p0 = 193 [Type: Int32 (0)] 更新前:Id:193-->FirtstName:Test NHibernate: UPDATECustomer SETVersion = @p0,Firstname = @p1,Lastname = @p2,Age = @p3 WHERECustomerId = @p4 AND Version = @p5;@p0 = 2 [Type: Int32 (0)], @p1 = '我是更新后的FirstName' [Type: String (4000)], @p2 = 'TestSessionFactoryCache' [Type: String (4000)], @p3 = 10 [Type: Int32 (0)], @p4 = 193 [Type: Int32 (0)], @p5 = 1 [Type: Int32 (0)] 更新后:Id:193-->FirtstName:我是更新后的FirstName 第2次獲取數據==============================: Id:193-->FirtstName:我是更新后的FirstName 第3次獲取數據==============================: Id:193-->FirtstName:我是更新后的FirstName View Code?
經測試,得出如下結論:
?
Update方法+Get方法+Lazy配置+二級緩存測試結果:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Customer.hbm.xml的<Set name="Orders" ? ? ? ? ? ? ? ? Customer.hbm.xml的<Set name="Orders" ?? ? ?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? lazy="true"> ?? ?lazy="false"> ?????
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?<cache usage="read-write"/> ? ? ? ? ? ? ? ? ? ? ?<cache usage="read-write"/>
?----------------------------------------------------------------------------------------------------------------------------------------
Customer數據庫中Order個數等于零 ? ? ? ? ? ? ? ? 用 Update方法Customer后, ? ? ? ? ? ? ? ? ? ? ? ? ? 用 Update方法Customer后,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Get方法從二級緩存獲取Customer ? ? ? ? ? ? ? ? ? ??Get方法從二級緩存獲取Customer?
----------------------------------------------------------------------------------------------------------------------------------------- ? ? ? ? ? ? ? ? ? ? ? ? ??
Customer數據庫中Order個數大于零 ??? ? ? ? ? ? ? ??用 Update方法Customer后 ? ? ? ? ? ? ? ? ? ? ? ? ? ???用 Update方法Customer后
? ? ? ? ? ? ?? Get方法從二級緩存獲取Customer ? ? ? ? ? ? ? ? ? ? ? ?Get方法從不從二級緩存獲取Customer ?
??
-----------------------------------------------------------------------------------------------------------------------------------------
?
?
轉載于:https://www.cnblogs.com/easy5weikai/p/3759277.html
總結
以上是生活随笔為你收集整理的01-08-02【Nhibernate (版本3.3.1.4000) 出入江湖】二级缓存:NHibernate自带的HashtableProvider...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java知多少(28)super关键字
- 下一篇: 设置DIV半透明CSS代码: