/// <include file='Doc/en_EN/FbCommand.xml' path='doc/class[@name="FbCommand"]/method[@name="Dispose(System.Boolean)"]/*'/> protected override void Dispose(bool disposing) { lock (this) { if (!this.disposed) { try { // If there are an active reader close it this.CloseReader(); // Release any unmanaged resources this.Release(); // release any managed resources if (disposing) { this.implicitTransaction = false; this.commandText = null; this.commandTimeout = 0; this.connection = null; this.transaction = null; this.parameters = null; this.namedParameters.Clear(); this.namedParameters = null; } this.disposed = true; } finally { base.Dispose(disposing); } } } }
internal void RollbackImplicitTransaction() { if (this.HasImplicitTransaction && this.transaction != null && this.transaction.Transaction != null) { try { this.transaction.Rollback(); } catch { } finally { this.implicitTransaction = false; this.transaction = null; if (this.statement != null) { this.statement.Transaction = null; } } } }
public void DisposeTransaction() { if (this.activeTransaction != null) { this.activeTransaction.Dispose(); this.activeTransaction = null; } }
public FbTransaction BeginTransaction(FbTransactionOptions options, string transactionName) { lock (this) { if (this.HasActiveTransaction) { throw new InvalidOperationException("A transaction is currently active. Parallel transactions are not supported."); } try { this.activeTransaction = new FbTransaction( this.owningConnection, IsolationLevel.Unspecified); this.activeTransaction.BeginTransaction(options); if (transactionName != null) { this.activeTransaction.Save(transactionName); } } catch (IscException ex) { throw new FbException(ex.Message, ex); } } return this.activeTransaction; }
private void Prepare(bool returnsSet) { FbConnectionInternal innerConn = this.connection.InnerConnection; // Check if we have a valid transaction if (this.transaction == null) { this.implicitTransaction = true; IsolationLevel il = this.connection.ConnectionOptions.IsolationLevel; this.transaction = new FbTransaction(this.connection, il); this.transaction.BeginTransaction(); // Update Statement transaction if (this.statement != null) { this.statement.Transaction = this.transaction.Transaction; } } // Check if we have a valid statement handle if (this.statement == null) { this.statement = innerConn.Database.CreateStatement(this.transaction.Transaction); } // Prepare the statement if needed if (!this.statement.IsPrepared) { // Close the inner DataReader if needed this.CloseReader(); // Reformat the SQL statement if needed string sql = this.commandText; if (this.commandType == CommandType.StoredProcedure) { sql = this.BuildStoredProcedureSql(sql, returnsSet); } try { // Try to prepare the command this.statement.Prepare(this.ParseNamedParameters(sql)); } catch { // Release the statement and rethrow the exception this.statement.Release(); this.statement = null; throw; } // Add this command to the active command list innerConn.AddPreparedCommand(this); } else { // Close statement for subsequently executions this.Close(); } }
/// <include file='Doc/en_EN/FbCommand.xml' path='doc/class[@name="FbCommand"]/constructor[@name="ctor(System.String,FbConnection,Transaction)"]/*'/> public FbCommand(string cmdText, FbConnection connection, FbTransaction transaction) : base() { this.parameters = new FbParameterCollection(); this.namedParameters = new StringCollection(); this.updatedRowSource = UpdateRowSource.Both; this.commandType = CommandType.Text; this.designTimeVisible = true; this.designTimeVisible = true; this.commandTimeout = 30; this.fetchSize = 200; this.commandText = ""; if (connection != null) { this.fetchSize = connection.ConnectionOptions.FetchSize; } if (cmdText != null) { this.CommandText = cmdText; } this.Connection = connection; this.transaction = transaction; }
private void CheckCommand() { if (this.transaction != null && this.transaction.IsUpdated) { this.transaction = null; } if (this.connection == null || this.connection.State != ConnectionState.Open) { throw new InvalidOperationException("Connection must valid and open"); } if (this.activeReader != null) { throw new InvalidOperationException("There is already an open DataReader associated with this Command which must be closed first."); } if (this.transaction == null && this.connection.InnerConnection.HasActiveTransaction) { throw new InvalidOperationException("Execute requires the Command object to have a Transaction object when the Connection object assigned to the command is in a pending local transaction. The Transaction property of the Command has not been initialized."); } if (this.transaction != null && !this.transaction.IsUpdated && !this.connection.Equals(transaction.Connection)) { throw new InvalidOperationException("Command Connection is not equal to Transaction Connection."); } if (this.commandText == null || this.commandText.Length == 0) { throw new InvalidOperationException("The command text for this Command has not been set."); } }
private void SetItemTitleChars(FbTransaction tx, int catId, String title) { using (FbCommand cmd = new FbCommand()) { cmd.Connection = tx.Connection; cmd.Transaction = tx; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "sp_AddCatalogItemTitleChar"; cmd.Parameters.Add("@v_catalog_item_id", FbDbType.Numeric); cmd.Parameters.Add("@v_title_char", FbDbType.Char, 1); cmd.Prepare(); //Build a hash table of the unique lower-case characters from the title foreach (String c in GetUniqueChars(title)) { cmd.Parameters[0].Value = catId; cmd.Parameters[1].Value = c; cmd.ExecuteNonQuery(); } } }
/// <summary> /// Starts the ordered execution of the SQL statements that are in <see cref="SqlStatements"/> collection. /// </summary> /// <param name="autoCommit">Specifies if the transaction should be committed after a DDL command execution</param> public virtual void Execute(bool autoCommit) { if (this.SqlStatements == null || this.SqlStatements.Count == 0) { throw new InvalidOperationException("There are no commands for execution."); } foreach (string sqlStatement in this.SqlStatements) { if (sqlStatement == null || sqlStatement.Length == 0) { continue; } // initializate outputs to default int rowsAffected = -1; FbDataReader dataReader = null; SqlStatementType statementType = FbBatchExecution.GetStatementType(sqlStatement); // Update command configuration this.ProvideCommand().CommandText = sqlStatement; // Check how transactions are going to be handled if (statementType == SqlStatementType.Insert || statementType == SqlStatementType.Update || statementType == SqlStatementType.Delete) { // DML commands should be inside a transaction if (this.sqlTransaction == null) { this.sqlTransaction = this.sqlConnection.BeginTransaction(); } this.sqlCommand.Transaction = this.sqlTransaction; } else if (this.sqlTransaction != null && (statementType != SqlStatementType.Commit && statementType != SqlStatementType.Rollback)) { // Non DML Statements should be executed using // implicit transaction support this.sqlTransaction.Commit(); this.sqlTransaction = null; } try { switch (statementType) { case SqlStatementType.AlterDatabase: case SqlStatementType.AlterDomain: case SqlStatementType.AlterException: case SqlStatementType.AlterIndex: case SqlStatementType.AlterProcedure: case SqlStatementType.AlterTable: case SqlStatementType.AlterTrigger: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.Commit: // raise the event this.OnCommandExecuting(null); this.sqlTransaction.Commit(); this.sqlTransaction = null; // raise the event this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.Connect: // raise the event this.OnCommandExecuting(null); this.ConnectToDatabase(sqlStatement); requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.CreateDatabase: #if (!NETCF) throw new NotImplementedException(); #else throw new NotSupportedException(); #endif case SqlStatementType.CreateDomain: case SqlStatementType.CreateException: case SqlStatementType.CreateGenerator: case SqlStatementType.CreateIndex: case SqlStatementType.CreateProcedure: case SqlStatementType.CreateRole: case SqlStatementType.CreateShadow: case SqlStatementType.CreateTable: case SqlStatementType.CreateTrigger: case SqlStatementType.CreateView: case SqlStatementType.DeclareCursor: case SqlStatementType.DeclareExternalFunction: case SqlStatementType.DeclareFilter: case SqlStatementType.DeclareStatement: case SqlStatementType.DeclareTable: case SqlStatementType.Delete: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.Describe: break; case SqlStatementType.Disconnect: this.sqlConnection.Close(); this.requiresNewConnection = false; break; case SqlStatementType.DropDatabase: #if (!NETCF) throw new NotImplementedException(); #else throw new NotSupportedException(); #endif case SqlStatementType.DropDomain: case SqlStatementType.DropException: case SqlStatementType.DropExternalFunction: case SqlStatementType.DropFilter: case SqlStatementType.DropGenerator: case SqlStatementType.DropIndex: case SqlStatementType.DropProcedure: case SqlStatementType.DropRole: case SqlStatementType.DropShadow: case SqlStatementType.DropTable: case SqlStatementType.DropTrigger: case SqlStatementType.DropView: case SqlStatementType.EventInit: case SqlStatementType.EventWait: case SqlStatementType.Execute: case SqlStatementType.ExecuteImmediate: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.ExecuteProcedure: case SqlStatementType.Fetch: break; case SqlStatementType.Grant: case SqlStatementType.Insert: case SqlStatementType.InsertCursor: case SqlStatementType.Open: case SqlStatementType.Prepare: case SqlStatementType.Revoke: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.Rollback: // raise the event this.OnCommandExecuting(null); this.sqlTransaction.Rollback(); this.sqlTransaction = null; // raise the event this.OnCommandExecuted(sqlStatement, null, -1); break; case SqlStatementType.Select: // raise the event this.OnCommandExecuting(this.sqlCommand); dataReader = this.sqlCommand.ExecuteReader(); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, dataReader, -1); if (!dataReader.IsClosed) { dataReader.Close(); } break; case SqlStatementType.SetGenerator: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; case SqlStatementType.SetDatabase: case SqlStatementType.SetNames: case SqlStatementType.SetSQLDialect: case SqlStatementType.SetStatistics: case SqlStatementType.SetTransaction: case SqlStatementType.ShowSQLDialect: #if (!NETCF) throw new NotImplementedException(); #else throw new NotSupportedException(); #endif case SqlStatementType.Update: case SqlStatementType.Whenever: // raise the event this.OnCommandExecuting(this.sqlCommand); rowsAffected = this.ExecuteCommand(this.sqlCommand, autoCommit); this.requiresNewConnection = false; // raise the event this.OnCommandExecuted(sqlStatement, null, rowsAffected); break; } } catch (Exception ex) { if (this.sqlTransaction != null) { this.sqlTransaction.Rollback(); this.sqlTransaction = null; } throw new FbException(String.Format(CultureInfo.CurrentCulture, "An exception was thrown when executing command: {0}\nBatch execution aborted\nThe returned message was: {1}", sqlStatement, ex.Message)); } } if (this.sqlTransaction != null) { // commit root transaction this.sqlTransaction.Commit(); this.sqlTransaction = null; } this.sqlConnection.Close(); }