Good afternoon, I'm migrating a fairly large project over to Fluent NHibernate for use with mono. I've gotten most of the key functionality working well, however I'm having a memory issue.
Currently, this code is in two of my controllers. This, does not seem even slightly optimal. But I'm unsure where to put this.
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure()
.Database(MySQLConfiguration.Standard.ConnectionString(
c => c.FromConnectionStringWithKey("DashboardModels")
))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Accounts>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Notes>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Sales_Forecast>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<ChangeLog>())
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Tasks>())
.BuildSessionFactory();
}
ISessionFactory sessionFactory = CreateSessionFactory();
Most of my db calls are AJAX, with several firing at once. This leads me to believe that I'm creating too many sessions, which are not being released.
public ActionResult ReadAccounts([DataSourceRequest] DataSourceRequest request)
{
DataSourceResult result;
using (var session = sessionFactory.OpenStatelessSession())
using (var tx = session.BeginTransaction())
{
var customers = from customer in session.Query<Accounts>().AsNoTracking()
where !customer.Deleted
select customer;
result = customers.ToDataSourceResult(request);
tx.Commit();
}
return Json(result, JsonRequestBehavior.AllowGet);
}
Consider that I am using StatelessSessions for methods that only return data.
public JsonResult GetNoteInfo(int id = 0)
{
Notes note;
using (var session = sessionFactory.OpenStatelessSession())
using (var tx = session.BeginTransaction())
{
note = (from notes in session.Query<Notes>().AsNoTracking()
where notes.Note_ID == id
select notes).FirstOrDefault();
tx.Commit();
return Json(JsonResponseFactory.SuccessResponse(note), JsonRequestBehavior.DenyGet);
}
}
As I mentioned, this is working, but how can I improve memory performance? There are about 5 methods similar to the bottom method that fire simultaneously, so anyone that has done this before if you can please advise on how to keep memory from quickly expanding I would be most grateful. Thank you for your time and happy coding!