i need to invoke a mysql connection and make a dummy query after every 20hrs to the connections present so that the connections doesnot get closed. Here i need a call to function( this function implements a connection and a dummy query) that independently be running the in backend. please suggest the timers functions that run in background and invoke the function after every 20hrs..
Asked
Active
Viewed 175 times
1
-
1If you're going so long without making a query such that it closes the connection, why don't you just keep the connection closed unless you need it? – corsiKa Dec 22 '11 at 06:49
-
i am keeping the connections in pool.i am reusing the old connections instead of closing and creating new one which is a cost effective then reusing. so thats why i need a timer function. i will try ur suggestion. thanks – satya Dec 22 '11 at 06:55
4 Answers
4
Not an answer to your question, but a useful tip:
Do not use a dummy query to fire every 20 hours to keep mysql connection keep alive. Better to go with connection pool technique. I will suggest you to use c3p0
.
For more help see this discussion: Connection pooling options with JDBC: DBCP vs C3P0
-
@Jasonw: Yes, BoneCP is also an alternative.+1. [BoneCP WebSite](http://jolbox.com/) – Harry Joy Dec 22 '11 at 07:02
-
with bonecp or c3p0 you can specify a validationQuery like 'SELECT 1' to keep up the connection. – proko Dec 22 '11 at 07:07
1
Do you mean like this:
http://docs.oracle.com/javase/1.5.0/docs/api/javax/swing/Timer.html
int delay = 1000 * 60 * 60 * 20; //milliseconds
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
}
};
new Timer(delay, taskPerformer).start();
You can also use an operating system scheduler (like "cron" on Linux, or Task Mgr on Windows).

paulsm4
- 114,292
- 17
- 138
- 190
1
Look at Quartz. It is an enerprise job scheduler.
The advantages for using quatz are:
- You do not have to care about task or thread development
- You need to develop solely the methods which need to be invoked on a timed basis
- You configure schedules etc. with defined and documented property files
Disadvantages are
- You introduce overhead

fyr
- 20,227
- 7
- 37
- 53
1
java.util.Timer can schedule events at pre-defined periods (like the timer mentioned by paulsm4 but no need to involve SWING/AWT).

Paul Jowett
- 6,513
- 2
- 24
- 19