<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>
我想从java访问以上wspSqlSessionFactory
,以下是代码:
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();
}
}
我是否正确地进行了mybatis会话,或者我需要采用不同的方式。如果我正确地做了,即使我在上面的路径中有dispatcher-servlet仍然得到文件未找到异常。即使我刚试过"dispatcher-servlet.xml"
可能的解决方案:
@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();
}
}
虽然我获得了缓存,但只有在我删除
时才能清除它们Lock w = cache.getReadWriteLock().writeLock();
w.lock();
try {
cache.clear();
} finally {
w.unlock();
}
并直接执行cache.clear();
否则我得到Null指针异常:Lock w = cache.getReadWriteLock().writeLock();
为什么在该行的NPE是可以/建议不使用Lock。