Reputation: 2298
I have a java program running 24/7. It accesses mysql database every 3 seconds only from 9.am to 3.PM. In this case when should I should open and close the MySql connection?
Should I open and close every 3 sec?
Should I open at 9.am and close at 3.pm?
Should I open once when the program start and never close it. But reconnect when connection is closed automatically and exceptions is thrown?
Upvotes: 1
Views: 803
Reputation: 11292
If you do not want to leave connections open overnight, you might able to configure your connection pool to open connections on demand and close them when they have been idle for a certain period of time -- say, 15 minutes. This would give you the benefit of being able to query the database whenever you wish and not having too many idle connections.
Upvotes: 2
Reputation: 9368
Why don't you simply use a connection pool. If that is too tedious since the connection will be frequently used you can reuse the same one imho.
Upvotes: 3