我最近升级了我们的应用程序以使用NHibernate 3.3并希望启用缓存。
我们处于多租户环境中,因此我希望每个会话工厂为每个客户保留一个区域。
这是我的NHibernate配置:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</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>
<property name="connection.connection_string">Data Source=localhost; database=test;user=test; password=test;</property>
<property name="show_sql">false</property>
<property name="command_timeout">1000</property>
</session-factory>
</hibernate-configuration>
并在我们的会话工厂生成器......
private static ISessionFactory GetSessionFactory(string connectionString, string driverClass = NormDriverClass, string prefix = null)
{
Configuration configuration = new Configuration();
configuration.Configure().SetProperty(ConnectionStringProperty, connectionString);
configuration.Properties.Add(ConnectionDriverProperty, driverClass);
configuration.Properties.Add("regionPrefix", prefix ?? "default");
configuration.AddAssembly(typeof(Foo).Assembly);
return configuration.BuildSessionFactory();
}
我已将查询设置为cachable:
query.SetCacheable(true).SetCacheMode(CacheMode.Normal);
我每次都会收到以下错误
System.IndexOutOfRangeException : Index was outside the bounds of the array.
at NHibernate.Type.TypeHelper.Disassemble(Object[] row, ICacheAssembler[] types, Boolean[] nonCacheable, ISessionImplementor session, Object owner)
at NHibernate.Cache.StandardQueryCache.Put(QueryKey key, ICacheAssembler[] returnTypes, IList result, Boolean isNaturalKeyLookup, ISessionImplementor session)
at NHibernate.Loader.Loader.PutResultInQueryCache(ISessionImplementor session, QueryParameters queryParameters, IType[] resultTypes, IQueryCache queryCache, QueryKey key, IList result)
at NHibernate.Loader.Loader.ListUsingQueryCache(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
at NHibernate.Impl.SessionImpl.ListCustomQuery(ICustomQuery customQuery, QueryParameters queryParameters, IList results)
at NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters, IList results)
at NHibernate.Impl.SessionImpl.List(NativeSQLQuerySpecification spec, QueryParameters queryParameters)
at NHibernate.Impl.SqlQueryImpl.List()
认为缓存提供商应该受到责备,我转回
<property name="cache.provider_class">NHibernate.Cache.HashtableCacheProvider,NHibernate</property>
没有变化。
我删除了区域前缀代码,没有变化。
将“Use_query_cache”设置为false可修复运行时异常,但不会使用任何缓存。