如何使用java在给定的时间限制内在会话超时之前在数据库中插入数据? [英] how to insert data in database before session timeouts within given time limit using java?

查看:37
本文介绍了如何使用java在给定的时间限制内在会话超时之前在数据库中插入数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在描述符页面中提到会话超时

If I am mentioning the session timeout in descriptor page

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>

在终止会话之前,我想在数据库中进行插入操作.有没有插入的可能.我有一个想法,当用户登录页面时,我可以使用触发器插入数据 30 分钟.但是,如果其他人更改为 -1 或超过 30 分钟而没有注意到用户登录时触发器正在运行,我该怎么办?

Before terminating session I want to do insert operation in database. Is there any possibility for insertion. I have a thought that when user logins the page and I can use trigger to insert the data for 30 minutes. But if anyone else changes to -1 or more than 30 mins without noticing the trigger is running when user logins at that case what can I do for it?

推荐答案

每次客户端进行交互时,会话超时的剩余时间都会发生变化,因此将其设置为 30 分钟是错误的解决方案.

The remaining time for a session timeout changes every time the client makes interaction, so timing this for 30 minutes is a wrong solution.

改为使用 HttpSessionListener.每次创建或销毁(失效)HTTP 会话时,都会调用注册的 HttpSessionListener.这不取决于配置的会话超时.sessionDestroyed() 方法总是在会话失效和丢弃之前调用.

Instead use an HttpSessionListener. A registered HttpSessionListener is called every time an HTTP session is created or destoryed (invalidated). This does not depend on the configured session timeout. The sessionDestroyed() method is always called right before a session is invalidated and discarded.

public class MySessionListener implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {}

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        // Here session is destoryed.
        // You can insert data to your database here.
        // You can access the session like this:
        HttpSession sess = se.getSession();
    }
}

以下是如何在 web.xml 中注册 HttpSessionListener:

Here is how you can register your HttpSessionListener it in your web.xml:

<web-app>
    <listener>
        <listener-class>com.dummy.MySessionListener</listener-class>
    </listener>
</web-app>

这篇关于如何使用java在给定的时间限制内在会话超时之前在数据库中插入数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆