public virtual IStoreResults ExecuteOperation(string operationName, XElement operationData) { return(SqlUtils.WithSqlExceptionHandling <IStoreResults>(() => { SqlResults results = new SqlResults(); using (SqlCommand cmd = _conn.CreateCommand()) using (XmlReader input = operationData.CreateReader()) { cmd.Transaction = _tran; cmd.CommandText = operationName; cmd.CommandType = CommandType.StoredProcedure; SqlUtils.AddCommandParameter(cmd, "@input", SqlDbType.Xml, ParameterDirection.Input, -1, new SqlXml(input)); SqlParameter result = SqlUtils.AddCommandParameter(cmd, "@result", SqlDbType.Int, ParameterDirection.Output, 0, 0); using (SqlDataReader reader = cmd.ExecuteReader()) { results.Fetch(reader); } // Output parameter will be used to specify the outcome. results.Result = (StoreResult)result.Value; } return results; })); }
/// <summary> /// Open the store connection. /// </summary> public virtual void Open() { SqlUtils.WithSqlExceptionHandling(() => { _conn.Open(); }); }
/// <summary> /// Constructs an instance of an atom transaction scope. /// </summary> /// <param name="kind">Type of transaction scope.</param> /// <param name="conn">Connection to use for the transaction scope.</param> protected internal SqlStoreTransactionScope(StoreTransactionScopeKind kind, SqlConnection conn) { this.Kind = kind; _conn = conn; switch (this.Kind) { case StoreTransactionScopeKind.ReadOnly: SqlUtils.WithSqlExceptionHandling(() => { _tran = conn.BeginTransaction(IsolationLevel.ReadCommitted); }); break; case StoreTransactionScopeKind.ReadWrite: SqlUtils.WithSqlExceptionHandling(() => { _tran = conn.BeginTransaction(IsolationLevel.RepeatableRead); }); break; default: // Do not start any transaction. Debug.Assert(this.Kind == StoreTransactionScopeKind.NonTransactional); break; } }
/// <summary> /// Performs actual Dispose of resources. /// </summary> /// <param name="disposing">Whether the invocation was from IDisposable.Dipose method.</param> protected virtual void Dispose(bool disposing) { if (disposing) { if (_tran != null) { SqlUtils.WithSqlExceptionHandling(() => { try { if (this.Success) { _tran.Commit(); } else { _tran.Rollback(); } } catch (InvalidOperationException) { // We ignore zombied transactions. } finally { _tran.Dispose(); _tran = null; } }); } } }
/// <summary> /// Open the store connection, and acquire a lock on the store. /// </summary> /// <param name="lockId">Lock Id.</param> public virtual void OpenWithLock(Guid lockId) { SqlUtils.WithSqlExceptionHandling(() => { _conn.Open(); this.GetAppLock(lockId); }); }
/// <summary> /// Closes the store connection. /// </summary> public virtual void Close() { SqlUtils.WithSqlExceptionHandling(() => { if (_conn != null) { _conn.Dispose(); _conn = null; } }); }
/// <summary> /// Closes the store connection after releasing lock. /// <param name="lockId">Lock Id.</param> /// </summary> public virtual void CloseWithUnlock(Guid lockId) { SqlUtils.WithSqlExceptionHandling(() => { if (_conn != null) { if (_conn.State == ConnectionState.Open) { this.ReleaseAppLock(lockId); } _conn.Dispose(); _conn = null; } }); }
public virtual void ExecuteCommandBatch(IEnumerable <StringBuilder> commands) { foreach (StringBuilder batch in commands) { SqlUtils.WithSqlExceptionHandling(() => { using (SqlCommand cmd = _conn.CreateCommand()) { cmd.Transaction = _tran; cmd.CommandText = batch.ToString(); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } }); } }
public virtual IStoreResults ExecuteCommandSingle(StringBuilder command) { return(SqlUtils.WithSqlExceptionHandling <IStoreResults>(() => { SqlResults results = new SqlResults(); using (SqlCommand cmd = _conn.CreateCommand()) { cmd.Transaction = _tran; cmd.CommandText = command.ToString(); cmd.CommandType = CommandType.Text; using (SqlDataReader reader = cmd.ExecuteReader()) { results.Fetch(reader); } } return results; })); }
/// <summary> /// Terminates connection on the source shard object. /// </summary> private void KillConnectionsOnSourceShard() { SqlUtils.WithSqlExceptionHandling(() => { string sourceShardConnectionString = this.GetConnectionStringForShardLocation(_mappingSource.StoreShard.Location); IStoreResults result; using (IStoreConnection connectionForKill = this.Manager.StoreConnectionFactory.GetConnection(StoreConnectionKind.LocalSource, sourceShardConnectionString)) { connectionForKill.Open(); using (IStoreTransactionScope ts = connectionForKill.GetTransactionScope(StoreTransactionScopeKind.NonTransactional)) { result = ts.ExecuteOperation( StoreOperationRequestBuilder.SpKillSessionsForShardMappingLocal, StoreOperationRequestBuilder.KillSessionsForShardMappingLocal(_patternForKill)); } } if (result.Result != StoreResult.Success) { // Possible errors are: // StoreResult.UnableToKillSessions // StoreResult.StoreVersionMismatch // StoreResult.MissingParametersForStoredProcedure throw StoreOperationErrorHandler.OnShardMapErrorLocal( result, _shardMap, _mappingSource.StoreShard.Location, _errorCategory, StoreOperationErrorHandler.OperationNameFromStoreOperationCode(this.OperationCode), StoreOperationRequestBuilder.SpKillSessionsForShardMappingLocal); } }); }