0

I have a problem where the getter of a datatable is called multiple times, and I see thats fine here Primefaces dataTable call method multiple times when click commandButton. why?

The problem is that is giving me a datatable with no results when there is actually results, because at somepoint it will be null, I did a workaround for this with a static variable but I don't know if this can give me a problem in the future (please read im gonna put you in context with the question). here is my code of the datatable:

<p:dialog id="dialogMenu" appendTo="@(body)" widgetVar="dialogMenuwv"
        closable="true" closeOnEscape="true">
        <h:form id="formDialogMenu">
            <p:dataTable value="#{homeView.data1}" var="plato" id="tablaMenu"
                resizableColumns="false" tableStyle="width:auto"
                emptyMessage="No hay platos disponibles" paginator="true" rows="5">
                <f:facet name="header">
                                           Platos
                                        </f:facet>


                <p:column filterBy="#{plato.activo}" sortBy="#{plato.activo}">
                    <f:facet name="header">
                        <h:outputText value="Activo" />
                    </f:facet>
                    <h:outputText value="#{plato.activo}" />
                </p:column>

                <p:column filterBy="#{plato.nombre}" sortBy="#{plato.nombre}">
                    <f:facet name="header">
                        <h:outputText value="Nombre" />
                    </f:facet>
                    <h:outputText value="#{plato.nombre}" />
                </p:column>

                <p:column filterBy="#{plato.precio}" sortBy="#{plato.precio}">
                    <f:facet name="header">
                        <h:outputText value="Precio" />
                    </f:facet>
                    <h:outputText value="#{plato.precio}" />
                </p:column>
                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Imagen" />
                    </f:facet>
                    <p:graphicImage alt="image" width="100%" height="50%"
                        value="#{plato.imagenPlato}" cache="false">

                    </p:graphicImage>

                </p:column>
            </p:dataTable>
        </h:form>
    </p:dialog>

As you can see the table is inside a dialog the one is opened when this button is clicked:

<p:commandButton value="Ver Menu" update="formDialogMenu:tablaMenu"
                actionListener="#{homeView.getData1()}"
                onclick="PF('dialogMenuwv').show()"></p:commandButton>

I had to add that actionListener and update to my button because I notice that leaving it only with the onlick to show the dialog wouldn't work and I followed advice from linked answer.

Finally here is my getter method as I expected it to work and it would work if it was only called when the marker was selected (Im working with gMap):

   public List<PlatoDTO> getData1() {

            try {
                if (data1 == null) {
// puestocomida isn't null when the marker is selected but since the getter is being called so many times, puestocomida will be null later and so data1 will return null.
                    data1 = businessDelegatorView.getDataPlatoPuesto(puestocomida.getPueId());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    return data1;
}

while debugging I notice that the getter is called multiple times and for some reason my puestocomida variable becomes null, I guess it is because no marker selected anymore, so an exception is throwed there and I get a null returned. So I decided to make a static variable that will store my puestocomida.getpueId() and so I modified the getter like this:

// I made this static to store the id I need for the getter
private static Integer pueId;

public List<PlatoDTO> getData1() {
    try {
            data1 = businessDelegatorView.getDataPlatoPuesto(pueId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data1;
}

and it is working as I needed it to work like this, but my question is, this variable static will be different or the same for each user that goes into the application? Because it varies depending on the marker that a user selects, I have a googleMap and depending on the marker that a user selects "puestodecomida" is set, and im storing its id on that static variable, this static only updates when a marker is selected.

   public void onMarkerSelect(OverlaySelectEvent event) {
            // Just making a Puestocomida object out of the selected marker
            marker = (Marker) event.getOverlay();
            Puestocomida puestocomida = (Puestocomida) marker.getData();
            this.puestocomida = puestocomida;
            try {
// Saving the puestocomida id in the static variable
                data1 = businessDelegatorView.getDataPlatoPuesto(puestocomida.getPueId());
                pueId = puestocomida.getPueId();
            } catch (Exception e1) {
                e1.printStackTrace();
            }

puestocomida is restaurant in english and it varies on the marker user selects. any advice is appreciated :)

BugsForBreakfast
  • 712
  • 10
  • 30
  • Getter modififation of `getDate1` is **bad**. Initual version was good. The null check is there for a reason: https://stackoverflow.com/questions/5765853/how-and-when-should-i-load-the-model-from-database-for-hdatatable. And you effectively answer your question. Static is bad in services with user specific data. Fix the original problem for which we need a [mcve] (you must know that by now) – Kukeltje Jul 29 '19 at 06:42
  • 1
    if data1 becomes null, you most likely have a scoping problem... – Kukeltje Jul 29 '19 at 06:56
  • @Kukeltje I should use ViewScoped right mate? The original problem is that after I chose a marker of my gMap, in that moment the marker isn't null, but after that it becomes null, and since the application is calling alot times the getter of data1, which needs the marker info, data1 also becomes null, I guess I should use then ViewScoped right mate? "Use ViewScoped for rich ajax-enabled dynamic views (ajaxbased validation, rendering, dialogs, etc)." – BugsForBreakfast Jul 29 '19 at 13:29
  • Instead of asking questions, post an [mcve]. data1 does not become null because of the number of times it is called. I have datatables where it is (lazyly, like in your first piece of code) called 1000 times without becoming null. – Kukeltje Jul 29 '19 at 13:34
  • @Kukeltje I will later mate the problem is im not in the pc with that project :s, but for now take a look at onMarkerSelect method, I instance the puestocomida variable there and it isn't null there, so neither the marker selected, and I succesfully get the data there, but while debugging I notice that for some reason marker will become null after everything was great, and after that data1 will try to get again and since puestocomida is null (cause marker null) then data1 will be null too, I will post the mcre later but for now can you tell if the viewscoped will solve it? – BugsForBreakfast Jul 29 '19 at 13:38
  • Sorry, no... [mcve].... I'm not spending time at guessing... (unless you want to hire me ;-) ) – Kukeltje Jul 29 '19 at 13:39
  • @Kukeltje I will post the example later mate, but I have another question, why is it bad with the static variable? it is working, the only fear I have with that and I was hoping you could clear is if for example one user1 is logged in, and uses that variable, and then another user2 logs in, will they have the same variable value or the user2 will have it like on "reset state" like when it is just run for the first time like user 1, can you clarify me that? – BugsForBreakfast Jul 29 '19 at 13:44
  • @Kukeltje I think it will be the same for all sessions if one of the session changes it, when I get to the pc with the project I will test ViewScoped and hopefully that will mantain marker not null.. thanks mate. – BugsForBreakfast Jul 29 '19 at 14:38
  • @Kukeltje mate why didn't you could just tell me that with static it was no problem? for this case atleast... I opened two sessions with two different users, and the fact that it is static didn't affect them, they visualize the restaurants menus according to the marker they did select so no problem mate, but anyways im gonna try with viewscope and tell you :) – BugsForBreakfast Jul 30 '19 at 00:41
  • @Kukeltje man this is so weird, guess what I already had my viewscoped annotation, so I was like lets try other annotations but first I wanna see the error again, so I let the getter like it was before doing the static workaround, launched server and now everything works fine... this is the parts of my job that I don't like, I don't like when something doesn't works and I don't undertand why but less when it works and I don't know why lmao – BugsForBreakfast Jul 30 '19 at 00:57
  • Using a static IS bad design. The problem might not manifest itself with just two users that are not accesing the page at the **same** time, and it might not with 10 but it will manifests it an a moment in time with either 100 or even with 2 users. Set a breakpoint right before the getData, have user 1 call the page, stop at the breakpoint, have user 2 call the pages, stops at the breakpoint and make it continue, now continue for user 1... Booommm problem. Static value with user 1 now has the value set by user 2. _**Don't use statics for user specific data... EVER**_ – Kukeltje Jul 30 '19 at 06:10
  • @Kukeltje Okay I will do the test, also mate I will leave it working like it is now, without the static, to avoid what you say of maybe 100 users triggering the error :) – BugsForBreakfast Jul 30 '19 at 13:13

0 Answers0