internal Session OpenSessionInternal(SessionConfiguration configuration, StorageNode storageNode, bool activate)
        {
            ArgumentValidator.EnsureArgumentNotNull(configuration, nameof(configuration));
            configuration.Lock(true);

            if (isDebugEventLoggingEnabled)
            {
                OrmLog.Debug(Strings.LogOpeningSessionX, configuration);
            }

            Session session;

            if (SingleConnection != null)
            {
                // Ensure that we check shared connection availability
                // and acquire connection atomically.
                lock (singleConnectionGuard) {
                    if (singleConnectionOwner != null)
                    {
                        throw new InvalidOperationException(string.Format(
                                                                Strings.ExSessionXStillUsesSingleAvailableConnection, singleConnectionOwner));
                    }
                    session = new Session(this, storageNode, configuration, activate);
                    singleConnectionOwner = session;
                }
            }
            else
            {
                session = new Session(this, storageNode, configuration, activate);
            }

            NotifySessionOpen(session);
            return(session);
        }
Exemple #2
0
 internal void SetStorageNode(StorageNode node)
 {
     if (storageNode != null)
     {
         throw new InvalidOperationException(Strings.ExStorageNodeIsAlreadySelected);
     }
     Handler.SetStorageNode(node);
     storageNode = node;
 }
Exemple #3
0
        // Constructors

        internal Session(Domain domain, StorageNode selectedStorageNode, SessionConfiguration configuration, bool activate)
            : base(domain)
        {
            Guid = Guid.NewGuid();
            IsDebugEventLoggingEnabled = OrmLog.IsLogged(LogLevel.Debug); // Just to cache this value

            // Both Domain and Configuration are valid references here;
            // Configuration is already locked
            Configuration  = configuration;
            Name           = configuration.Name;
            identifier     = Interlocked.Increment(ref lastUsedIdentifier);
            CommandTimeout = configuration.DefaultCommandTimeout;
            allowSwitching = configuration.Supports(SessionOptions.AllowSwitching);
            storageNode    = selectedStorageNode;

            // Handlers
            Handlers = domain.Handlers;
            Handler  = CreateSessionHandler();

            // Caches, registry
            EntityStateCache               = CreateSessionCache(configuration);
            EntityChangeRegistry           = new EntityChangeRegistry(this);
            EntitySetChangeRegistry        = new EntitySetChangeRegistry(this);
            ReferenceFieldsChangesRegistry = new ReferenceFieldsChangesRegistry(this);
            entitySetsWithInvalidState     = new HashSet <EntitySetBase>();

            // Events
            EntityEvents = new EntityEventBroker();
            Events       = new SessionEventAccessor(this, false);
            SystemEvents = new SessionEventAccessor(this, true);

            // Etc.
            PairSyncManager                 = new SyncManager(this);
            RemovalProcessor                = new RemovalProcessor(this);
            pinner                          = new Pinner(this);
            Operations                      = new OperationRegistry(this);
            NonPairedReferencesRegistry     = new NonPairedReferenceChangesRegistry(this);
            CommandProcessorContextProvider = new CommandProcessorContextProvider(this);

            // Validation context
            ValidationContext = Configuration.Supports(SessionOptions.ValidateEntities)
        ? (ValidationContext) new RealValidationContext()
        : new VoidValidationContext();

            // Creating Services
            Services = CreateServices();

            disposableSet = new DisposableSet();
            remapper      = new KeyRemapper(this);

            disableAutoSaveChanges = !configuration.Supports(SessionOptions.AutoSaveChanges);

            // Perform activation
            if (activate)
            {
                AttachToScope(new SessionScope());
            }

            // Query endpoint
            SystemQuery = Query = new QueryEndpoint(new QueryProvider(this));
        }
        internal async Task <Session> OpenSessionInternalAsync(SessionConfiguration configuration, StorageNode storageNode, SessionScope sessionScope, CancellationToken cancellationToken)
        {
            ArgumentValidator.EnsureArgumentNotNull(configuration, nameof(configuration));
            configuration.Lock(true);

            if (isDebugEventLoggingEnabled)
            {
                OrmLog.Debug(Strings.LogOpeningSessionX, configuration);
            }

            Session session;

            if (SingleConnection != null)
            {
                // Ensure that we check shared connection availability
                // and acquire connection atomically.
                lock (singleConnectionGuard) {
                    if (singleConnectionOwner != null)
                    {
                        throw new InvalidOperationException(string.Format(
                                                                Strings.ExSessionXStillUsesSingleAvailableConnection, singleConnectionOwner));
                    }
                    session = new Session(this, storageNode, configuration, false);
                    singleConnectionOwner = session;
                }
            }
            else
            {
                // DO NOT make session active right from constructor.
                // That would make session accessible for user before
                // connection become opened.
                session = new Session(this, storageNode, configuration, false);
                try {
                    await((SqlSessionHandler)session.Handler).OpenConnectionAsync(cancellationToken)
                    .ContinueWith(t => {
                        if (sessionScope != null)
                        {
                            session.AttachToScope(sessionScope);
                        }
                    }, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.ExecuteSynchronously)
                    .ConfigureAwait(false);
                }
                catch (OperationCanceledException) {
                    await session.DisposeSafelyAsync().ConfigureAwait(false);

                    throw;
                }
            }
            NotifySessionOpen(session);
            return(session);
        }