Ich habe ein .NET MVC Webapi mit Nhibernate, um mehrere Informix-Datenbanken zu verbinden, ich verwende Repository und Arbeitseinheit Muster..NET Nhibernate Sitzung macht Speicherverbrauch wächst
Das Problem ist, wenn send multiple Anfrage an mvc Web API, Speicher des Servers wächst und macht meine Website stoppen.
Dies ist die Einheit der Arbeitsklasse:
public interface IUnitOfWorkNH: IDisposable
{
void BeginTransaction();
void Commit();
}
public class UnitOfWorkNH : IUnitOfWorkNH
{
private ISessionFactory _sessionFactory;
private ITransaction _transaction;
public ISession Session { get; private set; }
public UnitOfWorkNH(string connection)
{
string stringConnection = ConfigurationManager.AppSettings["NH_DSN"].ToString();
if (!string.IsNullOrEmpty(connection))
{
stringConnection = connection;
}
_sessionFactory = Fluently.Configure()
.Database(OdbcConfiguration.InformixODBC.ConnectionString(stringConnection)
.Driver<NHibernate.Driver.OdbcDriver>()
.Dialect<NHibernate.Dialect.InformixDialect>()
//.ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<clsCiaMap>())
.ExposeConfiguration(cfg => new SchemaExport(cfg)
.Create(false, false))
.BuildSessionFactory();
Session = _sessionFactory.OpenSession();
}
public void BeginTransaction()
{
_transaction = Session.BeginTransaction();
}
public void Commit()
{
try
{
// commit transaction if there is one active
if (_transaction != null && _transaction.IsActive)
_transaction.Commit();
}
catch
{
// rollback if there was an exception
if (_transaction != null && _transaction.IsActive)
_transaction.Rollback();
throw;
}
finally
{
Session.Dispose();
}
}
public void Rollback()
{
try
{
if (_transaction != null && _transaction.IsActive)
_transaction.Rollback();
}
finally
{
Session.Dispose();
}
}
public void Dispose()
{
if (this._transaction != null)
{
this._transaction.Dispose();
this._transaction = null;
}
if (this.Session != null)
{
this.Session.Dispose();
Session = null;
}
this._sessionFactory.Dispose();
this._sessionFactory = null;
}
}
Dies ist die Repository-Klasse:
public interface IRepository<T> where T : class
{
List<T> GetAll();
T GetById(int id);
T GetById(string id);
T GetById(T id);
void Save(T entity);
void Delete(T entity);
}
public class RepositoryNH<T> : IRepository<T> where T : class
{
ISession _session;
public RepositoryNH(ISession session)
{
_session = session;
}
public ISession Session { get { return _session; } }
public List<T> GetAll()
{
return Session.QueryOver<T>().List().ToList();
}
public T GetById(int id)
{
return _session.Get<T>(id);
}
public T GetById(string id)
{
return Session.Get<T>(id);
}
public T GetById(T id)
{
return _session.Get<T>(id);
}
public void Save(T entity)
{
Session.SaveOrUpdate(entity);
Session.Flush();
}
public void Delete(T entity)
{
Session.Delete(entity);
}
}
Ich weiß nicht, wenn ich einen Fehler in diesen Klassen haben, ich brauche Hilfe.
Einheit der Arbeit muss nur mit der Sitzung verknüpft sein, und es ist üblich, eine Sitzung pro Webanforderung zu verwenden. Die Sitzungsfactory muss nur eine Instanz für Ihre gesamte Anwendung sein. – Najera
aber ich brauche nicht mehrere Datenbanken zu verbinden, was soll ich tun? – DanielVorph
Eine Sitzungsfactory pro Datenbank, die möglicherweise mehr Kontext liefert, kann eine bessere Frage sein. – Najera