异常堆栈:
|
java.lang.IllegalStateException: Cannot complete cache update - current state (2) is not UPDATE_IN_PROGRESS at com.opensymphony.oscache.base.EntryUpdateState.completeUpdate(EntryUpdateState.java:105) at com.opensymphony.oscache.base.Cache.completeUpdate(Cache.java:762) at com.opensymphony.oscache.base.Cache.putInCache(Cache.java:619) at com.opensymphony.oscache.base.Cache.putInCache(Cache.java:580) at com.opensymphony.oscache.general.GeneralCacheAdministrator.putInCache(GeneralCacheAdministrator.java:249) at com.opensymphony.oscache.general.GeneralCacheAdministrator.putInCache(GeneralCacheAdministrator.java:259) |
1 String myKey = "myKey"; 2 String myValue; 3 int myRefreshPeriod = 1000; 4 try { 5 // Get from the cache 6 myValue = (String) admin.getFromCache(myKey, myRefreshPeriod); 7 } catch (NeedsRefreshException nre) { 8 try { 9 // Get the value (probably from the database) 10 myValue = "This is the content retrieved."; 11 // Store in the cache 12 admin.putInCache(myKey, myValue); 13 } catch (Exception ex) { 14 // We have the current content if we want fail-over. 15 myValue = (String) nre.getCacheContent(); 16 // It is essential that cancelUpdate is called if the 17 // cached content is not rebuilt 18 admin.cancelUpdate(myKey); 19 } 20 }
1 public OSCache get(String key,int myRefreshPeriod){ 2 try{ 3 return (OSCache)this.admin.getFromCache(key,myRefreshPeriod); 4 }catch(NeedsRefreshException ex){ 5 this.admin.cancelUpdate(key); 6 return null; 7 } 8 }
线程1 正在重建缓存;
线程2 读取缓存时得到 NRE 异常,主动 cancel update;
线程1 重建缓存完毕,却发现状态被改为了 UPDATE_CANCELLED ,与期望不符,于是抛出异常 java.lang.IllegalStateException 。
- /**
- * Updates the state to <code>UPDATE_COMPLETE</code>. This should <em>only</em>
- * be called by the thread that managed to get the update lock.
- * @return the counter value after the operation completed
- */
- public int completeUpdate() {
- if (state != UPDATE_IN_PROGRESS) {
- throw new IllegalStateException("Cannot complete cache update - current state (" + state + ") is not UPDATE_IN_PROGRESS");
- }
- state = UPDATE_COMPLETE;
- return decrementUsageCounter();
- }