/// <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; }
/// <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; }
/// <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(); }
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; }
/// <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; }
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--; } } }
/// <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; }
/// <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; }
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; }
/// <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; }
/// <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; }
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); }
private void RemoveAndReplace(IInternalConnection connection) { RemoveConnection(connection); Task.Run(TryReplaceConnection); }
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(); }
/// <inheritdoc /> public override void OverrideConnection(IInternalConnection connection) { throw Error.EagerInternalContext_CannotSetConnectionInfo(); }
public abstract void OverrideConnection(IInternalConnection connection);
public override void OverrideConnection(IInternalConnection connection) { throw Error.EagerInternalContext_CannotSetConnectionInfo(); }