0

I have the following SessionScoped bean: As you can see, I initialize some variables that will be needed later on for a page load.

@ManagedBean
@SessionScoped
/*
* CassandraBean is the backing bean for holding and managing information  that is specific to each user session.
*/
public class CassandraBean implements Serializable {

private static final long serialVersionUID = 3542953038465572524L; 
private ResultSet rs;
private HashMap<String, TInfo> tur; 
private ArrayList<ErrorInfo> errors;    
private ArrayList<String> selected; 
private String selectedErrCode="114";
private LocalDate endDate=LocalDate.now();
private LocalDate startDate=LocalDate.of(endDate.getYear()-4, endDate.getMonthValue(), endDate.getDayOfMonth());

private CassandraManager cm;

/*
 * Creates a new instance of this bean, connects to the server and loads up data
 * Also starts a timer to refresh the data every 10 minutes.
 */
public CassandraBean() {
    cm=new CassandraManager("192.168.129.136", "keyspacename");
    createData();
    selected=new ArrayList<String>();
    selected.add("B01");

    Timer timer = new Timer();

    //queries the DB every 10 mins
    timer.scheduleAtFixedRate(new TimerTask() {
          @Override
          public void run() {
              createData();
          }
        }, 10*60*1000, 10*60*1000);
}

After the constructor is called, everything is fine. All variables have value when checked in the debugger.

However when I navigate to a different page using a method in another bean, the variables selected and selectedErrCode lose their values. They are not set to null, but selectedErrCode becomes an empty string and selected becomes an empty ArrayList.

The problem with this is that I need those values to stay there because then the following method is called.

public ArrayList<ErrorInfo> getErrors(){ getErrorData(selected, selectedErrCode, startDate, endDate); return errors; }

This method is called by a datatable in my xhtml page to fill up a table. Once the page has loaded the user can query using an onpage input form and the selected, selectedErrCode, startDate and endDate will be taken from the input forms.

I am on JBoss 6.3 EAP if that helps.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
sceluk
  • 13
  • 2
  • First off, how does the `Timer` periodically refresh session scoped data on the client side? This will not happen unless the user of the application himself/herself reloads the page. In order to accomplish this, you will need techniques like polling/long polling. – Tiny Aug 13 '15 at 13:06
  • @Tiny Oh yeah, I also have a script on the page that forces a reload every 10 minutes. So it works. I know it's probably not the best way but it's not important for now. – sceluk Aug 13 '15 at 13:08
  • Then why is this `Timer` required on the server side? You have scheduled the timer's job in the constructor of a session scoped bean potentially causing the session scoped bean to be recreated every ten minutes. – Tiny Aug 13 '15 at 13:13
  • Food for read: http://stackoverflow.com/q/7499534 – BalusC Aug 13 '15 at 14:39

0 Answers0