private IDbConnection GetConnection()
        {
            IDbConnection connection;

            if (ScopeIsActive)
            {
                var transaction = ThreadStorage.GetData <IDbTransaction>(TransactionScope.ScopeTransactionKey);

                if (transaction != null)
                {
                    connection = transaction.Connection;
                }
                else
                {
                    connection = this.GetDbConnection();

                    connection.Open();

                    transaction = connection.BeginTransaction();
                    ThreadStorage.SetData(TransactionScope.ScopeTransactionKey, transaction);
                }
            }
            else
            {
                connection = this.GetDbConnection();

                connection.Open();
            }

            return(connection);
        }
Beispiel #2
0
        public void Dispose()
        {
            var transaction = ThreadStorage.GetData <IDbTransaction>(ScopeTransactionKey);

            if (transaction == null || !ThreadStorage.GetData <bool>(ActiveScopeKey))
            {
                return;
            }

            var connection = transaction.Connection;

            if (_complete)
            {
                transaction.Commit();
            }
            else
            {
                transaction.Rollback();
            }

            if (connection != null && connection.State != ConnectionState.Closed)
            {
                connection.Dispose();
            }

            ThreadStorage.ClearData(ActiveScopeKey);
            ThreadStorage.ClearData(ScopeTransactionKey);
        }