SAP Sybase SQL Anywhere NullReference Exception when openening and closing many connections in a service
<p>Currently I've the problem that SAP Sybase SQL Anywhere randomly throws <code>NullReferenceException</code>s in a service which executes a lot of sql queries. The connections are always created in a <code>using</code> block and opened &amp; closed correctly. There are not many parallel connections, but after a while (randomly) the following exception is thrown when opening and closing connections:</p>
<blockquote><pre>
Exception: System.NullReferenceException: The object was not set to an instance.
   bei iAnywhere.Data.SQLAnywhere.SAConnection.Dispose(Boolean disposing)
   bei iAnywhere.Data.SQLAnywhere.SAConnection.Close()
   bei iAnywhere.Data.SQLAnywhere.SAConnection.get_State()
   bei product.Framework.DAL.ConnectionManager.GetOpenPoolConnection[T](String ModuleName, String Connection, Boolean resizePoolOnRimteOut, Int64 Time
Out, Boolean isSecondTry)
   bei product.Framework.DAL.ORM.Sybase.SybaseStack.LoadDataFromDB[T](String where_part, String connectionStringName, Object[] sa_params)
   bei product.Framework.DAL.ORM.Sybase.SybaseStack.LoadData[T](String optWherePart, Object[] parameter)
   bei product.PlugIn.DocCenterClient.AS_Modules.DefaultInstanceDataExportModule.DoSingalProcessing()
   bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCt
x)
   bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   bei System.Threading.ThreadHelper.ThreadStart()
</pre></blockquote>
<p>Does someone know what is causing this behavior? We could not figure out any regularity behind it.</p>
<p>The exception is being thrown in <code>GetOpenPoolConnection</code>, which creates a new <code>SAConnection</code> and opens it:</p>
<pre><code>internal static T GetOpenPoolConnection&lt;T&gt;(string Connection = &quot;Default&quot;) where T : DbConnection
{
    // Read connection string from static dictionary
    string cConnectionString = GetConnectionString(Connection);
    T cToReturn = null;

    if (cConnectionString != null)
    {
        if (typeof(T) == typeof(SqlConnection))
        {
            cToReturn = (new SqlConnection(cConnectionString) as T);
            cToReturn.Open();
        }
        else if (typeof(T) == typeof(SAConnection))
        {
            cToReturn = (new SAConnection(cConnectionString) as T);
            cToReturn.Open();
        }
        else if (typeof(T) == typeof(OdbcConnection))
        {
            cToReturn = (new OdbcConnection(cConnectionString) as T);
            cToReturn.Open();
        }

        return cToReturn;
    }
    else
    {
        return null;
    }
}
</code></pre>
<p>This function is called as:</p>
<pre><code>using (SAConnection connection = DAL.ConnectionManager.GetOpenPoolConnection&lt;SAConnection&gt;())
{
    var res = connection.Query&lt;Guid&gt;(&quot;SELECT InstanceDataGuid FROM AS_EX_Objects WHERE ExchangeObjectId = ?&quot;, new { ExchangeObjectId = ic.ItemId.ToString() });
    if (res.Any())
    {
        instanceDataGuid = res.Single&lt;Guid&gt;();
   }
}
</code></pre>
<p>We are using SAP SQL Anywhere 12 as database engine/server and SAP SQL Anywhere 16 as client component. The project and DB driver are 64-bit only. All the bugs that cause this should be fixed in the version of the ADO .Net Driver we're using (in the <a href="http://sqlasupport.sap.com/readme/sqla1201.html" rel="nofollow noreferrer">bugfixes</a>, see engineering cases #797124, #741721 and #738144).</p>