1
I have an application that connects to the database via NHibernate
, with this some operations are carried out as Save, Update e Delete
. However, when performing these procedures, it is incredible how the memory accumulation is done by not automatically performing the memory dispensation with the GC not used by the CLR.
For example: A query that returns on average 1,000 Rows accumulates on average 100mb of memory, this is normal ?
The implementation of the base connection code is done as follows:
public ISession OpenConnection<T>(SelectDb dbConfigKey, DatabaseType dbType)
{
ISessionFactory sessionFactory = null;
try
{
switch (dbType)
{
#region Conexão com a base de dados SQL Server 2000
case DatabaseType.SQLServer2000:
sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2000
.ConnectionString(dbConfigKey.ToDescriptionDatabase())
.ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<T>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(false, false))
.BuildSessionFactory();
break;
#endregion
#region Conexão com a base de dados SQL Server 2008
case DatabaseType.SQLServer2008:
sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(dbConfigKey.ToDescriptionDatabase())
.ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<T>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(false, false))
.BuildSessionFactory();
break;
#endregion
#region Conexão com a base de dados PostgreSQL
case DatabaseType.PostgreSQL:
sessionFactory = Fluently.Configure()
.Database(PostgreSQLConfiguration.Standard
.ConnectionString(dbConfigKey.ToDescriptionDatabase())
.ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<T>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(false, false))
.BuildSessionFactory();
break;
#endregion
#region Conexão com a base de dados MYSQL
case DatabaseType.MYSQL:
sessionFactory = Fluently.Configure()
.Database(MySQLConfiguration.Standard
.ConnectionString(dbConfigKey.ToDescriptionDatabase())
.ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<T>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(false, false))
.BuildSessionFactory();
break;
#endregion
#region Conexão com a base de dados Oracle9
case DatabaseType.Oracle9:
sessionFactory = Fluently.Configure()
.Database(OracleClientConfiguration.Oracle9
.ConnectionString(dbConfigKey.ToDescriptionDatabase())
.ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<T>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(false, false))
.BuildSessionFactory();
break;
#endregion
#region Conexão com a base de dados Oracle10
case DatabaseType.Oracle10:
sessionFactory = Fluently.Configure()
.Database(OracleClientConfiguration.Oracle10
.ConnectionString(dbConfigKey.ToDescriptionDatabase())
.ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<T>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(false, false))
.BuildSessionFactory();
break;
#endregion
}
}
catch (Exception)
{
sessionFactory.Close();
sessionFactory.Dispose();
}
return sessionFactory.OpenSession();
}
where public Isession Openconnection(Selectdb dbConfigKey, Databasetype dbType) receives a class and passes the database configuration and connection type by parameter.
Any tips for using Nhibernate
for that kind of purpose ?