コード例 #1
0
        private void RemoveTransactionAndCloseConnection(ContextUoW workItem)
        {
            _rwLock.EnterWriteLock();
            _workItems.Remove(workItem);
            _rwLock.ExitWriteLock();

            _connection.Close();
        }
コード例 #2
0
        /// <summary>
        /// Creates a new <see cref="ContextUoW"/>.
        /// </summary>
        /// <param name="isolationLevel">The <see cref="IsolationLevel"/> used for the transaction inside this unit of work. Default value: <see cref="IsolationLevel.ReadCommitted"/></param>
        /// <returns></returns>
        public ContextUoW CreateUnitOfWork(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted)
        {
            CreateOrReuseConnection();

            //To create a transaction, our connection needs to be open.
            //If we need to open the connection ourselves, we're also in charge of closing it when this transaction commits or rolls back.
            //This will be done by RemoveTransactionAndCloseConnection in that case.
            bool wasClosed = _connection.State == ConnectionState.Closed;

            if (wasClosed)
            {
                _connection.Open();
            }

            try
            {
                ContextUoW     unit;
                IDbTransaction transaction = _connection.BeginTransaction(isolationLevel);

                if (wasClosed)
                {
                    unit = new ContextUoW(transaction, RemoveTransactionAndCloseConnection, RemoveTransactionAndCloseConnection);
                }
                else
                {
                    unit = new ContextUoW(transaction, RemoveTransaction, RemoveTransaction);
                }

                _rwLock.EnterWriteLock();
                _workItems.AddLast(unit);
                _rwLock.ExitWriteLock();

                return(unit);
            }
            catch
            {
                //Close the connection if we're managing it, and if an exception is thrown when creating the transaction.
                if (wasClosed)
                {
                    _connection.Close();
                }

                throw; //Rethrow the original transaction
            }
        }
コード例 #3
0
 private void RemoveTransaction(ContextUoW workItem)
 {
     _rwLock.EnterWriteLock();
     _workItems.Remove(workItem);
     _rwLock.ExitWriteLock();
 }