1

I am trying to delete a row in my table. Here is my sample code

String DBDate = new String();
Long date = new Date().getTime();           
String dateToday = date.toString();             
dbobj.clear();
dbobj.put(LAST_TIME_ANSWERED, "");
int hasDB = dbobj.getFromDB(tableName,column key, primary key);

if(hasDB != 0) {
    DBDate = dbobj.get(LAST_TIME_ANSWERED);
    if(DBDate != dateToday) {
        dbobj.deleteClientDB(tableName, column key, primary key);
    }
}

How could I change the date of a timer? Any help would be nice. Thanks in advance!

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
Kryll_D
  • 11
  • 1

3 Answers3

0

This is awkward to achieve in java, because you need to run a job on a schedule to achieve this. You should strongly consider using the mysql event scheduler. See this answer for an example of this.

When you insert the row in the db you should set up an event that is triggered exactly once, 24-hours later, with the necessary sql to delete the row. You would end up with one event per row.

Alternatively, with java and a properly configured server, after each row insert you could add a cron or quartz job that would trigger once, 24-hours later, that would invoke a java function to remove the row. This is harder than using the db's built-in event scheduler.

Depending on your JEE version (>6) you could use an EJB 3.1 timer, as detailed in this blog post.

Of these options, the db event scheduler is probably the best, because the others can go wrong if the JEE sever is down.

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
0

One common practice is to delete database records which are older than some threshold value. For this purpose, the database table should have a column with a timestamp representing insert or update time. When executing the scheduled task there a command is performed (pseudocode, assuming an 'update' timestamp):

DELETE FROM TABLE_NAME WHERE LAST_UPDATED < (CURRENT_TIME - THRESHOLD)

Or there is a select query performed to retrieve all these records and handle their deletion one by one including logging and maybe some conditional logic.

To achieve this functionality java SE provides ScheduledExecutorService which can be used to execute delayed tasks:

    // initialize scheduler
    ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    // schedule repeating task
    ScheduledFuture future = scheduler.scheduleAtFixedRate(
            () -> {
                System.out.println("Excuting task...");
            },
            60,
            60,
            TimeUnit.SECONDS
    );

The future object can be used to cancel task:

future.cancel(true);
scheduler.shutdown();

But when using enterprise solutions, there are normally libraries and frameworks, providing functionality for executing scheduled tasks: quartz scheduler, spring framework task scheduling or EJB timer services.

Software Engineer
  • 15,457
  • 7
  • 74
  • 102
Daniil
  • 913
  • 8
  • 19
  • To be clear, you have recommended a job that runs frequently and which clears anything that has gone over the 24-hour threshold. This may be sufficient. However, if you want to delete a record as soon as it reaches 24-hours then you'd have to have a timer per row. Also, this question clearly asks for JEE, so SE and Spring are not in context (though nice to have). – Software Engineer Feb 19 '20 at 08:43
-1

there are two options for this. 1.) use java scheduler or 2.) use a scheduled job at DB level to clear data at the end of each day

Chiran K.
  • 406
  • 3
  • 16
  • This is basically what we call a 'one line answer', and they are discouraged on SO. It would be good if you could expand on this a bit. Perhaps give some guidance as to what the options represent and how to choose between them. – Software Engineer Feb 19 '20 at 08:37
  • things which could be said in simple, should not be written like essays too :) – Chiran K. Feb 19 '20 at 08:41