public void Dispose()
 {
     if (Context.TransactionModeOn == false)
     {
         CurrentConnection.Close();
     }
 }
Пример #2
0
        protected virtual void Dispose(bool disposing)
        {
            // If it's working, give it a few seconds to see if can finish
            for (int i = 1; IsWorking() && i <= 300; ++i)
            {
                Thread.Sleep(10);
            }

            if (!IsWorking())
            {
                if (CurrentTransaction != null)
                {
                    CurrentTransaction.Dispose();
                    CurrentTransaction = null;
                }

                if (CurrentConnection != null)
                {
                    CurrentConnection.Close();
                    CurrentConnection.Dispose();
                    CurrentConnection = null;
                }
            }
            else
            {
//#if DEBUG
//                if (System.Diagnostics.Debugger.IsAttached)
//                {
//                    System.Diagnostics.Debugger.Break();
//                }
//#endif
                CurrentTransaction = null;
                CurrentConnection  = null;
            }
        }
Пример #3
0
 public void CloseCurrentConnection()
 {
     if (CurrentConnection != null)
     {
         CurrentConnection.Close();
     }
 }
Пример #4
0
        /// <summary>
        /// Creates a connection to the database informed.
        /// </summary>
        /// <param name="dbName">Enumeration of type <see cref="DatabaseName"/>.</param>
        private void InitDbConnection(DatabaseName dbName)
        {
            if (IsEnlisted)
            {
                return;
            }

            if (CurrentConnection != null && CurrentConnection.State != ConnectionState.Closed && CurrentConnection.State != ConnectionState.Broken)
            {
                //if there is a connection opened to another db, throw an exception
                if (CurrentDbName != dbName)
                {
                    throw new DatabaseException($"The repository cannot request a connection to database '{dbName}' because it possesses an open connection to database {CurrentDbName}.");
                }

                //auto-rollback on dispose
                if (CurrentTransaction?.Connection != null)
                {
                    CurrentTransaction.Rollback();
                }

                if (CurrentConnection.State != ConnectionState.Closed || CurrentConnection.State != ConnectionState.Broken)
                {
                    CurrentConnection.Close();

                    CurrentConnection = null;
                }
            }

            CurrentConnection = Context.CreateConnection(dbName);
            CurrentDbName     = dbName;
        }
Пример #5
0
        /// <summary>
        /// Runs a repository operation either as part of an outer unit of work or a standalone one.
        /// If it's the latter case and CurrentConnection is null, it will connect to the CurrentDBName.
        /// </summary>
        /// <param name="fn">A delegate function that will be executed.</param>
        /// <param name="commandType"></param>
        protected void Run(Action <SqlCommand> fn, CommandType commandType = CommandType.StoredProcedure)
        {
            try
            {
                //we create a new connection without a transaction
                if (!IsEnlisted)
                {
                    InitDbConnection(CurrentDbName);
                }

                //we create a command that will be used by the current repository
                using (var cmd = CurrentConnection.CreateCommand())
                {
                    cmd.Transaction = CurrentTransaction;
                    cmd.CommandType = commandType;

                    fn((SqlCommand)cmd);
                }
            }
            finally
            {
                //if the repository is not enlisted within an outer unit of work,
                //we can commit the transaction and close it's connection.
                if (!IsEnlisted)
                {
                    CurrentConnection.Close();
                }
            }
        }
Пример #6
0
 public void Rollback()
 {
     CurrentTransaction?.Rollback();
     CurrentTransaction?.Dispose();
     if (CurrentConnection?.State == ConnectionState.Open)
     {
         CurrentConnection.Close();
     }
     CurrentConnection?.Dispose();
 }
Пример #7
0
 public void Commit()
 {
     CurrentTransaction?.Commit();
     CurrentTransaction?.Dispose();
     if (CurrentConnection?.State == ConnectionState.Open)
     {
         CurrentConnection.Close();
     }
     CurrentConnection?.Dispose();
 }
Пример #8
0
 public void CloseConnection()
 {
     if (CurrentTransaction != null)
     {
         CurrentTransaction.Rollback();
     }
     if (CurrentConnection != null)
     {
         CurrentConnection.Close();
         CurrentConnection  = null;
         CurrentTransaction = null;
     }
 }
Пример #9
0
 /// <summary>
 /// Execute the SQL query and return the results in a new DataSet.
 /// </summary>
 /// <param name="theCmdStr">SQL Query</param>
 /// <returns>DataSet object</returns>
 public System.Data.DataSet ExecuteDataSet(string theCmdStr)
 {
     System.Data.DataSet ds = new DataSet();
     try
     {
         SqlDataAdapter adapter = new SqlDataAdapter(theCmdStr, CurrentConnection);
         adapter.Fill(ds, "dataSet");
         CurrentConnection.Close();
     }
     catch
     {
         throw;
     }
     return(ds);
 }
Пример #10
0
        /// <summary>
        /// Runs a repository operation as a standalone operation without a transaction.
        /// It will connect to the database informed.
        /// </summary>
        /// <param name="dbName">Enumeration of type <see cref="DatabaseName"/>.</param>
        /// <param name="fn">Delegate function that will be executed with a parameter of type <see cref="IDbCommand"/>.</param>
        /// <param name="commandType"></param>
        protected void Run(DatabaseName dbName, Action <SqlCommand> fn, CommandType commandType = CommandType.StoredProcedure)
        {
            try
            {
                //we create a new connection without a transaction (for now)
                if (!IsEnlisted)
                {
                    InitDbConnection(dbName);
                }
                else
                {
                    if (CurrentDbName != dbName)
                    {
                        throw new DatabaseException($"The current repository is operating on database '{dbName}', but is enlisted to a Unit Of Work on database '{CurrentDbName}'. The repository must be enlisted with the same database as it's Unit Of Work.");
                    }
                }

                //we create a command that will be used by the current repository
                using (var cmd = CurrentConnection.CreateCommand())
                {
                    cmd.Transaction = CurrentTransaction;
                    cmd.CommandType = commandType;

                    fn((SqlCommand)cmd);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //if the repository is not enlisted within an outer unit of work,
                //we can close its connection.
                if (!IsEnlisted)
                {
                    CurrentConnection?.Close();
                }
            }
        }