0
@Autowired
       @Qualifier("dashbSqlSessionFactory")
       private SqlSessionFactoryBean  dashbSqlSessionFactory;

@RequestMapping(value = "/clearcache", method = RequestMethod.POST)
        public @ResponseBody void clearCache() throws Exception{
            //System.out.println("Cache is cleared.................");
             Configuration configuration = dashbSqlSessionFactory.getObject().getConfiguration(); 
                Collection<Cache> caches = configuration.getCaches(); 
                 for (Cache cache : caches) { 
                  /*System.out.println("cache Name:   "+cache);
                  cache.removeObject("keyEquipmentShiftAutomatedModelData");*/
                     /*Lock w = cache.getReadWriteLock().writeLock(); 
                     w.lock(); 
                     try { */
                         cache.clear(); 
                     /*} finally { 
                         w.unlock(); 
                     }*/
            }
            }

In Mybatis mapper xml

<cache
    eviction="FIFO"
    size="512"
    readOnly="true"/>

As per above code I am expecting the cache to be at session level and unique to a single user but my cache is getting overridden when some other user logs into the system. How so as both have different sessions dashbSqlSessionFactory.getObject().getConfiguration(); Collection<Cache> caches = configuration.getCaches(); should not get cache of the user in other session as per my understanding. Is there any problem with my code

Srinivas Gadilli
  • 342
  • 6
  • 25
  • 1
    Well, since the `SqlSessionFactoryBean` already builds the Configuration, see `buildSqlSessionFactory()` and there seems to be only one instance of `SqlSessionFactory`, I would assume that the `Configuration` is shared among all `Cache`s. You probably could implement your own `Cache` class that somehow retrieves the current session and only operates on data for that... – Florian Schaetz Mar 04 '16 at 06:44
  • I think @FlorianSchaetz is on the right track. Also, keep in mind that @ Autowired creates a singleton. So, only one instance for SqlSessionFactoryBean. – Rafa Mar 06 '16 at 03:45
  • Interesting point, would be an idea to find out what happens if you change the scope of the `SqlSessionFactoryBean` to `session`, for example. But even if it works, it would probably be a really good way to slow down the whole application. – Florian Schaetz Mar 06 '16 at 08:06

0 Answers0