本文介绍了确保NHibernate的SessionFactory的只创建一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写持有静态NHibernate的ISessionFactory的NHibernateSessionFactory类。这是用来确保我们只有一个会话工厂,并在第一时间的openSession()被调用,我创建了实际工作的SessionFactory - 接下来的时间我使用相同的并在其上打开会话。在code是这样的:

I have written an NHibernateSessionFactory class which holds a static Nhibernate ISessionFactory. This is used to make sure we only have one session factory, and the first time OpenSession() is called I create the actuall SessionFactory - next times I use the same and open a session on it. The code looks like this:

public class NhibernateSessionFactory : INhibernateSessionFactory
{
    private static ISessionFactory _sessionFactory;

    public ISession OpenSession()
    {
        if (_sessionFactory == null)
        {
            var cfg = Fluently.Configure().
                Database(SQLiteConfiguration.Standard.ShowSql().UsingFile("Foo.db")).
                Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>());
            _sessionFactory = cfg.BuildSessionFactory();
            BuildSchema(cfg);
        }
        return _sessionFactory.OpenSession();
    }

    private static void BuildSchema(FluentConfiguration configuration)
    {
        var sessionSource = new SessionSource(configuration);
        var session = sessionSource.CreateSession();
        sessionSource.BuildSchema(session);            
    }
}

现在我有一个问题。我的应用程序客户端和服务器之间的分裂。 NHibernate的东西是在服务器端。在启动时我的两个客户端和服务器想通过一些服务将使用NhibernateSessionFactory访问数据库。结果是一个争用条件是否创建_sessionFactory请求来自客户端之前。如果没有它就会失败。

Now I have a problem. My application is split between client and server. The Nhibernate stuff is on the server side. On startup both my client and server wants to access the database through some services which will use the NhibernateSessionFactory. Result is a race condition to whether the _sessionFactory is created before the request comes from the client. If it isn't it will fail..

我想我需要某种形式的排队或等待机制在NhibernateSessionFactory的,但我不知道该怎么办。任何人都收到了同样的问题?什么是最好的解决方案?

I guess I need some sort of queueing or wait mechanism in the NhibernateSessionFactory, but I'm not sure about what to do. Anyone had the same problem before? What's the best solution?

推荐答案

SessionFactory的必须是线程安全的单例。

The sessionFactory must be a thread-safe singleton.

在Java中一个常见的​​模式是一个静态初始化打造的 SessionFactory的。见HibernateUtil.你可以做同样的在C#。

A common pattern in Java is to build the sessionFactory in a static initializer. See HibernateUtil. You can do the same in C#.

有其他以实现单,包括锁定或同步部分的使用。这是,如果我理解正确的话应该解决您的问题轻微的变体。

There are other patterns to implement singleton, including the usage of lock or synchronized sections. Here is slight variant that should solve your problem if I understood it correctly.

static readonly object factorylock = new object();

public ISession OpenSession()
{
    lock (factorylock)
    {
       if (_sessionFactory == null)
       {
            var cfg = Fluently.Configure().
               Database(SQLiteConfiguration.Standard.ShowSql().UsingFile("Foo.db")).
               Mappings(m => m.FluentMappings.AddFromAssemblyOf<MappingsPersistenceModel>());
            _sessionFactory = cfg.BuildSessionFactory();
            BuildSchema(cfg);
        }
    }
    return _sessionFactory.OpenSession();
}

这篇关于确保NHibernate的SessionFactory的只创建一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 18:01