/// <summary>
        /// Opens a database connection with the property settings specified by the <see cref="ConnectionString" />.
        /// </summary>
        /// <remarks>
        /// The <see cref="AseConnection" /> draws an open connection from the connection pool if one is available.
        /// Otherwise, it establishes a new connection to an instance of ASE.
        /// </remarks>
        public override void Open()
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(nameof(AseConnection));
            }

            if (State == ConnectionState.Open)
            {
                return; //already open
            }

            if (State != ConnectionState.Closed)
            {
                throw new InvalidOperationException("Cannot open a connection which is not closed");
            }

            InternalState = ConnectionState.Connecting;

            try
            {
                var parameters = ConnectionParameters.Parse(_connectionString);

                _internal = _connectionPoolManager.Reserve(_connectionString, parameters, _eventNotifier, UserCertificateValidationCallback);

                InternalConnectionTimeout = parameters.LoginTimeout;
            }
            catch (Exception)
            {
                InternalState = ConnectionState.Closed;
                throw;
            }

            InternalState = ConnectionState.Open;
        }
Beispiel #2
0
        /// <summary>
        /// Opens a database connection with the property settings specified by the <see cref="ConnectionString" />.
        /// </summary>
        /// <remarks>
        /// The <see cref="AseConnection" /> draws an open connection from the connection pool if one is available.
        /// Otherwise, it establishes a new connection to an instance of ASE.
        /// </remarks>
        public override void Open()
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(nameof(AseConnection));
            }

            if (State == ConnectionState.Open)
            {
                return; //already open
            }

            if (State != ConnectionState.Closed)
            {
                throw new InvalidOperationException("Cannot open a connection which is not closed");
            }

            InternalState = ConnectionState.Connecting;

            var parameters = ConnectionParameters.Parse(_connectionString);

            _internal = _connectionPoolManager.Reserve(_connectionString, parameters);

            InternalConnectionTimeout = parameters.LoginTimeout;

            InternalState = ConnectionState.Open;
        }
Beispiel #3
0
        /// <summary>
        ///     Initializes the internal context, discovers and initializes sets, and initializes from a model if one is provided.
        /// </summary>
        private void InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model = null)
        {
            DbConfigurationManager.Instance.EnsureLoadedForContext(GetType());

            _internalContext = new LazyInternalContext(
                this, internalConnection, model, DbConfiguration.GetService <IDbModelCacheKeyFactory>());
            DiscoverAndInitializeSets();
        }
 internal virtual void InitializeLazyInternalContext(
     IInternalConnection internalConnection,
     DbCompiledModel model = null)
 {
     DbConfigurationManager.Instance.EnsureLoadedForContext(this.GetType());
     this._internalContext = (InternalContext) new LazyInternalContext(this, internalConnection, model, DbConfiguration.DependencyResolver.GetService <Func <DbContext, IDbModelCacheKey> >(), DbConfiguration.DependencyResolver.GetService <AttributeProvider>(), (Lazy <DbDispatchers>)null, (ObjectContext)null);
     this.DiscoverAndInitializeSets();
 }
Beispiel #5
0
 public override void OverrideConnection(IInternalConnection connection)
 {
     connection.AppConfig = this.AppConfig;
     if (connection.ConnectionHasModel != this._internalConnection.ConnectionHasModel)
     {
         throw this._internalConnection.ConnectionHasModel ? Error.LazyInternalContext_CannotReplaceEfConnectionWithDbConnection() : Error.LazyInternalContext_CannotReplaceDbConnectionWithEfConnection();
     }
     this._internalConnection.Dispose();
     this._internalConnection = connection;
 }
Beispiel #6
0
        /// <summary>
        ///     Initializes the internal context, discovers and initializes sets, and initializes from a model if one is provided.
        /// </summary>
        private void InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model = null)
        {
            DbConfigurationManager.Instance.EnsureLoadedForContext(GetType());

            _internalContext = new LazyInternalContext(
                this, internalConnection, model
                , DbConfiguration.DependencyResolver.GetService <Func <DbContext, IDbModelCacheKey> >()
                , DbConfiguration.DependencyResolver.GetService <AttributeProvider>());
            DiscoverAndInitializeSets();
        }
        /// <summary>
        /// TODO - document this once transactions are supported.
        /// </summary>
        public override void Close()
        {
            if (State == ConnectionState.Closed)
            {
                return;
            }

            _connectionPoolManager.Release(_connectionString, _internal);
            _internal     = null;
            InternalState = ConnectionState.Closed;
        }
Beispiel #8
0
        public void Release(string connectionString, IInternalConnection connection)
        {
            if (connection == null)
            {
                return; //essentially, it's released :)
            }

            if (Pools.TryGetValue(connectionString, out var pool))
            {
                pool.Release(connection);
            }
        }
 private void RemoveConnection(IInternalConnection connection = null)
 {
     lock (_mutex)
     {
         try
         {
             connection?.Dispose();
         }
         finally
         {
             PoolSize--;
         }
     }
 }
Beispiel #10
0
        /// <summary>
        ///     Constructs a <see cref="LazyInternalContext" /> for the given <see cref="DbContext" /> owner that will be initialized
        ///     on first use.
        /// </summary>
        /// <param name="owner">
        ///     The owner <see cref="DbContext" /> .
        /// </param>
        /// <param name="internalConnection"> Responsible for creating a connection lazily when the context is used for the first time. </param>
        /// <param name="model"> The model, or null if it will be created by convention </param>
        public LazyInternalContext(
            DbContext owner,
            IInternalConnection internalConnection,
            DbCompiledModel model,
            IDbModelCacheKeyFactory cacheKeyFactory = null)
            : base(owner)
        {
            DebugCheck.NotNull(internalConnection);

            _internalConnection = internalConnection;
            _model           = model;
            _cacheKeyFactory = cacheKeyFactory ?? new DefaultModelCacheKeyFactory();

            _createdWithExistingModel = model != null;
        }
        /// <summary>
        ///     Constructs a <see cref = "LazyInternalContext" /> for the given <see cref = "DbContext" /> owner that will be initialized
        ///     on first use.
        /// </summary>
        /// <param name = "owner">The owner <see cref = "DbContext" />.</param>
        /// <param name = "internalConnection">Responsible for creating a connection lazily when the context is used for the first time.</param>
        /// <param name = "model">The model, or null if it will be created by convention</param>
        public LazyInternalContext(
            DbContext owner,
            IInternalConnection internalConnection,
            DbCompiledModel model,
            IDbModelCacheKeyFactory cacheKeyFactory = null)
            : base(owner)
        {
            //Contract.Requires(internalConnection != null);

            _internalConnection = internalConnection;
            _model = model;
            _cacheKeyFactory = cacheKeyFactory ?? new DefaultModelCacheKeyFactory();

            _createdWithExistingModel = model != null;
        }
Beispiel #12
0
        /// <summary>
        /// TODO - document this once transactions are supported.
        /// </summary>
        public override void Close()
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException(nameof(AseConnection));
            }

            if (State == ConnectionState.Closed)
            {
                return;
            }

            _connectionPoolManager.Release(_connectionString, _internal);
            _internal     = null;
            InternalState = ConnectionState.Closed;
        }
Beispiel #13
0
 public LazyInternalContext(
     DbContext owner,
     IInternalConnection internalConnection,
     DbCompiledModel model,
     Func <DbContext, IDbModelCacheKey> cacheKeyFactory = null,
     AttributeProvider attributeProvider = null,
     Lazy <DbDispatchers> dispatchers    = null,
     ObjectContext objectContext         = null)
     : base(owner, dispatchers)
 {
     this._internalConnection = internalConnection;
     this._model                    = model;
     this._cacheKeyFactory          = cacheKeyFactory ?? new Func <DbContext, IDbModelCacheKey>(new DefaultModelCacheKeyFactory().Create);
     this._attributeProvider        = attributeProvider ?? new AttributeProvider();
     this._objectContext            = objectContext;
     this._createdWithExistingModel = model != null;
 }
Beispiel #14
0
        /// <summary>
        ///     Constructs a <see cref="LazyInternalContext" /> for the given <see cref="DbContext" /> owner that will be initialized
        ///     on first use.
        /// </summary>
        /// <param name="owner">
        ///     The owner <see cref="DbContext" /> .
        /// </param>
        /// <param name="internalConnection"> Responsible for creating a connection lazily when the context is used for the first time. </param>
        /// <param name="model"> The model, or null if it will be created by convention </param>
        public LazyInternalContext(
            DbContext owner,
            IInternalConnection internalConnection,
            DbCompiledModel model,
            Func <DbContext, IDbModelCacheKey> cacheKeyFactory = null,
            AttributeProvider attributeProvider = null,
            Lazy <Dispatchers> dispatchers      = null)
            : base(owner, dispatchers)
        {
            DebugCheck.NotNull(internalConnection);

            _internalConnection = internalConnection;
            _model             = model;
            _cacheKeyFactory   = cacheKeyFactory ?? new DefaultModelCacheKeyFactory().Create;
            _attributeProvider = attributeProvider ?? new AttributeProvider();

            _createdWithExistingModel = model != null;
        }
Beispiel #15
0
        /// <inheritdoc />
        public override void OverrideConnection(IInternalConnection connection)
        {
            DebugCheck.NotNull(connection);

            // Connection should not be changed once context is initialized
            Debug.Assert(_creatingModel == false);
            Debug.Assert(_objectContext == null);

            connection.AppConfig = AppConfig;

            if (connection.ConnectionHasModel
                != _internalConnection.ConnectionHasModel)
            {
                throw _internalConnection.ConnectionHasModel
                          ? Error.LazyInternalContext_CannotReplaceEfConnectionWithDbConnection()
                          : Error.LazyInternalContext_CannotReplaceDbConnectionWithEfConnection();
            }

            _internalConnection.Dispose();

            _internalConnection = connection;
        }
Beispiel #16
0
        public void Release(IInternalConnection connection)
        {
            if (!_parameters.Pooling)
            {
                connection?.Dispose();
                return;
            }

            if (connection == null)
            {
                return;
            }

            var now = DateTime.UtcNow;

            if (ShouldRemoveAndReplace(connection, now))
            {
                RemoveAndReplace(connection);
                return;
            }

            AddToPool(connection);
        }
        public void Release(IInternalConnection connection)
        {
            if (!_parameters.Pooling)
            {
                connection?.Dispose();
                return;
            }

            if (connection == null)
            {
                return;
            }

            var now = DateTime.UtcNow;

            if (ShouldRemoveAndReplace(connection, now))
            {
                RemoveAndReplace(connection);
                Logger.Instance?.WriteLine("Released connection was removed. Creation of a replacement was tasked.");
                return;
            }

            AddToPool(connection);
        }
        /// <inheritdoc />
        public override void OverrideConnection(IInternalConnection connection)
        {
            DebugCheck.NotNull(connection);

            // Connection should not be changed once context is initialized
            Debug.Assert(_creatingModel == false);
            Debug.Assert(_objectContext == null);

            connection.AppConfig = AppConfig;

            if (connection.ConnectionHasModel
                != _internalConnection.ConnectionHasModel)
            {
                throw _internalConnection.ConnectionHasModel
                          ? Error.LazyInternalContext_CannotReplaceEfConnectionWithDbConnection()
                          : Error.LazyInternalContext_CannotReplaceDbConnectionWithEfConnection();
            }

            _internalConnection.Dispose();

            _internalConnection = connection;
        }
 private void RemoveAndReplace(IInternalConnection connection)
 {
     RemoveConnection(connection);
     Task.Run(TryReplaceConnection);
 }
Beispiel #20
0
 public NonInitializingLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model)
     : base(new Mock <DbContext>().Object, internalConnection, model)
 {
 }
        // <inheritdoc />
        public override void OverrideConnection(IInternalConnection connection)
        {
            DebugCheck.NotNull(connection);

            throw Error.EagerInternalContext_CannotSetConnectionInfo();
        }
        /// <summary>
        ///     Constructs a <see cref="LazyInternalContext" /> for the given <see cref="DbContext" /> owner that will be initialized
        ///     on first use.
        /// </summary>
        /// <param name="owner">
        ///     The owner <see cref="DbContext" /> .
        /// </param>
        /// <param name="internalConnection"> Responsible for creating a connection lazily when the context is used for the first time. </param>
        /// <param name="model"> The model, or null if it will be created by convention </param>
        public LazyInternalContext(
            DbContext owner,
            IInternalConnection internalConnection,
            DbCompiledModel model,
            Func<DbContext, IDbModelCacheKey> cacheKeyFactory = null,
            AttributeProvider attributeProvider = null,
            Lazy<Dispatchers> dispatchers = null)
            : base(owner, dispatchers)
        {
            DebugCheck.NotNull(internalConnection);

            _internalConnection = internalConnection;
            _model = model;
            _cacheKeyFactory = cacheKeyFactory ?? new DefaultModelCacheKeyFactory().Create;
            _attributeProvider = attributeProvider ?? new AttributeProvider();

            _createdWithExistingModel = model != null;
        }
 private bool ShouldRemoveAndReplace(IInternalConnection connection, DateTime now)
 {
     return(connection.IsDoomed ||
            (_parameters.ConnectionLifetime > 0 && _parameters.ConnectionLifetime < (now - connection.Created).TotalSeconds) ||
            (_parameters.ConnectionIdleTimeout > 0 && _parameters.ConnectionIdleTimeout < (now - connection.LastActive).TotalSeconds));
 }
 internal override void InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model = null)
 {
     // do nothing - just placeholder object
 }
 private void AddToPool(IInternalConnection connection)
 {
     _available.Add(connection);
     Logger.Instance?.WriteLine("Released connection was placed in available queue");
 }
        /// <inheritdoc />
        public override void OverrideConnection(IInternalConnection connection)
        {
            DebugCheck.NotNull(connection);

            throw Error.EagerInternalContext_CannotSetConnectionInfo();
        }
 internal override void InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model = null)
 {
     // do nothing - just placeholder object
 }
 /// <inheritdoc />
 public override void OverrideConnection(IInternalConnection connection)
 {
     throw Error.EagerInternalContext_CannotSetConnectionInfo();
 }
Beispiel #29
0
 public abstract void OverrideConnection(IInternalConnection connection);
Beispiel #30
0
 public override void OverrideConnection(IInternalConnection connection)
 {
     throw Error.EagerInternalContext_CannotSetConnectionInfo();
 }