如何在vaadin中使SQLContainer自动刷新

时间:2014-12-15 12:04:38

标签: vaadin vaadin7

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

1 个答案:

答案 0 :(得分:5)

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