1
        <bean id="wspSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="wspdataSource" />
        <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>
        </bean>

        <bean id="wspDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="wspSqlSessionFactory"/> 
        <property name="mapperInterface" value="com.lodige.clcs.dao.WSPDao" />
        </bean> 

I wanted to access above wspSqlSessionFactory from java and below is the code:

      ApplicationContext ctx = new ClassPathXmlApplicationContext("/WEB-INF/dispatcher-servlet.xml");
           Object myBean = (SqlSessionFactory) ctx.getBean("dashbSqlSessionFactory");
           Configuration configuration = ((SqlSessionFactory) myBean).getConfiguration(); 
           Collection<Cache> caches = configuration.getCaches(); 
//Cache cache = configuration.getCache("src.main.domain.SysKeyEQ"); // namespace of particular XML
            //cache.clear();
           //If you have multiple caches and want a particular to get deleted.
            for (Cache cache : caches) { 
                System.out.println("cache Name:   "+caches);
                Lock w = cache.getReadWriteLock().writeLock(); 
                w.lock(); 
                try { 
                    //cache.clear(); 
                } finally { 
                    w.unlock(); 
                } 
       }

Am I doing it correctly to get the mybatis session or I need to it differently. And if I am doing correctly even if I have dispatcher-servlet in the above path still I get file not found exception. Even I just tried "dispatcher-servlet.xml"

Possible Solution:

          @Autowired
          @Qualifier("wspSqlSessionFactory")
          private SqlSessionFactoryBean  wspSqlSessionFactory;
           Configuration configuration = wspSqlSessionFactory.getObject().getConfiguration(); 
           Collection<Cache> caches = configuration.getCaches(); 
            for (Cache cache : caches) { 
                System.out.println("cache Name:   "+cache);
                Lock w = cache.getReadWriteLock().writeLock(); 
                w.lock(); 
                try { 
                    cache.clear(); 
                } finally { 
                    w.unlock(); 
                }
       }

Though I get caches and can clear them only if I remove

Lock w = cache.getReadWriteLock().writeLock(); 
   w.lock(); 
   try { 
   cache.clear(); 
   } finally { 
   w.unlock(); 
   }

and directly do cache.clear(); else I get Null pointer exception in line: Lock w = cache.getReadWriteLock().writeLock(); why is it NPE at that line is it okay/recommendable to not use Lock.

4

0 に答える 0