OdbcConnection Automatically Closing When Opening Another via API

Greg K 101 Reputation points
2021-04-13T23:41:23.12+00:00

I have an application that opens an OdbcConnection via System DSN. It stays open up until the point I have to make an async API call that has its own SQL connection. Is there something causing the OdbcConnection to automatically close if I need to use another connection, even if its an API call that manages its own connection on the API layer? I've never seen this before even using multiple connections within the same application.

The main application is a worker service using .NET 5 and C#. It open the OdbcConnection but then I make an async API call and when that is called, it closes my OdbcConnection for some reason. They aren't even pointing to the same database obviously. Is there something going on or can I prevent this from happening?

Thanks,
Greg

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,191 questions
{count} votes

Accepted answer
  1. Greg K 101 Reputation points
    2021-04-14T15:41:13.677+00:00

    I have figured out what the issue was. I was basically mixing synchronous and async calls. So, when I made the first await call, it returned back since the calling method was not awaited and executed the code to close the ODBC connection. Once I added await and async to my calling methods, the connection did not close until all methods were completed.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Greg K 101 Reputation points
    2021-04-14T13:21:58.277+00:00

    It's not that basic.

    I am not keeping the ODBC connection open - only for the lifetime of the process. Here is the flow:

    • Process starts
    • Open ODBC connection using DSN to connect to legacy file-based system.
    • Retrieve data from the legacy system
    • While processing retrieved data from legacy system, I make an API call to a ,NET 5 API application (using async methods).
    • When I call the API, the ODBC connection closes (this is my problem as it shouldn't).
    • Return back from the API call, need to process and get more data from the ODBC connection based on returned data.
    • Try to retrieve data from the legacy system again but the ODBC connection is closed.
    • Process ends so close the ODBC connection.

    The legacy system is really picky when it comes to opening and closing many connections so we try to limit it to one opening and closing for each process. We have a connection limit of 25 so within a single process, I probably retrieve data 10-15 times for various things. I cannot open and close that many times because it doesn't release the connection immediately and then we run out of connections on the legacy system.

    0 comments No comments