public ISqlDataSource GetDataSource(bool createIfNotExists) { ISqlDataSource ret = null; if (_credentials.DSN.Length > 0) { ret = new MySqlDataSource(this); } else { DataSourceType type = SqlDataSource.GetDriverType(_credentials); switch (type) { case DataSourceType.MySqlOdbc: ret = new MySqlDataSource(this); break; case DataSourceType.MySqlNative: default: ret = new MySqlNativeDataSource(this); break; } } return(ret); }
public DatabaseConnection GetConnection() { DatabaseConnection retConn = null; Exception reThrow = null; lock (this) { while (_availableConnectionStack.Count > 0) { /** take the first one */ DatabaseConnection connection = _availableConnectionStack[0]; _availableConnectionStack.RemoveAt(0); if (connection.LastUse <= DateTime.UtcNow - TimeSpan.FromSeconds(UNUSED_CONNECTION_TIMEOUT_SECS) || connection.DbConnection.State != ConnectionState.Open) { try { connection.DbConnection.Close(); } catch { /** this can't fail */ } continue; } else { connection.LastUse = DateTime.UtcNow; retConn = connection; _allocatedConnectionList.Add(retConn, retConn); if (LastConnectionLogTime < DateTime.UtcNow - TimeSpan.FromSeconds(LOG_CONNECTION_SECS)) { //Log.SysLogText(LogLevel.DEBUG, "******* DB CONNECTIONS: ALLOCED: {0} AVAILABLE: {1}", m_AllocatedConnectionList.Count, m_AvailableConnectionStack.Count); LastConnectionLogTime = DateTime.UtcNow; } break; } } if (retConn == null) { try { DataSourceType type = SqlDataSource.GetDriverType(_credentials); switch (type) { case DataSourceType.MySqlNative: retConn = MySqlNativeDataSource.CreateConnection(_credentials); // new OdbcConnection(strConnect); break; default: retConn = SqlDataSource.CreateConnection(_credentials); // new OdbcConnection(strConnect); break; } retConn.DbConnection.Open(); _allocatedConnectionList.Add(retConn, retConn); if (SqlConnectionStatusUpdate != null) { SqlConnectionStatusUpdate(this, true, retConn); } } catch (Exception e) { reThrow = new Exception(string.Format("DBUtil EXCEPTION: {0} CONNECT WAS {1}", e.Message, retConn.DbConnection.ConnectionString), e); if (SqlConnectionStatusUpdate != null) { SqlConnectionStatusUpdate(this, false, retConn); } } } } if (reThrow != null) { throw reThrow; } #if DEBUG_CONNECTION_POOL if (m_Credentials.UserName == "spunak") { Log.LogToConsoleAndDebug("CONNDBG: GetConn Allocced New {0:x} - available: {1} alloced: {2} caller: {3}", retConn.GetHashCode(), m_AvailableConnectionStack.Count, m_AllocatedConnectionList.Count, GetCallingMethod()); } #endif return(retConn); }