private void ReleaseConnection(DataConnection connection, bool inError = false) { if (connection == null) { return; } //close reader if exist if (connection.ActiveReader != null) { connection.ActiveReader.Close(); connection.ActiveReader = null; } if (inError) { if (connection.DbTransaction != null) { connection.Abort(); } connection.Close(); return; } //Do not close if there's a transaction, or it is a long-living connection if (connection.DbTransaction != null || connection.Lifetime == ConnectionLifetime.Explicit) { return; } //Otherwise, close it connection.Close(); if (connection.Session.CurrentConnection == connection) { connection.Session.CurrentConnection = null; } }
public object ExecuteDbCommand(IDbCommand command, DataConnection connection, DbExecutionType executionType, Func <IDataReader, int> resultsReader = null) { object result; IDataReader reader = null; connection.Session.SetLastCommand(command); try { command.Connection = connection.DbConnection; command.Transaction = connection.DbTransaction; var start = CurrentTickCount; int recordCount = -1; result = _driver.ExecuteCommand(command, executionType); if (executionType == DbExecutionType.Reader) { reader = (IDataReader)result; if (resultsReader != null) { recordCount = resultsReader(reader); } reader.Close(); } _driver.CommandExecuted(connection, command, executionType); var end = CurrentTickCount; var timeMs = (int)(end - start); LogCommand(connection.Session, command, timeMs, recordCount); return(result); } catch (Exception ex) { // Important: in some cases exception on invalid SQL is not thrown immediately but is thrown later when we try to read the results // ex (MS SQL): WHERE "Name" LIKE 'ABC%' ESCAPE '' - with empty ESCAPE arg string. // So driver.ExecuteDbCommand does NOT catch it, it is thrown only here in call to resultsReader var dex = ex as DataAccessException; if (dex == null) { dex = _driver.ConvertToDataAccessException(ex, command); } if (connection.DbTransaction != null) { connection.Session.LogMessage(" -- Aborting transaction on error"); connection.Abort(); } connection.Session.LogMessage(" -- Failed command text: "); LogCommand(connection.Session, command, 0); ReviewExceptionAndAddInfo(dex); LogException(connection.Session, dex); if (reader != null) { reader.Close(); } throw dex; } finally { command.Transaction = null; command.Connection = null; } }//method
public object ExecuteDbCommand(IDbCommand command, DataConnection connection, DbExecutionType executionType, Func<IDataReader, int> resultsReader = null) { object result; IDataReader reader = null; connection.Session.SetLastCommand(command); try { command.Connection = connection.DbConnection; command.Transaction = connection.DbTransaction; var start = CurrentTickCount; int recordCount = -1; result = _driver.ExecuteCommand(command, executionType); if(executionType == DbExecutionType.Reader) { reader = (IDataReader)result; if(resultsReader != null) recordCount = resultsReader(reader); reader.Close(); } _driver.CommandExecuted(connection, command, executionType); var end = CurrentTickCount; var timeMs = (int)(end - start); LogCommand(connection.Session, command, timeMs, recordCount); return result; } catch(Exception ex) { // Important: in some cases exception on invalid SQL is not thrown immediately but is thrown later when we try to read the results // ex (MS SQL): WHERE "Name" LIKE 'ABC%' ESCAPE '' - with empty ESCAPE arg string. // So driver.ExecuteDbCommand does NOT catch it, it is thrown only here in call to resultsReader var dex = ex as DataAccessException; if(dex == null) dex = _driver.ConvertToDataAccessException(ex, command); if (connection.DbTransaction != null) { connection.Session.LogMessage(" -- Aborting transaction on error"); connection.Abort(); } connection.Session.LogMessage(" -- Failed command text: "); LogCommand(connection.Session, command, 0); ReviewExceptionAndAddInfo(dex); LogException(connection.Session, dex); if(reader != null) reader.Close(); throw dex; } finally { command.Transaction = null; command.Connection = null; } }
private void ReleaseConnection(DataConnection connection, bool inError = false) { if(connection == null) return; //close reader if exist if (connection.ActiveReader != null) { connection.ActiveReader.Close(); connection.ActiveReader = null; } if (inError) { if (connection.DbTransaction != null) connection.Abort(); connection.Close(); return; } //Do not close if there's a transaction, or it is a long-living connection if(connection.DbTransaction != null || connection.Lifetime == ConnectionLifetime.Explicit) return; //Otherwise, close it connection.Close(); if (connection.Session.CurrentConnection == connection) connection.Session.CurrentConnection = null; }