¿Cómo configurar el almacenamiento en caché en el bean EJB donde estoy consultando la base de datos usando la plantilla de hibernación?

I am querying database from my EJB Bean, which is DAO, my query look's like:

public List findDirectories()
{
  allDirectories = getHibernateTemplate().find("from " + Directory.class.getName() +
        " d order by upper(d.name)";);
  return allDirectories;
}

I want to cache this results, how can i do that, is there an example which i can refer too. All i want to do is in my EBJ Bean, cache the resultset of above query so next time when page refreshes then i go and get results from the cache rather then getting from database.

Update: Am using older versions of EJB and so can use cool features of EJB3

preguntado el 30 de enero de 12 a las 19:01

Can you use Hibernate's second level cache? It also includes a query cache. -

I am working on using Hibernate cache, for time being, am just using static map so that it acts as cache... -

1 Respuestas

The general idea here is to use stateless session EJBs to cache and manage infrequently changed data. Update the EJB occasionally if data, against all expectations, changes.

Java EE 6 provides a slightly different technique, singleton beans: http://java.sun.com/developer/technicalArticles/JavaEE/JavaEE6Overview_Part3.html.

Respondido 28 Abr '13, 14:04

I am not using ejb3 and so not sure if i can use this approach. - Rachel

I haven't used "classic" EJBs for almost a decade now, but I am quite sure the approach with caching data in the stateless session EJBs is about as old as the EJB platform. - Olaf

i am using stateless session beans for the implementations but am not sure how caching would work here because am using hibernate query to get data back from database. - Rachel

@Rachel: I'm not sure what is your exact architecture. Is it anything like this: static.springsource.org/spring/docs/2.5.x/reference/ejb.html ? If it is, you should have enough facilities in the setSessionContext() method of a stateless session bean to run a Hibernate query. So, you can get all your values and store them in a member variable of a List type. You can either store objects that are detached from the Hibernate session, or just Java Strings that can be later used in UI or when building filtering criteria. Hope this helps. - Olaf

for time being, i have just created map and am storing information in map and am checking that map is not empty then get values from map or else get it from database and in that case application would take some time. - Rachel

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