我使用流畅的nHibernate作为ORM并使用自动映射在fluenthibernate中映射类。这些类只会被映射一次,因为我在global.asax中调用它们。由于某种原因,CPU正在加扰有时高达90%。我使用ANTS性能分析器对应用程序进行了分析,发现Configure()属性消耗了5%的cpu和buildsessionfactory(),调用后自动化器消耗了20%的CPU。我调试了应用程序和找不到问题所在。可能是什么问题?
这是我的代码
public static ISessionFactory GetSessionFactory()
{
lock (factorylock)
{
if (_sessionFactory == null)
{
string scriptLocation = HttpContext.Current.IsDebuggingEnabled
? HostingEnvironment.MapPath( ConfigurationManager.AppSettings["GetDataBaseScript"] )
: null;
FluentConfiguration config = Fluently
//// Start the configuration
.Configure() **--- This is the place where 5% of memory is being used**
// Setup the database configuration
.Database(
// Configure for MS SQL Server connection
MsSqlConfiguration
// use MS SQL Server 2010
.MsSql2008
// Specify the connection string
.ConnectionString(c => c.FromConnectionStringWithKey("DatabaseConnection"))
)
// Set up the mappings
.Mappings( maps =>
maps
Setup mappings
.AutoMappings
// Load mappings from assembly
.Add( AutoMap
// Load auto maps configuration from assembly
.AssemblyOf<AutoMapConfiguration>(new AutoMapConfiguration() )
// Specify some custom conventions for Auto Map
.Conventions.AddFromAssemblyOf<AutoMapConfiguration>()
// Override some automapping configuration
.UseOverridesFromAssemblyOf<AutoMapConfiguration>()
)
);
// determine if we need to generate the DB or just build the configuration
if( !String.IsNullOrEmpty( scriptLocation ) ) {
// Where to generate the script file
// Expose the configuration
config.ExposeConfiguration( c => {
// Export the schema
new SchemaExport( c )
// Specify where the script is to be exported
.SetOutputFile( scriptLocation )
.Create( true, false );
} );
} else {
// Build the configuration
config.BuildConfiguration();
}
// Return the NHibernate Session Factory
_sessionFactory = config.BuildSessionFactory(); **--- This is the place where 20% is being used**
}
return _sessionFactory;
}
}
答案 0 :(得分:0)
构建SessionFactory是一项重量级操作,应该只在应用程序启动时发生一次。如果要优化它,可能的解决方案是序列化/反序列化它。