-1

I am trying to store the time into a database when a user logs-off or logs-in into the computer but when I logs my computer off it does not store my current time into the database where if I press ctrl+c on console or terminate the program from NETBEANS it stores the logout time too. Where am I doing it wrong ? Please help me out with this. Below is my code. Thank you in advance.

  public void logoutTime() throws Exception {

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {

            SimpleDateFormat sdf1 = new SimpleDateFormat("HH:mm:ss");
            String time = String.format(sdf1.format(calendar.getTime()));

            Globals.globalClockBean.setLogout_time(time);
            try {
                cd.insert(Globals.globalClockBean);
            } catch (Exception ex) {
                Logger.getLogger(ClockController.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("Closing: Logged out at yayyyyyyyyy: " + time);

            System.out.println(sdf1.format(calendar.getTime()));
        }

    }));
asad sajjad
  • 85
  • 2
  • 12
  • Did you explicitly call logoutTime() method somewhere? – user6904265 Nov 08 '16 at 10:49
  • yes I did this ... `if (hashedPassword.equals(pass)) { System.out.println("You are logged in as " + loginUsername); ClockView cv = new ClockView(); cv.loginTime(); cv.logoutTime(); cv.timerStart(); cv.stopTimer(); cv.elapsedTime(); } else { System.out.println("Incorrect Password. Try agian"); }` – asad sajjad Nov 08 '16 at 10:53

2 Answers2

3

My guess is that when you log off, operating system shuts down the database before the JMV shutdown hook is called. So when you are trying to call the DB, it is actually down.

Try to setup a file logger and check if an exception is logged after you log off from computer. My guess is that there will be some "Connection failed" type of exception

Igor
  • 592
  • 2
  • 9
  • 31
  • this is exactly what I thought but I don't know how to sort this out ... :( – asad sajjad Nov 08 '16 at 10:56
  • I tried this with logger but after terminating the program from console by pressing `ctrl+c` it started the timer again and then I tried with logging off the system and shutting it off too but nothing happened. `Globals.globalClockBean.setLogout_time(time); try { cd.insert(Globals.globalClockBean); Logger logger2 = Logger.getLogger("Main2"); logger2.info("Shutting down 2"); } catch (Exception ex) { Logger.getLogger(ClockController.class.getName()).log(Level.SEVERE, null, ex); }` – asad sajjad Nov 08 '16 at 10:59
  • 1
    maybe in the catch block (when DB insert fails) you can write it into some temporary file and when your application starts again, it will read this file and insert the appropriate record into DB... but that's a really weird solution – Igor Nov 08 '16 at 11:02
  • Are you trying to say that I should create a whole new method within to check whether it handles the exception or not by storing the exeception case into a database ? right now it just stores login time when the application starts. – asad sajjad Nov 08 '16 at 11:08
  • no need to store the whole exception - just store the login time (or whatever you are trying to insert into DB): `try { cd.insert(Globals.globalClockBean); } catch (Exception ex) { //store Globals.globalClockBean into file }` – Igor Nov 08 '16 at 11:22
  • That's a nice idea, but instead of having two different flows on exit, I would just always put it into the file, and then move from file to DB on every startup. – Jaroslaw Pawlak Nov 08 '16 at 11:27
  • `catch (Exception ex) {Logger logger = Logger.getLogger("MyLog"); FileHandler fh; try { fh = new FileHandler("path"); logger.addHandler(fh); SimpleFormatter formatter = new SimpleFormatter(); fh.setFormatter(formatter); logger.info("My first log"); } catch (IOException | SecurityException ex1) {Logger.getLogger(ClockView.class.getName()).log(Level.SEVERE, null, ex1); }` will this work ? – asad sajjad Nov 08 '16 at 12:04
  • I still have a question that if I log my system off then it will remain the same it wont run this function so why would it go into exception where it will be terminated before the execution ? will it still save time in to the file ? – asad sajjad Nov 08 '16 at 12:08
  • after running this code on a console when I press `ctrl+c` it says "The media is write protected". – asad sajjad Nov 08 '16 at 12:14
  • it's not going in exception either ... can't find any file ... :/ – asad sajjad Nov 08 '16 at 12:28
1

When you simply log out from your computer the jvm is not shutted down, then the hook is not invoked.

user6904265
  • 1,938
  • 1
  • 16
  • 21