diff --git a/src/main/java/org/icatproject/topcat/repository/CacheRepository.java b/src/main/java/org/icatproject/topcat/repository/CacheRepository.java index 71e6adec..1863a1e5 100644 --- a/src/main/java/org/icatproject/topcat/repository/CacheRepository.java +++ b/src/main/java/org/icatproject/topcat/repository/CacheRepository.java @@ -27,7 +27,7 @@ public class CacheRepository { @PersistenceContext(unitName = "topcat") EntityManager em; - private static final Logger logger = LoggerFactory.getLogger(ConfVarRepository.class); + private static final Logger logger = LoggerFactory.getLogger(CacheRepository.class); public Object get(String key, Long seconds){ Cache cache = getCache(key); @@ -80,27 +80,32 @@ public void remove(String key) { @Schedule(hour="*", minute="0") public void prune(){ - Properties properties = Properties.getInstance(); - Integer maxCacheSize = Integer.valueOf(properties.getProperty("maxCacheSize", "10000")); - - TypedQuery query = em.createQuery("select cache from Cache cache order by cache.lastAccessTime desc", Cache.class); - query.setMaxResults(maxCacheSize); - - List caches; - int page = 1; - while(true){ - query.setFirstResult(maxCacheSize * page); - caches = query.getResultList(); - if(caches.size() > 0){ - for(Cache cache : caches){ - em.remove(cache); + try { + Properties properties = Properties.getInstance(); + Integer maxCacheSize = Integer.valueOf(properties.getProperty("maxCacheSize", "10000")); + + TypedQuery query = em.createQuery("select cache from Cache cache order by cache.lastAccessTime desc", Cache.class); + query.setMaxResults(maxCacheSize); + + List caches; + int page = 1; + while(true){ + query.setFirstResult(maxCacheSize * page); + caches = query.getResultList(); + if(caches.size() > 0){ + for(Cache cache : caches){ + em.remove(cache); + } + } else { + break; } - } else { - break; } - } - em.flush(); + em.flush(); + } catch (RuntimeException e) { + // Catch exceptions to prevent the EJBTimerService from crashing + logger.error("Unhandled exception in prune()", e); + } } private Cache getCache(String key){