Is there any reason to make one connection to the database when the server starts? That is to make a singleton that will once connect to the database when the server starts. And close it when you stop.

  • 2
    as with all other singletons - no - etki

2 answers 2

It is necessary at the start to make a pool of connections and increase / decrease the number of connections as needed.

  • not worth it. very difficult. + There may be problems with the definition of the pool. Found an easier solution. - Tsyklop

The solution is to create one connection to the database for each user and store it in the session. After the session time, close the connection in the listener.

  • If there are 50 users, then there will be 50 connections, even if the user does not do anything with the base? - Suvitruf
  • @Suvitruf yes. If you take a pool, it will be the same thing. Creating one global connection is also not an option. If there are many users, this will affect the performance. - Tsyklop
  • @Tsyklop makes sense to allocate from the pool a connection for at least every request that requires working with the database. Holding on each user by the connection is a wild search, if you plan to use the service more, for example, 100 users at a time. - Regent
  • @Regent why bust? it will close itself after 30 minutes if the user is not active. and requests will go regularly. There are many nuances in the pool. you need to constantly make their number. Do not exclude the fact that there is a multithreading. - Tsyklop
  • @Tsyklop hundreds of libraries already exist (I’m talking about pools). "If you take a pool, it will be the same thing." Will not be, as connections are reused. We, for example, have enough of a pool of 10 connections for hundreds / thousands of users. - Suvitruf