1

これが重複した投稿であることはわかっていますが、私のコンテキストは異なります。hereからこの問題の解決策を調べました。そして、私は次のように変更しました:

    private static Mutex _sessionMutex = new Mutex();
    public void OpenMySessionFactory(string conStr)
        {
            try
            {
                _sessionMutex.WaitOne();
                config = Fluently.Configure()
                .Database(MySQLConfiguration.Standard.ConnectionString(conStr))
                .Mappings(m => m.FluentMappings.AddFromAssembly(System.Reflection.Assembly.GetExecutingAssembly()))
                .BuildConfiguration();
                sessionFactory = config.BuildSessionFactory();
                _sessionMutex.ReleaseMutex();
            }
            catch (Exception)
            {
                throw;
            }
        }

上記のように実装した場合、何か影響はありますか?

エラーの理由:

同じキーを持つアイテムが既に追加されています

Fluent NHibernateを使用してデータアクセスで何かをしなければならないボタンをダブルクリックすると発生します。

4

1 に答える 1

0

例外が発生した場合、ミューテックスのロックを解除することはありません (コントロールはcatchブロックにジャンプし、ロック解除をスキップします)。finallyこれを行うには、ブロックを使用します。

public void OpenMySessionFactory(string conStr)
{
    _sessionMutex.WaitOne();
    try
    {
        config = Fluently.Configure()
        .Database(MySQLConfiguration.Standard.ConnectionString(conStr))
        .Mappings(m => m.FluentMappings.AddFromAssembly(System.Reflection.Assembly.GetExecutingAssembly()))
        .BuildConfiguration();
        sessionFactory = config.BuildSessionFactory();
    }
    catch (Exception)
    {
        throw;
    }
    finally
    {
        _sessionMutex.ReleaseMutex();
    }
}
于 2012-07-11T19:19:48.143 に答える