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.
Asked
Active
Viewed 1,628 times
2
-
1You 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 Answers
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
-
Relevant meta post: [Answering a question, then closing it as duplicate](https://meta.stackoverflow.com/questions/286072/answering-a-question-then-closing-it-as-duplicate) – Hovercraft Full Of Eels Jul 02 '18 at 00:33
-
-
It's happened to me too at times. Usually I delete the answer and post a comment, or make the answer a community wiki – Hovercraft Full Of Eels Jul 02 '18 at 00:35
-
Funny, it's my up-vote too, since it was a lot better answer than Luis's – Hovercraft Full Of Eels Jul 02 '18 at 00:35
-
@HovercraftFullOfEels I just read the meta post, and many of the attitudes expressed there seem to me to be totally bizarre. The question about whether anybody with enough reputation to close-hammer really needs to game the system is about the only sensible thing there.. – user207421 Jul 02 '18 at 02:05
-
Dunno, it's an odd predicament, and so I looked up the only meta reference I could find. – Hovercraft Full Of Eels Jul 02 '18 at 02:27