如何在vaadin中实现SQLContainer自动刷新

如何在vaadin中实现SQLContainer自动刷新,vaadin,vaadin7,Vaadin,Vaadin7,我有一个vaadin应用程序,我使用SQLContainer来显示数据库记录。 当数据库发生任何更改时,我希望使SQLcontainer自身更新。 有人能帮我吗?AFAIK你不能注册一个“监听器”来监听MySQL、Oracle或MSSQL等数据库系统中的更改。这不是数据库的目的和架构 解决方法是在Vaadin中注册JavaScript回调,并每x秒更新一次表。通过这种方式,您甚至不需要启用PushMode——仍然是客户端向服务器发出请求,而不是另一种方式(脏imo) 以下示例在MySQL和Vaa

我有一个vaadin应用程序,我使用SQLContainer来显示数据库记录。 当数据库发生任何更改时,我希望使SQLcontainer自身更新。
有人能帮我吗?

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();
        }
    });

恐怕你不能在任何数据库系统(如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();
        }
    });

为什么要投否决票?当然,OP希望在每次修改其数据库表中的行时更新其vaadin表。我还没有找到任何简单的解决方案。根据内置的刷新功能:
SQLContainer不断检查连接的数据库表中的行数,以检测外部添加或删除行。默认情况下,假定表行计数在10秒内保持有效。此值可以从代码中更改;在SQLContainer中使用setSizeValidMissels()。
我还没有验证这一点。我也不知道是否检测到更新。重要信息:您需要使用推送功能使刷新显示在客户端,而不需要用户交互。为什么要进行向下投票?当然,OP希望在每次修改其数据库表中的行时更新其vaadin表。我还没有找到任何简单的解决方案。根据内置的刷新功能:
SQLContainer不断检查连接的数据库表中的行数,以检测外部添加或删除行。默认情况下,假定表行计数在10秒内保持有效。此值可以从代码中更改;在SQLContainer中使用setSizeValidMissels()。
我还没有验证这一点。我也不知道是否检测到更新。重要信息:您需要使用推送功能,以便在没有用户交互的客户端上显示刷新。