【问题标题】:How to make SQLContainer auto refresher in vaadin [closed]如何在vaadin中使SQLContainer自动刷新[关闭]
【发布时间】:2014-12-15 12:04:37
【问题描述】:

我有一个 vaadin 应用程序,我使用 SQLContainer 来显示数据库记录。 我想让 SQLcontainer 在发生任何数据库更改时自行更新的问题。 有人帮帮我吗?

【问题讨论】:

  • 为什么要投反对票?当然,每次在他的数据库表中修改一行时,OP 都希望更新他的 vaadin 表。我还没有找到任何直接的解决方案。
  • 根据this page 刷新功能是内置的:The SQLContainer keeps continuously checking the amount of rows in the connected database table in order to detect external addition or removal of rows. By default, the table row count is assumed to remain valid for 10 seconds. This value can be altered from code; with setSizeValidMilliSeconds() in SQLContainer. 这个我还没有验证。我也不知道是否检测到更新。 重要提示您需要使用 Push 以使刷新出现在客户端,无需用户交互。

标签: vaadin vaadin7


【解决方案1】:

AFAIK 您不能在任何数据库系统(如 MySQL、Oracle 或 MSSQL)中注册“监听器”。这不是数据库的预期和架构工作方式。

解决方法是在 Vaadin 中注册 JavaScript 回调并每 x 秒更新一次表。这样,您甚至不需要启用 PushMode - 它仍然是客户端向服务器发出请求,而不是其他(肮脏的 imo)方式。

以下示例在 MySQL 和 Vaadin 7.3 上进行了测试:

    final VerticalLayout layout = new VerticalLayout();
    setContent(layout);
    final Table table =  new Table("Table");
    try
    {       
        JDBCConnectionPool connectionPool = new SimpleJDBCConnectionPool("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/test", "root", "");
        QueryDelegate qd = new FreeformQuery("select * from testTable", connectionPool, "id");
        final SQLContainer c = new SQLContainer(qd);
        table.setContainerDataSource(c);           
    }
    catch (SQLException e)
    {
        e.printStackTrace();
    }
    layout.addComponent(table);
    JavaScript.getCurrent().execute("setInterval(function(){refreshTable();},5000);");
    JavaScript.getCurrent().addFunction("refreshTable", new JavaScriptFunction()
    {
        @Override
        public void call(JSONArray arguments) throws JSONException
        {
            table.refreshRowCache();
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 2015-12-06
    • 2014-09-12
    • 2014-02-05
    • 2017-10-27
    • 2017-02-17
    相关资源
    最近更新 更多