Home » Java » java – Deleting a row from MySQL after 24 hours in JEE?-Exceptionshub

java – Deleting a row from MySQL after 24 hours in JEE?-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

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!

How to&Answers:

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.

Answer:

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.

Answer:

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