2

My Java App has a lot of queries ( ~1000 queries/each button). Can I open JDBC connection when my app is launched and keep it instead of opening a connection every single time the button pressed and closing it after 1k queries? I understand that it is possible, but would it be better or no? Can it be lost somehow? I use free mysql hosting that's why every time I open/close a new one it takes a lot of time.

Vasyl Butov
  • 454
  • 1
  • 4
  • 19
  • 1
    You might find a little bit of useful information in https://stackoverflow.com/q/7280825/924 the general advice seems to be to use a Connection Pool instead of a single connection. – Brandon Haugen Jul 01 '18 at 23:38
  • *"Can it be lost somehow?"* Yes. If server is rebooted. If you lose connectivity. If you don't use it for a while, server may time it out and discard it. – Andreas Jul 02 '18 at 00:07
  • Adding to @BrandonHaugen comment, you can have a pool with max idle/active connection set to 1. This way you will have the best of both the worlds. – gagan singh Jul 02 '18 at 00:18
  • In this case is that possible use procedures on Database instead? You can call the function with the queries and part of your logic is exec on DB a way more efficient. – Leonardo Roese Jul 02 '18 at 00:27

1 Answers1

3

No. It can be closed by the server any time, typically on an idle timeout. You should get a new connection per transaction, and you should mitigate the ill-effects of that by using a connection pool like Apache DBCP.

user207421
  • 305,947
  • 44
  • 307
  • 483