Reputation: 459
I have a java application which has remote access in an online MySQL Database, and it works fine.
The only problem is that after a few minutes, the connection to the server was closed, is there anyway to keep the connection active? So that I won't have to instantiate another connection just to perform the task.
Any ideas and inputs would be much appreciated. Thanks in Advance.
Upvotes: 0
Views: 499
Reputation: 11
jdbc:mysql://[host]:[port]/[database]?autoReconnect=true&user=root&password=passwordIchose
Upvotes: 1
Reputation: 61148
As other answers have pointed out you can use the autoReconnect
option on the connection to solve the immediate problem.
However, if you are not using a database connection pool I would strongly suggest that you switch to one.
They have a number of advantages:
If you manage connections yourself you will almost certainly end up writing some form of connection pool - and why reinvent the wheel.
By the time you have implemented connection checking so that they don't go stale, some sort of connection holder so that you don't need to re-open them each time, some sort of exception handling code you are well on your way to writing a lot of code that has already been written and thoroughly tested.
I have used dbcp and boneCP and both are very easy to use and configure and will save you hours and hours of frustration dealing with JDBC connection issues.
Upvotes: 3
Reputation: 7692
You need to auto reconnect, try editing your JDBC URL:
jdbc:mysql://[host]:[port]/[database]?autoReconnect=true
See connector reference: http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html
Upvotes: 3