views:

144

answers:

1

I have a web site running Burrow, and I'd like to use it for Quartz jobs as well.

The thing is that I want them to not share any state. The Quartz jobs is running in each own threads while the Mvc framework closes the Workspace at the end of every request. Ideally, mvc should have it's own session, and each job should have it's own session.

What are my possibilities here?

PS: I'm very new to Quartz, Burrow and MVC btw, so I'm probably missing some very essential knowledge :|

A: 

I tried a simple naive way that at least seem to work for now. Is there something fundamentally wrong I'm doing here? Will these variables be garbage collected when a thread exits?

public static class SessionManager
{
    [ThreadStatic]
    private static IDictionary<ISessionFactory, ISession> _sessions;

    public static ISession GetSession(Type type)
    {
        var burrow = new BurrowFramework();

        if (burrow.WorkSpaceIsReady)
        {
            return burrow.GetSession(type);
        }
        else
        {
            if (_sessions == null)
            {
                _sessions = new Dictionary<ISessionFactory, ISession>();
            }

            var factory = burrow.GetSessionFactory(type);
            if (!_sessions.ContainsKey(factory))
            {
                _sessions[factory] = null;
            }

            var session = _sessions[factory];
            if (session == null || !session.IsOpen)
            {
                session = _sessions[factory] = factory.OpenSession();
            }

            return session;
        }
    }
}
simendsjo