简体   繁体   English

使用Java Shutdown Hook无法将注销时间存储在COMPUTER'S LOGOUT上

[英]Can't be able to store logout time on COMPUTER'S LOGOUT using Java Shutdown Hook

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. 当用户注销或登录计算机时,我试图将时间存储到数据库中,但是当我注销计算机时,它不将当前时间存储到数据库中,如果我在控制台上按ctrl+c或从NETBEANS终止程序,它也存储注销时间。 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()));
        }

    }));

My guess is that when you log off, operating system shuts down the database before the JMV shutdown hook is called. 我的猜测是,当你注销,操作系统关闭数据库JMV关闭钩子被调用之前。 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 我的猜测是会有一些“连接失败”类型的异常

当您仅从计算机注销时,不会关闭jvm,则不会调用该挂钩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM