.net-coremaxmind

Maxmind Injecting new DatabaseReader as a singletone to avoid re-accessing the file again and again


In a .net core web app, I want inject a new DatabaseReader as a singleton. Therefore i use the AddSingelton in my Startup-Class.

services.AddSingleton(x => new DatabaseReader(pathToFile));

Do you think its a good idea to reuse DatabaseReader?
Thanks


Solution

  • A single connection is a bad idea - if access to the connection is properly locked, it means that website / application could only serve one user at a time.

    This means that you are extremly limited in your application scalability and have no ability to get a lot of users.

    There is also a problem when your connection is not locked very well, things can get weird.

    For example, one thread might dispose the connection while another thread is trying to execute a command against it.

    A better possibility is to use connection pooling by creating a new connection object when you need one. So you can handle many requests at the same time and your limitation should be the database.