我對NHibernate的相對較新的,我有 問題緩存的實體在會話(一級緩存),這裏是我的代碼NHibernate的會話不緩存
public Book IProductRepository.GetBookbyName(string bookName)
{
ISession session = NHibernateHelper.GetSession();
ICriteria criteria = session.CreateCriteria<Book>()
.Add(Restrictions.Eq("Name", bookName));
return criteria.UniqueResult<Book>();
}
和
私有靜態ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(Product).Assembly);
_sessionFactory = configuration.BuildSessionFactory();
CurrentSessionContext.Bind(_sessionFactory.OpenSession());
}
return _sessionFactory;
}
}
public static ISession GetSession()
{
return SessionFactory.GetCurrentSession();
}
和配置文件是
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=(local);initial catalog=NhibernateTest;Integrated Security=SSPI</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<property name ="current_session_context_class">thread_static</property>
<property name="cache.use_query_cache" >true</property>
<property name="show_sql">true</property>
,每次我打電話GetBookByName方法,訪問數據庫不管是什麼?謝謝
爲您的問題的答案見[這個問題] [1]。 也,我注意到你創建'ISessionFactory'時似乎只創建一個新的會話。應該創建會話,而不是每個應用程序運行時。 [1]:http://stackoverflow.com/questions/4919636/can-first-level-cache-be-used-with-icriteria-or-other-apis –