When using NHibernate in web applications, I will usually let my IoC
container take care of opening and closing an `ISession` per request
and commit/rollback the transaction. The nature of HTTP makes it very
easy to define a clear Unit-of-Work in such applications.
Now, I have been tasked with putting together a small program, which
will be invoked regularly by a task scheduler, for sending out
newsletters. The concepts of both newsletters and subscribers are
already well defined entities in our domain model, and sending a
newsletter to all subscribers would involve doing something similar to
this:
var subscribers = _session
.QueryOver<Subscription>()
.Where(s => !s.HasReceivedNewsletter)
.List();
foreach (var subscriber in subscribers)
{
SendNewsletterTo(subscriber);
subscriber.HasReceivedNewsletter = true;
}
Notice how each `Subscriber` object is updated within the loop,
recording that she has now received the newsletter. The idea is, that
if the mail sending program should crash, it can be restarted and
continue sending newsletters from where it left off.
The problem I am facing, is in defining and implementing the Unit-of-
Work pattern here. I will probably need to commit changes to the
database by the end of each iteration of the loop. Simply wrapping the
loop body with a `using (var trans = _session.BeginTransaction())`
block seems to be extremely expensive in running time, and I also seem
to experience locking issues between this long running process and
other (web) applications using the same database.
After reading some articles and documentation on NHibernate
transactions, I have come to think, that I might need to detach the
list of subscribers from the session to avoid the locking issues, and
reattach each to a fresh session in the loop body. I am not sure how
this will work for performance, though.
So, NHibernate experts, how would you design and implement a long
running job like this?
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.