This project has moved and is read-only. For the latest updates, please go here.
1
Vote

Transaction.Rollback throws NullReferenceException

description

Hi!
There is a serious bug in the ProviderBase.Run implementation.

When the bulkinsert takes too long, so SQL Server threw a SQLException "Timeout Expired". This is caught in the "Run" method and the transaction is rolledback.
However, as the connection is not valid anymore (or for whatever else reason) the call to "Transaction.Rollback()" in that case itself throws a NullREferenceException.
Problem is, that this completely hides the original error, as the only thing you see in the logs is that nonsense-NullReferenceException.

So I had to change the code in the following way:
 public virtual void Run<T>(IEnumerable<T> entities)
        {
            using (var dbConnection = GetConnection())
            {
                dbConnection.Open();

                Exception exception = null;
                try
                {
                    using (var transaction = dbConnection.BeginTransaction())
                    {
                        try
                        {
                            Run(entities, transaction);
                            transaction.Commit();
                        }
                        catch (Exception x)
                        {
                            exception = x;
                            if (transaction.Connection != null)
                            {

                                transaction.Rollback();
                            }
                            throw;
                        }
                    }
                }
                catch (NullReferenceException)
                {
                    if (exception != null)
                        throw exception;
                    throw;
                }
            }
        }
That way, in case there is an NRE, I check if there is any causing exception. If so, I throw that, otherwise I just throw the NRE.

Please fix this.

Also, if you could move to github, I could do pullrequests. Would be much nicer

comments