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 , false , 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="" lazy="false">
<!--.这个不是必须的,因为在nhibernate.cfg.xml文件中已经有了一个总配置
.cache标签必须在id标签前面
-->
<cache usage="read-write"/>
<!--unsaved-value="" 主键表中不需要定义,而是需要在子表中定义-->
<id name="CustomerId"
column="CustomerId"
type="int"
unsaved-value="">
<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="" lazy="true">
<!--.这个不是必须的,因为在nhibernate.cfg.xml文件中已经有了一个总配置
.cache标签必须在id标签前面
-->
<cache usage="read-write"/>
测试代码:
[TestMethod]
public void TestSessionFactoryCacheUpdateCustomerHavingNotOrders()
{
CustomerService customerService = new CustomerService(); Customer customer = new Customer()
{
FirstName = "Test",
LastName = "TestSessionFactoryCache",
Age =
}; 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"; //修改FirstName
customerService.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); }
测试结果:
NHibernate:
INSERT
INTO
Customer
(Version, Firstname, Lastname, Age)
VALUES
(@p0, @p1, @p2, @p3);
select
SCOPE_IDENTITY();
@p0 = [Type: Int32 ()],
@p1 = 'Test' [Type: String ()],
@p2 = 'TestSessionFactoryCache' [Type: String ()],
@p3 = [Type: Int32 ()]
第1次获取数据并且更新------------------------------:
NHibernate:
SELECT
customer0_.CustomerId as CustomerId0_0_,
customer0_.Version as Version0_0_,
customer0_.Firstname as Firstname0_0_,
customer0_.Lastname as Lastname0_0_,
customer0_.Age as Age0_0_
FROM
Customer customer0_
WHERE
customer0_.CustomerId=@p0;
@p0 = [Type: Int32 ()]
NHibernate:
SELECT
orders0_.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_ WHERE
orders0_.CustomerId=@p0;
@p0 = [Type: Int32 ()]
更新前:Id:-->FirtstName:Test
NHibernate:
UPDATE
Customer
SET
Version = @p0,
Firstname = @p1,
Lastname = @p2,
Age = @p3
WHERE
CustomerId = @p4
AND Version = @p5;
@p0 = [Type: Int32 ()], @p1 = '我是更新后的FirstName' [Type: String ()], @p2 = 'TestSessionFactoryCache' [Type: String ()], @p3 = [Type: Int32 ()], @p4 = [Type: Int32 ()], @p5 = [Type: Int32 ()]
更新后:Id:-->FirtstName:我是更新后的FirstName
第2次获取数据==============================:
Id:-->FirtstName:我是更新后的FirstName
第3次获取数据==============================:
Id:-->FirtstName:我是更新后的FirstName
---------------------------------------------------
测试更新-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 =
}; 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"; //修改FirstName
customerService.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); }
NHibernate:
INSERT
INTO
Customer
(Version, Firstname, Lastname, Age)
VALUES
(@p0, @p1, @p2, @p3);
select
SCOPE_IDENTITY();
@p0 = [Type: Int32 ()],
@p1 = 'Test' [Type: String ()],
@p2 = 'TestSessionFactoryCache' [Type: String ()],
@p3 = [Type: Int32 ()]
NHibernate:
INSERT
INTO
[ Order] (
Version, OrderDate, CustomerId
)
VALUES
(@p0, @p1, @p2);
select
SCOPE_IDENTITY();
@p0 = [Type: Int32 ()],
@p1 = // :: [Type: DateTime ()],
@p2 = [Type: Int32 ()]
第1次获取数据并且更新------------------------------:
NHibernate:
SELECT
customer0_.CustomerId as CustomerId0_0_,
customer0_.Version as Version0_0_,
customer0_.Firstname as Firstname0_0_,
customer0_.Lastname as Lastname0_0_,
customer0_.Age as Age0_0_
FROM
Customer customer0_
WHERE
customer0_.CustomerId=@p0;
@p0 = [Type: Int32 ()]
NHibernate:
SELECT
orders0_.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_ WHERE
orders0_.CustomerId=@p0;
@p0 = [Type: Int32 ()]
更新前:Id:-->FirtstName:Test
NHibernate:
UPDATE
Customer
SET
Version = @p0,
Firstname = @p1,
Lastname = @p2,
Age = @p3
WHERE
CustomerId = @p4
AND Version = @p5;
@p0 = [Type: Int32 ()], @p1 = '我是更新后的FirstName' [Type: String ()], @p2 = 'TestSessionFactoryCache' [Type: String ()], @p3 = [Type: Int32 ()], @p4 = [Type: Int32 ()], @p5 = [Type: Int32 ()]
NHibernate:
UPDATE
[
Order] SET
Version = @p0,
OrderDate = @p1,
CustomerId = @p2
WHERE
OrderId = @p3
AND Version = @p4;
@p0 = [Type: Int32 ()], @p1 = // :: [Type: DateTime ()], @p2 = [Type: Int32 ()], @p3 = [Type: Int32 ()], @p4 = [Type: Int32 ()]
更新后:Id:-->FirtstName:我是更新后的FirstName
第2次获取数据==============================:
NHibernate:
SELECT
order0_.OrderId as OrderId1_0_,
order0_.Version as Version1_0_,
order0_.OrderDate as OrderDate1_0_,
order0_.CustomerId as CustomerId1_0_
FROM
[
Order] order0_ WHERE
order0_.OrderId=@p0;
@p0 = [Type: Int32 ()]
Id:-->FirtstName:我是更新后的FirstName
第3次获取数据==============================:
NHibernate:
SELECT
order0_.OrderId as OrderId1_0_,
order0_.Version as Version1_0_,
order0_.OrderDate as OrderDate1_0_,
order0_.CustomerId as CustomerId1_0_
FROM
[
Order] order0_ WHERE
order0_.OrderId=@p0;
@p0 = [Type: Int32 ()]
Id:-->FirtstName:我是更新后的FirstName
将该测试(测试更新-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
INTO
Customer
(Version, Firstname, Lastname, Age)
VALUES
(@p0, @p1, @p2, @p3);
select
SCOPE_IDENTITY();
@p0 = [Type: Int32 ()],
@p1 = 'Test' [Type: String ()],
@p2 = 'TestSessionFactoryCache' [Type: String ()],
@p3 = [Type: Int32 ()]
NHibernate:
INSERT
INTO
[ Order] (
Version, OrderDate, CustomerId
)
VALUES
(@p0, @p1, @p2);
select
SCOPE_IDENTITY();
@p0 = [Type: Int32 ()],
@p1 = // :: [Type: DateTime ()],
@p2 = [Type: Int32 ()]
第1次获取数据并且更新------------------------------:
NHibernate:
SELECT
customer0_.CustomerId as CustomerId0_0_,
customer0_.Version as Version0_0_,
customer0_.Firstname as Firstname0_0_,
customer0_.Lastname as Lastname0_0_,
customer0_.Age as Age0_0_
FROM
Customer customer0_
WHERE
customer0_.CustomerId=@p0;
@p0 = [Type: Int32 ()]
更新前:Id:-->FirtstName:Test
NHibernate:
UPDATE
Customer
SET
Version = @p0,
Firstname = @p1,
Lastname = @p2,
Age = @p3
WHERE
CustomerId = @p4
AND Version = @p5;
@p0 = [Type: Int32 ()], @p1 = '我是更新后的FirstName' [Type: String ()], @p2 = 'TestSessionFactoryCache' [Type: String ()], @p3 = [Type: Int32 ()], @p4 = [Type: Int32 ()], @p5 = [Type: Int32 ()]
更新后:Id:-->FirtstName:我是更新后的FirstName
第2次获取数据==============================:
Id:-->FirtstName:我是更新后的FirstName
第3次获取数据==============================:
Id:-->FirtstName:我是更新后的FirstName
经测试,得出如下结论:
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
-----------------------------------------------------------------------------------------------------------------------------------------
01-08-02【Nhibernate (版本3.3.1.4000) 出入江湖】二级缓存:NHibernate自带的HashtableProvider的更多相关文章
- 01-08-03【Nhibernate (版本3.3.1.4000) 出入江湖】二级缓存:NHibernate自带的HashtableProvider之缓存管理
http://www.cnblogs.com/lyj/archive/2008/11/28/1343418.html 管理NHibernate二级缓存 NHibernate二级缓存由ISessionF ...
- 01-08-05【Nhibernate (版本3.3.1.4000) 出入江湖】NHibernate二级缓存:第三方MemCache缓存
一.准备工作 [1]根据操作系统(位数)选择下载相应版本的MemCache, MemCache的下载和安装,参看: http://www.cnblogs.com/easy5weikai/p/37606 ...
- 01-03-03【Nhibernate (版本3.3.1.4000) 出入江湖】cascade的测试
相关文章: http://www.cnblogs.com/amboyna/archive/2008/02/18/1072260.html注意上面是hibernate,不是Nhibernate,这解释是 ...
- 01-03-02-2【Nhibernate (版本3.3.1.4000) 出入江湖】CRUP操作-Save方法的一些问题
此文由于当时不知道NHibernate的Sava方法不是更新操作,不知道Save就是Add,造成如下荒唐的求证过程,但结论是对的 ,可报废此文,特此声明. NHibernate--Save方法: Cu ...
- 01-08-01【Nhibernate (版本3.3.1.4000) 出入江湖】NHibernate中的一级缓存
缓存的范围? 1.事务范围 事务范围的缓存只能被当前事务访问,每个事务都有各自的缓存,缓存内的数据通常采用相互关联的对象形式.缓存的生命周期依赖于事务的生命周期,只有当事务结束时,缓存的生命周期才会结 ...
- 01-05-01-1【Nhibernate (版本3.3.1.4000) 出入江湖】延迟加载及其class和集合(set、bag等)的Lazy属性配置组合对Get和Load方法的影响
这篇文章 http://ayende.com/blog/3988/nhibernate-the-difference-between-get-load-and-querying-by-id One o ...
- 01-07-01【Nhibernate (版本3.3.1.4000) 出入江湖】并发控制
Nhibernate 并发控制 [1]悲观并发控制 正在使用数据的操作,加上锁,使用完后解锁释放资源. 使用场景:数据竞争激烈,锁的成本低于回滚事务的成本 缺点:阻塞,可能死锁 [2]乐观并发控制: ...
- 01-03-01【Nhibernate (版本3.3.1.4000) 出入江湖】id标签的unsaved-value属性
父表 <class name="Model.Customer, Model" discriminator-value="0"> <!--uns ...
- 01-08-04【Nhibernate (版本3.3.1.4000) 出入江湖】二级缓存:NHibernate自带的HashtableProvider之命名缓存
http://www.cnblogs.com/lyj/archive/2008/11/28/1343418.html 可以在映射文件中定义命名查询,<query>元素提供了很多属性,可以用 ...
随机推荐
- struts2<s:property />标签
struts2的<property />标签是输出标签 其value属性是指定输出的内容,如果value属性没有写出来,则默认输出对象栈栈顶的元素. 例如,我们在对象栈中添加一个Perso ...
- Ant打jar包指定MainClass
一般用ant打jar的时候不用指定程序的入口!这个jar一般是给其他app引用的. 但是如果该jar就是程序的启动jar.例如: java -jar abc.jar 这个时候需要指定jar的入口类! ...
- Aborting a running program
In the event that a calculation appears to be running excessively long, one can abort thecalculation ...
- MyBatis用嵌套ResultMap实现一对多映射
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3959451.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- 工厂方法(Factory Method)模式
一.工厂方法(Factory Method)模式 工厂方法(FactoryMethod)模式是类的创建模式,其用意是定义一个创建产品对象的工厂接口,将实际创建工作推迟的子类中. 工厂方法模式是简单工厂 ...
- boost:program_options
由于系统库getopt和getopt_long用起来不够直观,仔细看了下boost发现Boost.Program_options可以满足我的需求,它和getopt系列函数一样,可以抓起命令行参数,这里 ...
- WP开发笔记——阻止Back后退键
WP7中如何阻止Back后退键的后退事件呢? WP7上提供了物理的Back按键,获取Back物理键按下可以通过PhoneApplicationPage的BackKeyPress事件. 具体实现方法如下 ...
- .Net中的Socket通讯
.NetFrameWork为Socket通讯提供了System.Net.Socket命名空间,在这个命名空间里面有以下几个常用的重要类分别是: ·Socket类 这个低层的类用于管理连接,WebReq ...
- centos 减少tty数量的方法
在linux中,包括本文介绍的centos系统中,tty系统默认是给出7个,前六个是terminal,一个用于X. 在centos5.x中减少tty数量,通过修改/etc/inittab来实现. [r ...
- 如何在ARC代码中混编非ARC代码
“ios中如果arc和非arc文件混编,可以在build parses中指定compile flags,如果arc文件设为"-fobjc-arc",非arc文件设为"-f ...