title: Lazy-loaded primefaces datatable gets updated before SQL statements finish executing
body: <p>I have a problem when the primefaces data table gets updated before the SQL statements for updating\removing entities from this table finish executing.</p>
  
  <pre><code>01:08:13,255 INFO [reg.data.model.LazyEventDataModelMod]  - Loading the lazy event data between 0 and 10 
  01:08:13,256 DEBUG [org.hibernate.SQL]  - 
      select
          count(*) as col_0_0_ 
      from
          events event0_ limit ? 
  01:08:13,394 DEBUG [org.hibernate.SQL]  - 
      delete 
      from
          events 
      where
          id=? 
  </code></pre>
  
  <p>The problem is that the entity manager doesn't return anything to indicate that it had successfully executed the delete\update query.</p>
  
  <p>I use JSF 2, Weld 1.1.5.Final, Primefaces 3.0.1, Seam persistence/transaction/solder 3.0.1.Final.<br/> 
  The server is Tomcat 7.</p>
  
  <p>Here is the code for the button that invokes the action on the controller bean:</p>
  
  <pre><code>&lt;p:commandButton id="deleteButton" value="#{msg.delete}"
      icon="ui-icon-trash" update=":events_form :messages"
      actionListener="#{eventController.delete()}" /&gt;
  
  &lt;p:commandButton id="refreshButton" value="#{msg.refresh}"
      icon="ui-icon-refresh" update=":events_form :messages" /&gt;
  </code></pre>
  
  <p>The update doesn't happen after the first button is pressed, however the second one does update the table, both of them have identical ajax update sections.</p>
  
  <p>Code of the delete() method:</p>
  
  <pre><code>public void delete() {
              if (getSelEvent() != null) {
                  log.debug("deleting event " + getSelEvent().getId());
  
                  dao.delete(getSelEvent());
  
      // Tried putting the code for reloading te data model here, but it is also executed before the sql statements...
  
              } else {
                  log.warn("delete action cannot proceed, no event selected!");
              }
  
          }
  </code></pre>
  
  <p>deleteById() method in the dao:</p>
  
  <pre><code>public void delete(T entity) {
          if (em == null)
              init();
          log.debugf("removing %s instance", simpleName);
  
          entity = em.merge(entity);
  
  // The problem lies here - em.remove() does not return any value to indicate success/failure, so the control is released before the SQL statements finish execution 
  
  em.remove(entity);
  }
  </code></pre>
  
  <p>data table:</p>
  
  <pre><code>&lt;p:dataTable id="event_list" value="#{eventController.lazyModel}"
                  var="event" rowKey="#{event.id}" paginator="true" rows="20"
                  selection="#{eventController.selEvent}" selectionMode="single"
                  onRowSelectUpdate="content,messages"
                  onRowEditUpdate="content,messages"
                  paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                  rowsPerPageTemplate="10,25,50,100"&gt;
  </code></pre>
  
  <p>LazyDataModel class:</p>
  
  <pre><code>public class LazyEventDataModel extends LazyDataModel&lt;Event&gt; {
  
  private static final long serialVersionUID = 2386224274507659474L;
  
  private EventDAO dao;
  
  private Logger log = Logger.getLogger(LazyEventDataModel.class);
  
  private List&lt;Event&gt; data;
  
  public LazyEventDataModel() {
      dao = new EventDAO();
  }
  
  @Override
  public Event getRowData(String rowKey) {
      return dao.find(Long.parseLong(rowKey));
  }
  
  @Override
  public Object getRowKey(Event event) {
      return event.getId();
  }
  
  @Override
  public List&lt;Event&gt; load(int first, int pageSize, String sortField,
          SortOrder sortOrder, Map&lt;String, String&gt; filters) {
  
      if (sortField == null || "".equals(sortField)) {
          sortField = "event_date";
          sortOrder = SortOrder.DESCENDING;
      }
  
      if (sortField.equals("date"))
          sortField = "event_date";
  
      // Get the paginated and sorted data
      data = dao.findPageObjects(Event.class, first, pageSize, sortField,
              sortOrder, filters);
  
      log.infof("Loading the lazy event data between " + first + " and "
              + (first + pageSize));
  
      // Set the row count
      this.setRowCount(Global.safeLongToInt(dao.count()));
  
      return data;
  }
  }
  </code></pre>
  
  <p>The only solution I can think of right now would be adding the timer after calling the dao.delete(getSelEvent()) and refreshing the data model after that timer.</p>