private long Execute(SequenceQuery query, Session session)
        {
            switch (query.Compartment)
            {
            case SequenceQueryCompartment.SameSession:
                return(query.ExecuteWith(session.Services.Demand <ISqlExecutor>()));

            case SequenceQueryCompartment.SeparateSession:
                return(ExecuteInKeyGeneratorSession(query, session.StorageNode));

            default:
                throw new ArgumentOutOfRangeException("query.Compartment");
            }
        }
        private long ExecuteInKeyGeneratorSession(SequenceQuery query, StorageNode node)
        {
            long result;

            using (var session = domain.OpenSession(SessionType.KeyGenerator)) {
                session.SetStorageNode(node);
                using (var transaction = session.OpenTransaction()) {
                    result = query.ExecuteWith(session.Services.Demand <ISqlExecutor>());
                    // This is for stupid MySQL auto-increment settings
                    // DO NOT COMMIT transaction when provider is not MySql
                    if (hasAISettingsInMemory)
                    {
                        transaction.Complete();
                    }
                }
            }
            return(result);
        }