I am using quartz and nhibernate and ran into a problem. Normally I have all my nhibernate sessions close on finish of a web request but I have a scheduler that starts on application start and I need to pass in a nhibernate session that I think should never be closed.
I am unsure how to do that.
Ninject
public class NhibernateSessionFactoryProvider : Provider<ISessionFactory>
{
protected override ISessionFactory CreateInstance(IContext context)
{
var sessionFactory = new NhibernateSessionFactory();
return sessionFactory.GetSessionFactory();
}
}
public class NhibernateModule : NinjectModule
{
public override void Load()
{
Bind<ISessionFactory>().ToProvider<NhibernateSessionFactoryProvider>().InSingletonScope();
Bind<ISession>().ToMethod(context => context.Kernel.Get<ISessionFactory>().OpenSession()).InRequestScope();
}
}
Global.aspx
protected void Application_Start()
{
// Hook our DI stuff when application starts
IKernel kernel = SetupDependencyInjection();
// get the reminder service HERE IS WHERE THE PROBLEMS START
IScheduledRemindersService scheduledRemindersService = kernel.Get<IScheduledRemindersService>();
scheduledRemindersService.StartTaskRemindersSchedule();
RegisterMaps.Register();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
public IKernel SetupDependencyInjection()
{
IKernel kernel = CreateKernel();
// Tell ASP.NET MVC 3 to use our Ninject DI Container
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
return kernel;
}
protected IKernel CreateKernel()
{
var modules = new INinjectModule[]
{
new NhibernateModule(),
new ServiceModule(),
new RepoModule()
};
return new StandardKernel(modules);
}
// service that is causing me the problems. Ninject will bind reminderRepo and give it an nihbernate session.
private readonly IReminderRepo reminderRepo;
private readonly ISchedulerFactory schedulerFactory;
public ScheduledRemindersService(IReminderRepo reminderRepo)
{
this.reminderRepo = reminderRepo;
schedulerFactory = new StdSchedulerFactory();
}
public void StartTaskRemindersSchedule()
{
IScheduler scheduler = schedulerFactory.GetScheduler();
scheduler.Start();
JobDetail jobDetail = new JobDetail("TaskRemindersJob",null,typeof(TaskReminderJob));
jobDetail.JobDataMap["reminderRepo"] = reminderRepo;
DateTime evenMinuteDate = TriggerUtils.GetEvenMinuteDate(DateTime.UtcNow);
SimpleTrigger trigger = new SimpleTrigger("TaskRemindersTrigger", null,
DateTime.UtcNow,
null,
SimpleTrigger.RepeatIndefinitely,
TimeSpan.FromMinutes(1));
scheduler.ScheduleJob(jobDetail, trigger);
}
So I need to pass in the reminderRepo into the job as I am doing above
jobDetail.JobDataMap["reminderRepo"] = reminderRepo;
It's the only way you can pass something into a job. Everytime the schedule gets executed a job is recreated and I am assuming it uses the same reminderRepo that I sent in.
My code in the service layer never gets executed again and of course the application start as well(unless I redeploy the site)
Job
public class TaskReminderJob : IJob
{
public void Execute(JobExecutionContext context)
{
JobDataMap dataMap = context.JobDetail.JobDataMap;
ReminderRepo reminderRepo = dataMap["reminderRepo"] as ReminderRepo;
if (context.ScheduledFireTimeUtc.HasValue && context.NextFireTimeUtc.HasValue && reminderRepo != null)
{
DateTime start = context.ScheduledFireTimeUtc.Value;
DateTime end = context.NextFireTimeUtc.Value;
List<PersonalTaskReminder> personalTaskReminders = reminderRepo.GetPersonalTaskReminders(start, end);
if (personalTaskReminders.Count > 0)
{
reminderRepo.DeletePersonalTaskReminders(personalTaskReminders.Select(x => x.ReminderId).ToList());
}
}
}
Reminder Repo. (When this repo gets instantiated a session should be given that will live till the end of the request)
public class ReminderRepo : IReminderRepo
{
private readonly ISession session;
public ReminderRepo(ISession session)
{
this.session = session;
}
public List<PersonalTaskReminder> GetPersonalTaskReminders(DateTime start, DateTime end)
{
List<PersonalTaskReminder> personalTaskReminders = session.Query<PersonalTaskReminder>().Where(x => x.DateToBeSent <= start && x.DateToBeSent <= end).ToList();
return personalTaskReminders;
}
public void DeletePersonalTaskReminders(List<int> reminderId)
{
const string query = "DELETE FROM PersonalTaskReminder WHERE ReminderId IN (:reminderId)";
session.CreateQuery(query).SetParameterList("reminderId", reminderId).ExecuteUpdate();
}
public void Commit()
{
using (ITransaction transaction = session.BeginTransaction())
{
transaction.Commit();
}
}
}
So I need some way of keeping the session alive for my reminders. All my other sessions for all my other repos should be as I have it now. It's only this one that seems to need to live forever.
Edit
I tried to get a new session each time so I am passing the IsessionFactory around. Probably not 100% best but it was the only way I could figure out how to get some new sessions.
I however do not know if my session are being closed through ninject still since I am manually passing in the session now. I thinking now but cannot verify.
**private readonly ISessionFactory sessionFactory;**
private readonly ISchedulerFactory schedulerFactory;
public ScheduledRemindersService(ISessionFactory sessionFactory)
{
**this.sessionFactory = sessionFactory;**
schedulerFactory = new StdSchedulerFactory();
}
public void StartTaskRemindersSchedule()
{
IScheduler scheduler = schedulerFactory.GetScheduler();
scheduler.Start();
JobDetail jobDetail = new JobDetail("TaskRemindersJob",null,typeof(TaskReminderJob));
**jobDetail.JobDataMap["reminderRepo"] = sessionFactory;**
DateTime evenMinuteDate = TriggerUtils.GetEvenMinuteDate(DateTime.UtcNow);
SimpleTrigger trigger = new SimpleTrigger("TaskRemindersTrigger", null,
DateTime.UtcNow,
null,
SimpleTrigger.RepeatIndefinitely,
TimeSpan.FromMinutes(1));
scheduler.ScheduleJob(jobDetail, trigger);
}
So my global.aspx is the same but since ninject now sees that "ScheduledRemindersService" now takes in a nhibernate session factory it binds one for me that I can use.
I then pass it off to the job.
public void Execute(JobExecutionContext context)
{
JobDataMap dataMap = context.JobDetail.JobDataMap;
ISessionFactory sessionFactory = dataMap["reminderRepo"] as ISessionFactory;
if (sessionFactory != null)
{
ISession openSession = sessionFactory.OpenSession();
ReminderRepo reminderRepo = new ReminderRepo(openSession);
}
}
I then pass it into my ReminderRepo so I am guessing it ignores the auto session binding from ninject but I am not 100% sure thus I am not sure if my sessions are being closed.