标签:
配置NHibernate连接sqlite数据库文件,以HSqlite.cfg为例:
<?xml version="1.0" encoding="utf-8"?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory name="NHibernate.Test"> <property name="current_session_context_class">thread_static</property> <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property> <property name="connection.connection_string"></property> <property name="adonet.batch_size">10</property> <property name="show_sql">true</property> <property name="dialect">NHibernate.Dialect.SQLiteDialect</property> <property name="use_outer_join">true</property> <property name="command_timeout">60</property> <property name="hbm2ddl.auto">update</property> <property name="query.substitutions">true=1;false=0</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> <mapping assembly="InspectClient.Domain"/> </session-factory> </hibernate-configuration>
其中current_session_context_class主要用于配置session类型,winform类型使用thread_static参数,web类型使用web参数。
sessionhelper
public static class SessionBuilder { private static readonly ISessionFactory _sessionFactory ; private static readonly object _lock = new object(); static SessionBuilder() { lock (_lock) { _sessionFactory = new Configuration().Configure("HSqlite.cfg.xml") .SetProperty("connection.connection_string", SqliteConfig.DataSource) .BuildSessionFactory(); } } #region Session在当前上下文的操作 private static void BindContext() { lock (_lock) { if (!CurrentSessionContext.HasBind(_sessionFactory)) { CurrentSessionContext.Bind(_sessionFactory.OpenSession()); } } } private static void UnBindContext() { lock (_lock) { if (CurrentSessionContext.HasBind(_sessionFactory)) { CurrentSessionContext.Unbind(_sessionFactory); } } } public static void CloseCurrentSession() { UnBindContext(); } public static ISession GetCurrentSession() { BindContext(); return _sessionFactory.GetCurrentSession(); } #endregion #region 关闭SessionFactory(一般在应用程序结束时操作) public static void CloseSessionFactory() { if (!_sessionFactory.IsClosed) { _sessionFactory.Close(); } } #endregion #region 打开一个新的Session public static ISession OpenSession() { lock (_lock) { return _sessionFactory.OpenSession(); } } #endregion }
标签:
原文地址:http://www.cnblogs.com/johnvwan/p/4612553.html