# Azure SQL: Adding a new job is failing

**URL:** <https://discuss.hangfire.io/t/azure-sql-adding-a-new-job-is-failing/2844>\
**Category:** bug?\
**Tags:** sql-azure, distributed-locks\
**Created:** [January 13, 2017, 6:05am UTC](https://discuss.hangfire.io/t/azure-sql-adding-a-new-job-is-failing/2844 "2017-01-13T06:05:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaushal](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@kaushal](https://discuss.hangfire.io/u/kaushal)\
**Post date:** [January 13, 2017, 6:05am UTC](https://discuss.hangfire.io/t/azure-sql-adding-a-new-job-is-failing/2844/1 "2017-01-13T06:05:02Z")

</div>

**Getting the below error**

> Inner Exception: “Object reference not set to an instance of an object.”  
> Stack Trace:  
> at System.Transactions.Transaction.GetPromotedToken()

The error comes from the line

` connection.EnlistTransaction(Transaction.Current);`

in below code even if i set enlist=false in the connection string.

> ```
> internal T UseTransaction<T>([InstantHandle] Func<DbConnection, DbTransaction, T> func, IsolationLevel? isolationLevel)
> 
> ```

```
  {
> using (var transaction = CreateTransaction(isolationLevel ?? _options.TransactionIsolationLevel))
> {
> var result = UseConnection(connection =>
> {
> connection.EnlistTransaction(Transaction.Current);
> return func(connection, null);
> });
> transaction.Complete();
> return result;
> }
> }

```

when we set enlist=true, then we get error at the line

`connection.Open();`

from the below code

```
internal DbConnection CreateAndOpenConnection()
        {
            if (_existingConnection != null)
            {
                return _existingConnection;
            }

            var connection = new SqlConnection(_connectionString);
            connection.Open();

            return connection;
        }

```

both the methods are present under Hangfire.SqlServer.SqlServerStorage class

Same code works when connected to the local DB (SQL server 2014).

Landed on a discussion [c# - WCF Transaction against Azure SQL DB - Stack Overflow](http://stackoverflow.com/questions/36648561/wcf-transaction-against-azure-sql-db)

not sure if it is relevant for this issue.

Please help.  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![kaushal](https://avatars.discourse-cdn.com/v4/letter/k/a88e4f/32.png) [@kaushal](https://discuss.hangfire.io/u/kaushal)\
**Post date:** [January 19, 2017, 5:48am UTC](https://discuss.hangfire.io/t/azure-sql-adding-a-new-job-is-failing/2844/2 "2017-01-19T05:48:32Z")

</div>

Found the issue. call to method to queue job was inside transaction which was somehow leading to Distribution Transaction and Azure SQL doesn’t support it.

Consider the code below:

```
        public override void UpdateFile(int intUniqueId)
        {
            using (var scope = GetTransactionScope(...))
            {
                 QueueJob<Updater>(x => x.Update(token, intUniqueId));
                scope.Complete();
            }
        }

        private static void QueueJob<T>(Expression<Action<T>> action)
        {
                BackgroundJob.Enqueue(action);
        }

```

Removing the transaction scope in callee method works.
