sessionfactory与多线程

时间:2012-01-31 09:26:22

标签: nhibernate fluent-nhibernate

  

是否可以在一个类上创建一些类的迷你会话工厂   线程然后在第二个线程上创建完整的会话工厂?

     

我想加快sessionfactory的初始化

     

你能说明怎么做吗?

1 个答案:

答案 0 :(得分:1)

加速启动的一种方法是序列化配置对象,该对象占用构建和验证的大部分启动时间see here

实现您的方法的代码:

var minisessionfactory = Fluently.Configure()
    .DataBase(SomeConfiguration.Standard....)
    // only add the relevant mappings
    .Mapping(m => m.FluentMappings.Add<AMap>().Add<BMap>()...)
    .BuildSessionfactory();

ThreadPool.QueueUserWorkItem(state =>
{
    RealSessionFactory = Fluently.Configure()
        .DataBase(SomeConfiguration.Standard....)
        // only add the relevant mappings
        .Mapping(m => ...)
        .BuildSessionfactory();
    SessionFactoryReady = true;
});

// do some Stuff with minisessionfactory

更新

您是否测量了实际花费的时间:构建配置或构建工厂?

如果它正在构建工厂,请启用完整日志记录以查看是否有一些需要很长时间且可以禁用的步骤。

UpdateUpdate:简单实现

ISession GetSession()
{
    while (!SessionFactoryReady) Thread.Sleep(1000);
    return RealSessionFactory.OpenSession();
}

// or

ISessionFactory SessionFactory
{
    get
    {
        while (!SessionFactoryReady) Thread.Sleep(1000);
        return RealSessionFactory;
    }
}