Cómo construir una matriz cuando se inicia el servicio web Java REST

I have method in my web service which loads the same mySQL data into an array every time it is called. It's a fairly large amount of data and it would be much more efficient if this could be done once when the service is started, instead of for every single request.

I've searched around but can't see any examples of how to achieve this, is it possible and if so - how?

¡Gracias!

preguntado el 09 de marzo de 12 a las 16:03

1 Respuestas

You should do it something like

private static List<Items> data;

public static List<Items> getData() {
   if(data==null) {
        //get from db
   }
   return data;
}

this way you will only have one trip to the database to populate the data. Alternatively since you are using Jersey you could extend Application and populate you data that way when registering the classes

   <init-param>
       <param-name>javax.ws.rs.Application</param-name>
       <param-value>com.foo.MyApplication</param-value>
   </init-param> 

respondido 09 mar '12, 17:03

I had tried that in the path resource, but "data" was always null and it would reload - is there a specific place to do this? - user1178196

Ah, sorry, brainfart there - I'm actually using Jackson, not Jersey. Does the second option still apply? - user1178196

All good. Tried your first solution again and it worked this time - I must have done something wrong first time around. - user1178196

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.