I am new (of course) to NHibernate and have been able to get it working with some basic satisfaction. However, when I move to the next level of performance testing I'm having some frustration.
I use the session per unit of work pattern (method scoping). The problem occurs with this type of code
Code:
public override void Save(IMessageContext message)
{
try
{
if (log.IsDebugEnabled)
log.Debug("Starting Save");
using (ISession s = _sessionFactory.OpenSession())
{
using (ITransaction t = s.BeginTransaction())
{
try
{
t.Begin();
Partition p = s.Get<Partition>(_partitionId);
p.Messages.Add(message);
t.Commit();
}
catch
{
t.Rollback();
throw;
}
}
}
}
catch (Exception ex)
{
if (log.IsErrorEnabled)
log.Error("Error encountered in Save", ex);
throw;
}
finally
{
if (log.IsDebugEnabled)
log.Debug("Finished Save");
}
}
This is fine if there are only a few messages in the "Partition" object, but as this is a very frequent operation (thousands of messages an hour) it very quickly bogs down as it seems to load the entire collection into memory when I call p.Messages.Add(...)
I don't want to keep p in memory (with a long lived session) as there are potentially hundreds of thousands of messages in the collection, and each message is potentially very heavy (image data, etc contained within it).
This is
not an ASP.NET application. It is a Windows Service application.
What is the appropriate pattern for such a situation?
Thanks