Can I keep one jdbc connection for the whole time [duplicate]


Can I keep one jdbc connection for the whole time [duplicate]



This question already has an answer here:



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.



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





You might find a little bit of useful information in stackoverflow.com/q/7280825/924 the general advice seems to be to use a Connection Pool instead of a single connection.
– Brandon Haugen
Jul 1 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 2 at 0: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 2 at 0: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 G. Roese
Jul 2 at 0:27





1 Answer
1



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.





Relevant meta post: Answering a question, then closing it as duplicate
– Hovercraft Full Of Eels
Jul 2 at 0:33





@HovercraftFullOfEels This happens to me a lot somehow.
– EJP
Jul 2 at 0:34





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 2 at 0:35





Funny, it's my up-vote too, since it was a lot better answer than Luis's
– Hovercraft Full Of Eels
Jul 2 at 0: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..
– EJP
Jul 2 at 2:05

Popular posts from this blog

How to add background colour in existing image using Swift?

What's the difference between DDS and SOME/IP?

Moria Casán