Alexev
Alexev

Reputation: 157

ApplicationScoped Managed bean atribute not being refreshedin datatable

Well, I have a OLD jsp system using JavaEE5

One of the screens shows the user a BIG datatable that is called from the database each view, because the tables is HUGE and they need all the data it takes 15 miutus to render

In order to solve the problem I choose to use an AplicationScoped ManagedBean that stores the list in a List myList and it is acceded for the backing bean of the user interface

So far, so good, it work.

When the application adds another ecord I aded a line in the procedure to add the new object to the list myList.add(Object)

When I debug the application the object is correctly put in the list (the list grows by one record)

But when I logoff and log in, or refresh the grid the list is not renewed.

Maybe is not the best solution (using an application scoped bean)

I am using JSF 2.2, Primefaces 6.0 and javaEE5

My aPPScoped bean (getters and setters not shown to save space)

@ManagedBean(eager=true)
@ApplicationScoped
public class AplicationBean {

   public static List<Object> myList = new ArrayList();

   @PostConstruct
   public void iniciar() {
        myList=  est.getALLDATA();
   }
}

The backing bean

@ManagedBean
@SessionScoped
public class jsfbean {

    private List<Object> myList= new ArrayList();

    @PostConstruct
    public void init(){
        this.myList =  AplicationBean.getALLDATA();
    }
}     

the grid

                     paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {Exporters}"                      
                     paginator="true"
                     rows="40"
                     >

After the Insert an object P y created and then I add that in the list as this:

System.out.println("size"+AplicationBean.getMylist().size()); //(size is 1000)
AplicationBean.getMyList().add(P);

System.out.println("NEW SIZE"+AplicationBean.getMyList().size());

Upvotes: 0

Views: 106

Answers (1)

jklee
jklee

Reputation: 2268

EJB and static attributes is a bad idea. I propose the following structure.

@Startup
@Singleton
public class ApplicationBean {

    public List<Object> myList = new ArrayList();

    @PostConstruct
    public void iniciar()
    {
        myList=  est.getALLDATA();
    }

    public void add(Object o){
        this.myList.add(o);
    }

}

@ManagedBean
@SessionScoped
public class SessionBean {

    @EJB
    ApplicationBean applicationBean;

    List<Object> list;

    // Cached for the session
    public List<Object> getObjectList(){
        if(list==null){
            list = applicationBean.getObjectList();
        }
        return list;
    }

    // Always fresh data
    public List<Object> getObjectListNotCached(){
        return applicationBean.getObjectList();
    }
    ...
}

The list attributes live as long as the session. But this design have also problems, by example, what is after you update the base list? And the solutions isn't thread safe.

Upvotes: 1

Related Questions