gpt4 book ai didi

nhibernate - 多线程应用程序中的Fluent Nhibernate配置错误

转载 作者:行者123 更新时间:2023-12-04 18:16:32 25 4
gpt4 key购买 nike

我已经在IIS(ASP.NET MVC)上创建了一个多线程应用程序,当线程服务器启动时,它会创建10个线程,并且正在线程中执行工作项。

通常我的应用程序运行良好,但是有一段时间我遇到了错误,并且我确定该问题来自流畅的配置。而且我敢肯定,我犯了一些错误:)

这是我的SessionFactory类:

public class NHibernateHelper
{
private static ISessionFactory sessionFactory;

/// <summary>
/// SessionFactory is static because it is expensive to create and is therefore at application scope.
/// The property exists to provide 'instantiate on first use' behaviour.
/// </summary>
private static ISessionFactory SessionFactory
{
get
{
if (sessionFactory == null)
{
sessionFactory = CreateSessionFactory();
}
return sessionFactory;
}
}


/// <summary>
/// CreateSessionFactory
/// </summary>
/// <returns></returns>
private static ISessionFactory CreateSessionFactory()
{
IPersistenceConfigurer dbConfigurer = MsSqlConfiguration.MsSql2005
.ConnectionString("connection string ..")
.Cache(c => c
.UseQueryCache()
.ProviderClass<NoCacheProvider>()
).ShowSql()
.CurrentSessionContext<ThreadStaticSessionContext>();
return Fluently
.Configure()
.Database(dbConfigurer)
.Mappings(mc =>
{
mc.FluentMappings.Add(typeof(UserMap));
mc.FluentMappings.Add(typeof(ApplicationMap));
mc.FluentMappings.Add(typeof(SubscriptionsMap));
})
.BuildSessionFactory();
}


public static ISession GetCurrentSession()
{
if (!CurrentSessionContext.HasBind(SessionFactory))
{
CurrentSessionContext.Bind(SessionFactory.OpenSession());
}
return SessionFactory.GetCurrentSession();
}



public static void DisposeSession()
{
var session = GetCurrentSession();
session.Close();
session.Dispose();
}

public static void BeginTransaction()
{
GetCurrentSession().BeginTransaction();
}

public static void CommitTransaction()
{
var session = GetCurrentSession();
if (session.Transaction.IsActive)
session.Transaction.Commit();
}

public static void RollbackTransaction()
{
var session = GetCurrentSession();
if (session.Transaction.IsActive)
session.Transaction.Rollback();
}
}

每个线程都调用NHibernateHelper类,其中的这一行本身;
            var myobjectinstance = new ObjectInstance();
NHibernateHelper.GetCurrentSession().Save( myobjectinstance );

我看到,当我启动服务器一段时间后,它已成功调用300.000个工作项以进行测试。但是有时它会给出大约2-3个工作项的错误。

异常(exception)是:
[0] = {"An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.\r\n\r\n"}

内在兴奋是:
Object reference not set to an instance of an object.

内部异常堆栈跟踪为:
    at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
at NHibernate.Impl.SessionFactoryObjectFactory.AddInstance(String uid, String name, ISessionFactory instance, IDictionary`2 properties)
at NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
at NHibernate.Cfg.Configuration.BuildSessionFactory()
at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()
in d:\Builds\FluentNH\src\FluentNHibernate\Cfg\FluentConfiguration.cs:line 93

欢迎任何建议或帮助

最佳答案

好像CreateSessionFactory方法被多次调用了。在此方法中,sessionFactory静态字段访问未同步,这使它不是线程安全的:

private static ISessionFactory SessionFactory
{
get
{
if (sessionFactory == null)
{
sessionFactory = CreateSessionFactory();
}
return sessionFactory;
}
}

确保始终同步对多线程应用程序中共享资源的访问。在这种情况下,有一个常用的模式称为 singleton

关于nhibernate - 多线程应用程序中的Fluent Nhibernate配置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2412664/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com