iBatis使用OSCache作缓存与java.io.FileNotFoundException异常
时间:2011-01-17 隔叶黄莺
这种异常只会发生在 Windows 平台上,可以说是 iBatis 的一个 Bug,大约 iBatis 的开发测试人员都比较热衷于 Unix 族系的平台,而忽略了 Windows 下的兼容性测试。
出现 FileNotFoundException 异常的情形是这样的:Windows7 + iBatis2.3.0.677 + OSCache2.4.1。
iBatis 中配置使用 OSCache 缓存,在映射文件 Product.xml 中有如下声明片断:
1.<cacheModel type="OSCACHE" id="productCache">
2.<flushOnExecute statement="Product.insert"/>
3.<flushOnExecute statement="Product.delete"/>
4.</cacheModel>
5.
6.<select id="getById" cacheModel="productCache" parameterClass="int" resultClass="Product">
7.select id, name,description as desc1 from test_product where id = #value#
8.</select>
OSCache 的 oscache.properties 中指明用磁盘文件缓存数据,注意以下几个配置:
01.#不缓存到内存
02.cache.memory=false
03.
04.#缓存持久化实现类,磁盘持久化******
05.cache.persistence.class=com.opensymphony.oscache.plugins.diskpersistence.DiskPersistenceListener
06.
07.#缓存磁盘路径,以后注意观察该目录生成的内容
08.cache.path=e:\\OSCache\\cache
09.
10.#选择一种缓存算法,还可选 FIFOCache、UnlimitedCache
11.cache.algorithm=com.opensymphony.oscache.base.algorithm.LRUCache
Java 测试代码:
1.public static void main(String[] args) throws Exception{
2.Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
3.SqlMapClient sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
4.System.out.println(sqlMapClient.queryForObject("Product.getById",5));
5.
6.//后面同样的查询将不再查询数据库,而是直接使用前面执行放在缓存中的内容
7.System.out.println(sqlMapClient.queryForObject("Product.getById",5));
8.}
执行上面那段代码,你将会得到异常:
2009-8-19 10:45:52 com.opensymphony.oscache.base.algorithm.AbstractConcurrentReadCache persistStore
严重: [oscache] Exception persisting 1991729862|2162672802|5|Product.getById|62996507| select id, name,description as desc1 from test_product where id = ? |executeQueryForObject
com.opensymphony.oscache.base.persistence.CachePersistenceException: Unable to write ''e:\OSCache\cache\application\1991729862|2162672802|5|Product_31getById|62996507|_49_50_51_52_53_54_55_56_57select_64id,_68name,description_85as_88desc1_94_95from_100test_105product_113where_119id_122=_124_125_126_127_128_129_130|executeQueryForObject.cache'' in the cache.Exception: java.io.FileNotFoundException, Message: e:\OSCache\cache\application\1991729862|2162672802|5|Product_31getById|62996507|_49_50_51_52_53_54_55_56_57select_64id,_68name,description_85as_88desc1_94_95from_100test_105product_113where_119id_122=_124_125_126_127_128_129_130|executeQueryForObject.cache (文件名、目录名或卷标语法不正确。)
at com.opensymphony.oscache.plugins.diskpersistence.AbstractDiskPersistenceLis
|