-4

I'm working on an app in Asp.net.
I have a DBHandler class which handles database connections and queries,
the DBHandler constructor opens a new OleDBConnection, DBHandler's Dispose function closes and disposes of the connection, normally this works.

Some code from the DBHandler class:

readonly OleDbConnection Con;
        public DBHandler()
        {
            string cs = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
            Con = new OleDbConnection(cs);
            Con.Open();
        }
public void Dispose()
        {

            Con.Close();
            Con.Dispose();
            Console.WriteLine("db.dispose");
        }

I also have a few request controllers/handlers which handle get and post requests,
here is some example code:

    public class CountryInfoHandler : HttpMessageHandler
    {
        protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage message, CancellationToken token)
        {
            HttpContent content;
            HttpResponseMessage response = new HttpResponseMessage();
            ......
            CountryInfo info = null;
            using (DBHandler db = new DBHandler())
            {
                if (db.DoesCountryExist(parsedUri[3]))
                {
                    info = db.Countries[parsedUri[3]];
                }
                else
                {
                    info = null;
                }
             }
             content = new StringContent(JsonConvert.SerializeObject(info));
             content.Headers.ContentType.MediaType = "application/json";
             response = new HttpResponseMessage(HttpStatusCode.OK)
             {
                 Content = content
             };
             .....
             return response;
        }
    }


Everything works well unless multiple requests which require calls to the database come in at the same time, when that happens, I get a System.Runtime.InteropServices.SEHException.
I'm guessing this is caused by multiple database connections being open at once, is there any way to either prevent this from happening or have multiple connections without any problems?

I'm currently thinking of making it a static class with OpenConnection and CloseConnection functions that all work on a single connection but are there any better ways?

matthias_h
  • 11,356
  • 9
  • 22
  • 40
GideonMax
  • 526
  • 4
  • 11
  • You need to share the relevant code for us to understand what could be the issue – Chetan Mar 22 '20 at 17:29
  • Not an exact duplicate, but the right answer (in .NET) is to NOT have a single reusable connection. They are designed to be used only for a single (or a few serial) command. – D Stanley Mar 23 '20 at 12:15
  • I removed my close vote since you don't seem to be sharing the handler. If you have anything that uses a handler statically then you'll have problems, but I don't see that in the code that you show.. – D Stanley Mar 23 '20 at 15:24
  • Please add information about what OleDb provider you are using, and, if possible, code for a simple console app that reproduces the issue. – David Browne - Microsoft Mar 23 '20 at 15:28
  • what db is this? is the `InnerException` null? – mxmissile Mar 23 '20 at 15:32

1 Answers1

0

I have found a work around to this problem,
I made the class a static class and added open and close functions,
whenever they're called an int (which defaults to zero) that represents the amount of locations using the class is either incremented or decremented,
if it becomes 0 I close the connection, if it was zero and got incremented I open the connection.

GideonMax
  • 526
  • 4
  • 11