NHibernate二级缓存不返回对象

时间:2012-09-05 01:14:59

标签: nhibernate caching stored-procedures

我正在使用存储过程进行简单的2级缓存实验,并且我收到的表或视图不存在Oracle错误。

缓存正在检索我的DTO对象并尝试向数据库发出SQL语句。我猜这是一些配置错误。

这是app.config

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
  <property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
  <property name="show_sql">true</property>
  <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
  <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
  <property name="cache.use_query_cache">true</property>
  <property name="cache.use_second_level_cache">true</property>
  <property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property>
</session-factory>
</hibernate-configuration>

这是要测试的代码:     [测试方法]     public void GetNomHeaderInfo_TestingCache_BPNomHeaderShouldBeCached()     {         //安排         DateTime StartTime;         DateTime EndTime;         TimeSpan FirstTry;         TimeSpan SecondTry;

    //Act
    using (var session = factory.OpenSession())
    {
        var query = session.GetNamedQuery("GetMyDTO");
        query.SetInt32("id", 1);
        query.SetCacheRegion("Id");
        query.SetCacheMode(CacheMode.Normal);
        query.SetCacheable(true);
        StartTime = DateTime.Now;
        myDTO DTO = query.UniqueResult<myDTO>();
        EndTime = DateTime.Now;
        FirstTry = EndTime - StartTime;
    }

    using (var session = factory.OpenSession())
    {
        var query = session.GetNamedQuery("GetMyDTO");
        query.SetInt32("id", 1);
        query.SetCacheRegion("Id");
        query.SetCacheMode(CacheMode.Normal);
        query.SetCacheable(true);
        StartTime = DateTime.Now;
        myDTO DTO = query.UniqueResult<myDTO>();
        EndTime = DateTime.Now;
        SecondTry = EndTime - StartTime;
    }

    //Test
    Assert.IsTrue(SecondTry < FirstTry);
}

然后我在第二次查询时出错.UniqueResult();错误消息是:

SELECT blah blah_.blahblah, etc FROM MyDTO SomeAlias_ WHERE SomeAlias_.id=:p0

但是没有myDTO表或视图。我不知道NHibernate为什么想从缓存myDTO中提取,然后尝试创建一个SQL语句。

这是追踪:

NHibernate.Cache.StandardQueryCache: DEBUG checking cached query results in region: 'Id'; sql: { call SomePackage.MyProc(?) }; parameters: []; named parameters: {'id'='1'}
NHibernate.Cache.StandardQueryCache: DEBUG checking cached query results in region: 'Id'; sql: { call SomePackage.MyProc(?) }; parameters: []; named parameters: {'id'='1'}
NHibernate.Caches.SysCache.SysCache: DEBUG Fetching object 'NHibernate-Cache:nomId:sql: { call SomePackage.MyProc(?) }; parameters: []; named parameters: {'id'='1'}@601355831' from the cache.
NHibernate.Caches.SysCache.SysCache: DEBUG Fetching object 'NHibernate-Cache:nomId:sql: { call SomePackage.MyProc(?) }; parameters: []; named parameters: {'id'='1'}@601355831' from the cache.
NHibernate.Cache.StandardQueryCache: DEBUG Checking query spaces for up-to-dateness [MyDTO]
NHibernate.Cache.StandardQueryCache: DEBUG Checking query spaces for up-to-dateness [MyDTO]
NHibernate.Caches.SysCache.SysCache: DEBUG Fetching object 'NHibernate-Cache:UpdateTimestampsCache:MyDTO@1270222867' from the cache.
NHibernate.Caches.SysCache.SysCache: DEBUG Fetching object 'NHibernate-Cache:UpdateTimestampsCache:MyDTO@1270222867' from the cache.
NHibernate.Cache.StandardQueryCache: DEBUG returning cached query results for: sql: { call SomePackage.MyProc(?) }; parameters: []; named parameters: {'id'='1'}
NHibernate.Cache.StandardQueryCache: DEBUG returning cached query results for: sql: { call SomePackage.MyProc(?) }; parameters: []; named parameters: {'id'='1'}
NHibernate.Event.Default.DefaultLoadEventListener: DEBUG loading entity: [MyAssembly.MyDTO#1]
NHibernate.Event.Default.DefaultLoadEventListener: DEBUG loading entity: [MyAssembly.MyDTO#1]
NHibernate.Event.Default.DefaultLoadEventListener: DEBUG attempting to resolve: [MyAssembly.MyDTO#1]
NHibernate.Event.Default.DefaultLoadEventListener: DEBUG attempting to resolve: [MyAssembly.MyDTO#1]
NHibernate.Event.Default.DefaultLoadEventListener: DEBUG object not resolved in any cache: [MyAssembly.MyDTO#1]
NHibernate.Event.Default.DefaultLoadEventListener: DEBUG object not resolved in any cache: [MyAssembly.MyDTO#1]
NHibernate.Persister.Entity.AbstractEntityPersister: DEBUG Fetching entity: [MyAssembly.MyDTO#1]
NHibernate.Persister.Entity.AbstractEntityPersister: DEBUG Fetching entity: [MyAssembly.MyDTO#1]
NHibernate.Loader.Loader: DEBUG loading entity: [MyAssembly.MyDTO#1]
NHibernate.Loader.Loader: DEBUG loading entity: [MyAssembly.MyDTO#1]
NHibernate.AdoNet.AbstractBatcher: DEBUG Opened new IDbCommand, open IDbCommands: 1
NHibernate.AdoNet.AbstractBatcher: DEBUG Opened new IDbCommand, open IDbCommands: 1
NHibernate.AdoNet.AbstractBatcher: DEBUG Building an IDbCommand object for the SqlString: SELECT blah blah_.blahblah, etc FROM MyDTO SomeAlias_ WHERE SomeAlias_.id=:p0

任何人都知道我在这里做错了吗?

谢谢,Bill N

2 个答案:

答案 0 :(得分:2)

我看不到你的映射,但这是一个解释。

  • 您正在缓存查询结果,但不是您的实体(这些是单独的缓存)
  • 缓存查询的结果只存储ID;如果你也没有缓存你的实体,则会发出一个查询来加载每个返回的实体(这通常是坏的)
  • MyDTO类的默认表名是MyDTO,所以它正在寻找
  • 这看起来像是按ID查询,您不应该使用松散的命名查询,而是使用正确的loader(请参阅17.4. Custom SQL for loading)。

设置加载程序和实体缓存之后,只需使用session.Get<MyDTO>(id)就可以检索对象,这将使用二级缓存,只要你在交易中完成所有工作,这是推荐的做法。

答案 1 :(得分:0)

对不起迭戈,你可能想要看看我的映射是什么样的。一旦我加入:

<cache usage="read-write"/> 

到hbm.xml文件一切正常。