/// <summary> /// Opens a database connection with the property settings specified by the ConnectionString. /// </summary> public void Open() { if (state == ConnectionState.Open) { throw new MySqlException("error connecting: The connection is already Open (state=Open)."); } SetState(ConnectionState.Connecting); try { if (settings.Pooling) { internalConnection = MySqlPoolManager.GetConnection(settings); } else { internalConnection = new MySqlInternalConnection(settings); internalConnection.Open(); } } catch (Exception ex) { SetState(ConnectionState.Closed); throw ex; } SetState(ConnectionState.Open); internalConnection.SetServerVariables(this); if (settings.Database != null && settings.Database != String.Empty) { ChangeDatabase(settings.Database); } }
private MySqlInternalConnection GetPooledConnection() { MySqlInternalConnection conn = null; // if there are no idle connections and the in use pool is full // then return null to indicate that we cannot provide a connection // at this time. if (idlePool.Count == 0 && inUsePool.Count == maxSize) { return(null); } lock (idlePool.SyncRoot) { for (int i = idlePool.Count - 1; i >= 0; i--) { conn = (idlePool[i] as MySqlInternalConnection); if (conn.IsAlive()) { lock (inUsePool) { inUsePool.Add(conn); } idlePool.RemoveAt(i); return(conn); } else { conn.Close(); idlePool.RemoveAt(i); conn = null; } } } // if we couldn't get a pooled connection and there is still room // make a new one if (conn == null && (idlePool.Count + inUsePool.Count) < maxSize) { conn = CreateNewPooledConnection(); if (conn == null) { return(null); } lock (idlePool.SyncRoot) lock (inUsePool.SyncRoot) { idlePool.Remove(conn); inUsePool.Add(conn); } } return(conn); }
public static void ReleaseConnection( MySqlInternalConnection connection ) { lock (pools.SyncRoot) { string key = connection.Settings.GetConnectionString(); MySqlPool pool = (MySqlPool)pools[ key ]; if (pool == null) throw new MySqlException("Pooling exception: Unable to find original pool for connection"); pool.ReleaseConnection(connection); } }
public static void ReleaseConnection(MySqlInternalConnection connection) { lock (pools.SyncRoot) { string key = connection.Settings.GetConnectionString(); MySqlPool pool = (MySqlPool)pools[key]; if (pool == null) { throw new MySqlException("Pooling exception: Unable to find original pool for connection"); } pool.ReleaseConnection(connection); } }
public void ReleaseConnection(MySqlInternalConnection connection) { lock (idlePool.SyncRoot) lock (inUsePool.SyncRoot) { inUsePool.Remove(connection); if (connection.Settings.ConnectionLifetime != 0 && connection.IsTooOld()) { connection.Close(); } else { idlePool.Add(connection); } } }
private MySqlInternalConnection CreateNewPooledConnection() { lock (idlePool.SyncRoot) lock (inUsePool.SyncRoot) { // first we check if we are allowed to create another if ((inUsePool.Count + idlePool.Count) == maxSize) { return(null); } MySqlInternalConnection conn = new MySqlInternalConnection(settings); conn.Open(); idlePool.Add(conn); return(conn); } }
public MySqlInternalConnection GetConnection() { MySqlInternalConnection conn = null; int start = Environment.TickCount; int ticks = settings.ConnectionTimeout * 1000; // wait timeOut seconds at most to get a connection while (conn == null && (Environment.TickCount - start) < ticks) { conn = GetPooledConnection(); } // if pool size is at maximum, then we must have reached our timeout so we simply // throw our exception if (conn == null) { throw new MySqlException("error connecting: Timeout expired. The timeout period elapsed " + "prior to obtaining a connection from the pool. This may have occurred because all " + "pooled connections were in use and max pool size was reached."); } return(conn); }
public void ReleaseConnection( MySqlInternalConnection connection ) { lock (idlePool.SyncRoot) lock (inUsePool.SyncRoot) { inUsePool.Remove( connection ); if (connection.Settings.ConnectionLifetime != 0 && connection.IsTooOld()) connection.Close(); else idlePool.Add( connection ); } }
private MySqlInternalConnection CreateNewPooledConnection() { lock(idlePool.SyncRoot) lock (inUsePool.SyncRoot) { // first we check if we are allowed to create another if ((inUsePool.Count + idlePool.Count) == maxSize) return null; MySqlInternalConnection conn = new MySqlInternalConnection( settings ); conn.Open(); idlePool.Add( conn ); return conn; } }
private void CheckOutConnection(MySqlInternalConnection conn) { }
/// <summary> /// Opens a database connection with the property settings specified by the ConnectionString. /// </summary> public void Open() { if (state == ConnectionState.Open) throw new MySqlException("error connecting: The connection is already Open (state=Open)."); SetState( ConnectionState.Connecting ); try { if (settings.Pooling) { internalConnection = MySqlPoolManager.GetConnection( settings ); } else { internalConnection = new MySqlInternalConnection( settings ); internalConnection.Open(); } } catch (Exception ex) { SetState( ConnectionState.Closed ); throw ex; } SetState( ConnectionState.Open ); internalConnection.SetServerVariables(this); if (settings.Database != null && settings.Database != String.Empty) ChangeDatabase( settings.Database ); }