Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4625062
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:13:08+00:00 2026-05-22T03:13:08+00:00

I am using quartz and nhibernate and ran into a problem. Normally I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:13:09+00:00Added an answer on May 22, 2026 at 3:13 am

    Is there a reason the job can’t just open up a new session every time it runs? Presumably it’s running at periodic intervals and not forever ever.

    Keeping stuff open forever is usually a sure-fire way to encounter weird behavior.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to display text using a Quartz context, but no matter what
I am using an UDP network receiver in a quartz composition, and I have
I'm using Cocoon and want to store the jobs and triggers for the quartz
Using online interfaces to a version control system is a nice way to have
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using JDeveloper , I started developing a set of web pages for a project
I've got the following code to schedule reboot jobs using Quartz. The code sets
I've run into the issue of using a UIBarButtonItem with a custom color. Everything
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using ASP.NET MVC there are situations (such as form submission) that may require a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.