Search code examples
restletpersistent

How to have a persistent array in RESTlets for multiple requests to access


I'm using Restlets for a REST architecture. The REST classes accesses a database and returns the results as JSON.

I have an instance where I only want to go to the database once and store the results in an arraylist so that every subsequent REST request can access its data.

Here is the code that initializes the process:

        if(myArray == null){
            System.out.println("retrieving my array");
        passports = getMyArray();
        }else{
            System.out.println("in memory");
        }

if this were to run ideally, "retrieving my array" would be called from the first REST request, the next request from a user would cause the "in memory" to print.

This doesn't happen and each request hits the "retrieving my array" condition. How can I accomplish this?


Solution

  • REST is per definition stateless, but you can always store your array in a static variable that you initialize the first time you need it.