repetetively write to db or text file

Short version: if you had to save the current date and time every minute, overwriting the existing entry how would you do it?

writing it to a text file, a database, or a third option? And possibly, what would be considered best practice.

my scenario is i want to know, on application startup, when was the last time the application was running.

Long version:

I have a question of performance, and maybe also proper coding style.

i have an application that keeps track of a food stock, and when some things get stale and has to be removed. There's a timertask running every minute checking the current items in stock and if they have exceeded the date and time where they are considered stale.

the issue is, if the application shuts down and restarts, items gone stale in during downtime will not have been checked.

code for the repeated method

runeveryminute = new TimerTask() {

        @Override
        public void run() {

                for (Item i : lager.getAllItems) {

                    if (i.getMinutesTillStale == 0) {
                    JOptionPane.showConfirmDialog(MainFrame.this,"WARNING",
                    "Alarm",
                    JOptionPane.OK_CANCEL_OPTION);
                    }
                }
            }
        }
    };

I don't really agree to this way of checking if something has gone stale, but it's a school project so it isn't that important.

i added a window listener to the mainframe, but i'm uncertain if i can rely on it, to catch unexpected crashes, like power failure.

Answers


It depends on how i.getMinutesTillStale is defined. Will getMinutesTillStale give a negative value if an item has gone stale during downtime? Then you can change the condition to i.getMinutesTillStale <= 0.

Suggestion: Are you storing the items information in a database? e.g., records like itemId, datestored, expiryperiod(or dateofExpiry) Then you can write a stored procedure or a query with the timer time as the input. It will check all the items which are expired and send you the list of those items.

Let me know if this helps.


Need Your Help

Why is JSON not validating against schema?

json jsonschema

I get an "Additional properties are not allowed" error on each and every key in the JSON item. Below is the schema and item.

Fixed Bar like facebook/wibiya with jquery/YUI/prototype

jquery jquery-ui jquery-plugins yui scriptaculous

I was just wondering if any one knows of a good script to emulate a bar like wibiya or the facebook notification bar that used to appear at the bottom(just before the current facebook homepage chan...