void InnerInvoke(BehaviorContext context, Action next, Func <IDbConnection> connectionRetriever)
        {
            var lazySession = new Lazy <ISession>(() =>
            {
                var session       = SessionFactoryProvider.SessionFactory.OpenSession(connectionRetriever());
                session.FlushMode = FlushMode.Never;

                return(session);
            });

            context.Set(string.Format("LazyNHibernateSession-{0}", ConnectionString), lazySession);
            try
            {
                next();

                if (lazySession.IsValueCreated)
                {
                    lazySession.Value.Flush();
                }
            }
            finally
            {
                if (lazySession.IsValueCreated)
                {
                    lazySession.Value.Dispose();
                }

                context.Remove(string.Format("LazyNHibernateSession-{0}", ConnectionString));
            }
        }
        void InnerInvoke(BehaviorContext context, Action next, Func <IDbConnection> connectionRetriever)
        {
            var lazySession = new Lazy <ISession>(() =>
            {
                var session = SessionCreator != null
                    ? SessionCreator(SessionFactoryProvider.SessionFactory, ConnectionString)
                    : SessionFactoryProvider.SessionFactory.OpenSession(connectionRetriever());

                session.FlushMode = FlushMode.Never;

                if (Transaction.Current == null)
                {
                    var tx = session.BeginTransaction();
                    context.Set(LazyNHibernateTransactionKey, tx);
                }
                return(session);
            });

            context.Set(LazyNHibernateSessionKey, lazySession);
            try
            {
                next();

                if (lazySession.IsValueCreated)
                {
                    lazySession.Value.Flush();
                    ITransaction tx;
                    if (context.TryGet(LazyNHibernateTransactionKey, out tx))
                    {
                        tx.Commit();
                        tx.Dispose();
                    }
                }
            }
            finally
            {
                if (lazySession.IsValueCreated)
                {
                    ITransaction tx;
                    if (context.TryGet(LazyNHibernateTransactionKey, out tx))
                    {
                        tx.Dispose();
                    }
                    lazySession.Value.Dispose();
                }

                context.Remove(LazyNHibernateSessionKey);
            }
        }