public static void ClearPool(string connectionString) { lock (_connectionPools) { Pool pool; if (_connectionPools.TryGetValue(connectionString, out pool)) { pool.PoolVersion++; while (pool.Queue.Count > 0) { UtlConnectionProxy target = pool.Queue.Dequeue().Target as UtlConnectionProxy; if (target != null) { try { target.Close(); continue; } catch (Exception) { continue; } } } } } }
public static UtlConnectionProxy Remove(string connectionString, int maxPoolSize, int minPoolSize, out int version) { lock (_connectionPools) { Pool pool; version = _poolVersion; if (!_connectionPools.TryGetValue(connectionString, out pool)) { pool = new Pool(_poolVersion, connectionString, maxPoolSize, minPoolSize); _connectionPools.Add(connectionString, pool); return(null); } version = pool.PoolVersion; pool.MaxPoolSize = maxPoolSize; ResizePool(pool, false); while (pool.Queue.Count > 0) { UtlConnectionProxy target = pool.Queue.Dequeue().Target as UtlConnectionProxy; if ((target != null) && ((target.Created + target.LifeTime) > DateTime.UtcNow.Ticks)) { target.IsPooled = false; return(target); } } return(null); } }
public static void ClearAllPools() { lock (_connectionPools) { foreach (KeyValuePair <string, Pool> pair in _connectionPools) { while (pair.Value.Queue.Count > 0) { UtlConnectionProxy target = pair.Value.Queue.Dequeue().Target as UtlConnectionProxy; if (target != null) { try { target.Close(); continue; } catch (Exception) { continue; } } } if (_poolVersion <= pair.Value.PoolVersion) { _poolVersion = pair.Value.PoolVersion + 1; } } _connectionPools.Clear(); } }
public UtlConnection(ISessionInterface sessionProxy) { this._connectionState = ConnectionState.Closed; this._connectionOptions = new UtlConnectionOptions(); this._connectionString = string.Empty; this.Version += 1L; this.InnerConnection = new UtlConnectionProxy(sessionProxy, this._connectionOptions, this); this._connectionState = ConnectionState.Open; }
private void Cleanup(UtlConnectionProxy cnn) { if (this.DisposeConnection || (!cnn.IsPooled && ((cnn.Owner == null) || cnn.Owner.IsClosed))) { cnn.Dispose(); } this._transaction = null; this._scope = null; this._connectionProxy = null; }
public override void Close() { if ((this.State != ConnectionState.Closed) && (this.InnerConnection != null)) { if (this.InnerConnection.IsEnlisted) { UtlConnection connection = new UtlConnection { InnerConnection = this.InnerConnection }; connection.InnerConnection.Owner = connection; connection.ConnectionString = this.ConnectionString; connection._connectionOptions = this._connectionOptions; connection.Version = this.Version; connection._connectionState = this._connectionState; connection.InnerConnection.Enlistment.DbTransaction.Conn = connection; connection.InnerConnection.Enlistment.DisposeConnection = true; this.InnerConnection = null; } try { if (this.InnerConnection != null) { lock (this.InnerConnection) { this.InnerConnection.DisposeTransaction(); this.InnerConnection.Owner = null; if (this._connectionOptions.Pooling) { UtlConnectionPool.Add(this._connectionString, this.InnerConnection, this._poolVersion); } else { this.InnerConnection.Dispose(); } } } this.InnerConnection = null; } catch (Exception) { } finally { this.OnStateChange(ConnectionState.Closed); } } }
private static void ResizePool(Pool queue, bool forAdding) { int maxPoolSize = queue.MaxPoolSize; if (!forAdding && (maxPoolSize > 0)) { maxPoolSize++; } else if (forAdding && (maxPoolSize > 0)) { maxPoolSize--; } while (queue.Queue.Count > maxPoolSize) { UtlConnectionProxy target = queue.Queue.Dequeue().Target as UtlConnectionProxy; if (target != null) { try { target.Close(); continue; } catch (Exception) { continue; } } } maxPoolSize = queue.MinPoolSize; if (!forAdding && (maxPoolSize > 0)) { maxPoolSize++; } else if (forAdding && (maxPoolSize > 0)) { maxPoolSize--; } while (queue.Queue.Count < maxPoolSize) { UtlConnectionProxy target = new UtlConnectionProxy(new UtlConnectionOptions(queue.ConnectionString)); target.Open(); target.IsPooled = true; queue.Queue.Enqueue(new WeakReference(target, false)); GC.KeepAlive(target); } }
public override void Open() { if (this._connectionState != ConnectionState.Closed) { throw new InvalidOperationException("Connection already open."); } if (this._connectionOptions.Enlist && (Transaction.Current == null)) { throw new InvalidOperationException("No active Transaction to enlist."); } try { if (this._connectionOptions.Pooling) { this.InnerConnection = UtlConnectionPool.Remove(this._connectionString, this._connectionOptions.MaxPoolSize, this._connectionOptions.MinPoolSize, out this._poolVersion); if ((this.InnerConnection != null) && this.InnerConnection.SessionProxy.IsClosed()) { this.InnerConnection = null; } } if (this.InnerConnection == null) { this.InnerConnection = new UtlConnectionProxy(this._connectionOptions, this); this.InnerConnection.Open(); } else { this.InnerConnection.Owner = this; } this.Version += 1L; ConnectionState state = this._connectionState; this._connectionState = ConnectionState.Open; if ((Transaction.Current != null) && this._connectionOptions.Enlist) { this.EnlistTransaction(Transaction.Current); } this._connectionState = state; this.OnStateChange(ConnectionState.Open); } catch (UtlException) { this.Close(); throw; } }
public UtlEnlistment(UtlConnectionProxy connectionProxy, Transaction scope) { this._scope = scope; this._connectionProxy = connectionProxy; System.Data.IsolationLevel readCommitted = System.Data.IsolationLevel.ReadCommitted; switch (this._scope.IsolationLevel) { case System.Transactions.IsolationLevel.Serializable: readCommitted = System.Data.IsolationLevel.Serializable; break; case System.Transactions.IsolationLevel.RepeatableRead: readCommitted = System.Data.IsolationLevel.RepeatableRead; break; case System.Transactions.IsolationLevel.ReadCommitted: readCommitted = System.Data.IsolationLevel.ReadCommitted; break; case System.Transactions.IsolationLevel.ReadUncommitted: readCommitted = System.Data.IsolationLevel.ReadUncommitted; break; case System.Transactions.IsolationLevel.Snapshot: readCommitted = System.Data.IsolationLevel.Snapshot; break; case System.Transactions.IsolationLevel.Chaos: readCommitted = System.Data.IsolationLevel.Chaos; break; case System.Transactions.IsolationLevel.Unspecified: readCommitted = System.Data.IsolationLevel.Unspecified; break; } this._transaction = this._connectionProxy.BeginTransaction(readCommitted); this._scope = scope; this._scope.EnlistVolatile(this, EnlistmentOptions.None); }
public static void Add(string connectionString, UtlConnectionProxy hdl, int version) { lock (_connectionPools) { Pool pool; if ((_connectionPools.TryGetValue(connectionString, out pool) && (version == pool.PoolVersion)) && ((hdl.Created + hdl.LifeTime) > DateTime.UtcNow.Ticks)) { ResizePool(pool, true); hdl.IsPooled = true; pool.Queue.Enqueue(new WeakReference(hdl, false)); GC.KeepAlive(hdl); } else { try { hdl.Close(); } catch (Exception) { } } } }