private void ConnectionOnOnFailure(object sender, FailureEventArgs failureEventArgs) { var connection = sender as IConnection; if (null != connection) { connection.OnFailure -= ConnectionOnOnFailure; if (_connection?.connection == connection) { _connection = null; return; } else { // find the connection in connections and remove it. if (appendConnections.Length == 0) { return; } lock (this) { var connections = appendConnections; var leftConnections = connections.Where(c => c.connection != connection).ToArray(); if (leftConnections.Length != connections.Length) { // means we found it in connections. _cluster.DisposeConnection(connection); appendConnections = leftConnections; } } } } }
private connectionInfo prepareConnection() { connectionInfo connection; if (null == (connection = _connection)) { lock (this) { if (null == (connection = _connection)) { var conn = _cluster.GetConnection(); conn.OnFailure += ConnectionOnOnFailure; var cl = _consistencyLevel ?? conn.DefaultConsistencyLevel; var executionFlags = _executionFlags ?? conn.DefaultExecutionFlags; var futPrepare = new PrepareQuery(conn, cl, executionFlags, _cql).AsFuture(); futPrepare.Wait(); Tuple <byte[], IColumnSpec[]> preparedInfo = futPrepare.Result.Single(); var newconn = new connectionInfo() { connection = conn, }; newconn.id = preparedInfo.Item1; newconn.columnSpecs = preparedInfo.Item2; _connection = newconn; } } } return(_connection); }
private connectionInfo peakConnection() { var currentConnections = appendConnections; var availableConnection = currentConnections.Where(c => c.connection.GetAvailableStreamIdCount > Transport.LongRunningConnection.SUGGEST_AVIALABLE_STREAM_COUNT).FirstOrDefault(); if (availableConnection != null) { return(availableConnection); } lock (this) { // when we enter locked section, we can check again! // in case we were blocked by previous lock, then we can get new one. currentConnections = appendConnections; availableConnection = currentConnections.Where(c => c.connection.GetAvailableStreamIdCount > Transport.LongRunningConnection.SUGGEST_AVIALABLE_STREAM_COUNT).FirstOrDefault(); if (availableConnection != null) { return(availableConnection); } // well, we were the first lock section, make a new connection! var newConnections = new connectionInfo[appendConnections.Length + 1]; if (appendConnections.Length > 0) { Array.Copy(appendConnections, 0, newConnections, 0, appendConnections.Length); _cluster.GetLogger.Info("prepared query extended to connection count {0}", newConnections.Length); } var connection = _cluster.GetConnection(null, true); var coninfo = new connectionInfo() { connection = connection, }; connection.OnFailure += ConnectionOnOnFailure; var cl = _consistencyLevel ?? connection.DefaultConsistencyLevel; var executionFlags = _executionFlags ?? connection.DefaultExecutionFlags; var futPrepare = new PrepareQuery(connection, cl, executionFlags, _cql).AsFuture(); futPrepare.Wait(); Tuple <byte[], IColumnSpec[]> preparedInfo = futPrepare.Result.Single(); coninfo.id = preparedInfo.Item1; coninfo.columnSpecs = preparedInfo.Item2; newConnections[newConnections.Length - 1] = coninfo; appendConnections = newConnections; return(coninfo); } }
public PreparedQuery(ICluster cluster, ConsistencyLevel?consistencyLevel, ExecutionFlags?executionFlags, IDataMapperFactory factoryIn, IDataMapperFactory factoryOut, string cql) { _cluster = cluster; _consistencyLevel = consistencyLevel; _executionFlags = executionFlags; _factoryIn = factoryIn; _factoryOut = factoryOut; _cql = cql; _connection = null; appendConnections = new connectionInfo[] { }; }
public void Dispose() { if (_connection != null) { _connection.connection.OnFailure -= ConnectionOnOnFailure; _connection = null; } if (appendConnections.Length > 0) { lock (this) { var connections = appendConnections; foreach (var c in connections.Where(x => x != null)) { c.connection.OnFailure -= ConnectionOnOnFailure; _cluster.DisposeConnection(c.connection); } appendConnections = new connectionInfo[] { }; } } }