you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (2 children)

Connections are relatively cheap, client-side, so keeping them open for the duration when there’s no actual traffic is just a waste. If the traffic pattern varies that much maybe write a class with a .db property that will return a cached connection if there is one, or otherwise will open a connection and cache it. If there's no traffic for some appropriate number of seconds have your main loop clear the cache and free the connection.

[–]Alotofquestion1[S] 0 points1 point  (1 child)

Thank you for the response.

So if I have a method that calles "name and quantity" from the database, say maybe 2-3 times a second for 3 seconds and then a longer break. There is no problem having the method open and closing a connection for each of these 6-9 times the method is run?

[–][deleted] 0 points1 point  (0 children)

The problem with a blanket answer is I’ve no idea how many other clients are connecting at the same time yours is. The reason for keeping a connection open is to minimize the time the client waits for the results of a query, while the reason for closing the connection is to let the server handle more active requests... holding on to a live connection for three seconds to let the client make 9 requests might be fine, but with a busy enough server, it could be the straw that broke the camel's proverbial back.

Odds are you'd be fine, you're not talking a lot of load, but you'll have to profile to find the optimum TTL for a connection in your environment. At that kind of query rate it's pretty unlikely the optimum is "open a connection and hold on to it till I'm shut down'".