I had the same problem, I used the cache mechanism from GEA to solve the problem. Basically, the cache is a distributed HasMap
some code: create a map:
try { cache = CacheManager.getInstance().getCacheFactory().createCache( new ConcurrentHashMap<String, Category>()); } catch (CacheException e) { Logger .getLogger(TipsDAO.class.getName()) .severe( "unable to cretate cache using an internal ConcurrentHashMap"); cache = new ConcurrentHashMap<String, Category>(); }
For each pop code you read, you first check the card, if you find that I will return, if you did not find it, you read from the database and put it on the card before returning.
if (cache.containsKey(cat)) { return (Category) cache.get(cat); } try { Query query = entityManager .createQuery("SELECT FROM Category WHERE name = ?1"); query.setParameter(1, cat); Category temp = (Category) query.getSingleResult(); cache.put(cat, temp); return temp; } catch (Exception e) { LOG.severe(e.getMessage()); return null; }
For each write operation to the database, you also write to the card
cache.put(cat.getName(), cat);
Frank source share