-
Mybatis 结合ehcache5
刚开始看mybatis ,在结合缓存的时候,遇到个问题,按照官方文档上面配置的ehcache配置,相应的jar包也加入进来,但是无路如何都没有成功
数据每次都是从数据库里面读取的,命中要么为0 ,要么直接miss
不知道啥原因,谁帮忙看下
代码如下 先看debug 信息
2012-07-17 12:47:47,124 [main] DEBUG [net.sf.ehcache.store.MemoryStore] - Initialized net.sf.ehcache.store.MemoryStore for com.ztiny.mybatis.dao.impl
2012-07-17 12:47:47,126 [main] DEBUG [net.sf.ehcache.Cache] - Initialised cache: com.ztiny.mybatis.dao.impl
2012-07-17 12:47:47,189 [main] DEBUG [net.sf.ehcache.Cache] - com.test.mybatis.dao.implCache: com.ztiny.mybatis.dao.implMemoryStore miss for 139696085:3585427282:com.ztiny.mybatis.dao.impl.selectAll:0:2147483647:select name as username ,age from employee
2012-07-17 12:47:47,189 [main] DEBUG [net.sf.ehcache.Cache] - com.test.mybatis.dao.impl cache - Miss
2012-07-17 12:47:47,391 [main] DEBUG [com.test.mybatis.dao.impl.selectAll] - ooo Using Connection [com.mysql.jdbc.Connection@6abde0]
2012-07-17 12:47:47,392 [main] DEBUG [com.test.mybatis.dao.impl.selectAll] - ==> Preparing: select name as username ,age from employee
2012-07-17 12:47:47,419 [main] DEBUG [com.test.mybatis.dao.impl.selectAll] - ==> Parameters:
2012-07-17 12:47:47,666 [main] DEBUG [net.sf.ehcache.Cache] - com.test.mybatis.dao.implCache: com.test.mybatis.dao.implMemoryStore miss for 139696085:3585427282:com.ztiny.mybatis.dao.impl.selectAll:0:2147483647:select name as username ,age from employee
2012-07-17 12:47:47,666 [main] DEBUG [net.sf.ehcache.Cache] - com.test.mybatis.dao.impl cache - Miss
2012-07-17 12:47:47,674 [main] DEBUG [com.test.mybatis.dao.impl.selectAll] - ooo Using Connection [com.mysql.jdbc.Connection@9f45f4]
2012-07-17 12:47:47,674 [main] DEBUG [com.test.mybatis.dao.impl.selectAll] - ==> Preparing: select name as username ,age from employee
2012-07-17 12:47:47,674 [main] DEBUG [com.test.mybatis.dao.impl.selectAll] - ==> Parameters:
从这个debug信息可以看到,打开了两次数据库连接,cache 没起作用
下面看下我的ehcache。xml配置<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../bin/ehcache.xsd"> <defaultCache overflowToDisk="true" eternal="true"/> <diskStore path="f:/test/cache" /> </ehcache>
employee.xmlmapper namespace="com.test.mybatis.dao.impl"> <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> <select id="selectAll" resultType="com.test.entity.Employee"> select name as username ,age from employee </select>
测试代码public List<Employee> selectList() { return super.getSession(false).selectList("selectAll"); } public static void main(String args[]) throws Exception{ EmployeeDaoImpl emplDao = new EmployeeDaoImpl(); emplDao.selectList();//第一次 emplDao.selectList();//第二次,应该从缓存里面读取 }
mybatis 源码:BoundSql boundSql = ms.getBoundSql(parameterObject); //根据规则创建缓存对应的键,因为查询相同,所以第二次这个还是这个键,没错 CacheKey key = createCacheKey(ms, parameterObject, rowBounds, boundSql); //在执行查询 return query(ms, parameterObject, rowBounds, resultHandler, key, boundSql); public <E> List<E> query(MappedStatement ms, Object parameterObject, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException { Cache cache = ms.getCache(); if (cache != null) { flushCacheIfRequired(ms); if (ms.isUseCache() && resultHandler == null) { ensureNoOutParams(ms, key, parameterObject, boundSql); if (!dirty) { cache.getReadWriteLock().readLock().lock(); try { @SuppressWarnings("unchecked") List<E> cachedList = (List<E>) cache.getObject(key); if (cachedList != null) return cachedList; } finally { cache.getReadWriteLock().readLock().unlock(); } } List<E> list = delegate.<E> query(ms, parameterObject, rowBounds, resultHandler, key, boundSql); //因为第一次没有缓存,所以创建了缓存然后再把数据放入缓存里面,但是这里有个问题,这个缓存每次都是重新创建的,大家可以去debug 看看,不知是我配置错了,还是怎么的,反正进入到这个方法里面,每次都是空的,看下面代码 tcm.putObject(cache, key, list); // issue #578. Query must be not synchronized to prevent deadlocks return list; } } //这里老是空对象,每次都是从新创建 TransactionalCache txCache = transactionalCaches.get(cache); if (txCache == null) { txCache = new TransactionalCache(cache); transactionalCaches.put(cache, txCache); }
问题补充: ps:做个之前最好能在本地测下,成功了,用真实数据给我看下,因为网络上很多都是简单的配置,我不知道具体有没有成功,我这里是没成功,看坛子里面有个有为同学用memcached 测试成功了,
membercached 我没测试,但是这里的ehcache 就是不行 谢谢2012年7月18日 09:44
目前还没有答案
相关推荐
springMVC+MyBatis+Ehcache项目整合 里面有几个调用的例子 还解决一般行整合出现 MyBatis事物无法回滚问题 Ehcache 以注解的方式进行整合 项目架构一般 不喜欢别骂我 没打算收你们的积分 不要问我叫什么 大家都叫我...
实际使用shiro的时候大部分都是和spring等框架结合使用,主要就是配置web.xml将shiro的filter和spring容器bean的filter关联起来,生命周期由servlet容器来控制,然后配置shiro的spring的xml文件,其中主要配置shiro...
plus(快速操作Mybatis)、BeetlSQL(强大的ORM框架)、upload(本地文件上传和七牛云文件上传)、redis(缓存)、ehcache(缓存)、email(发送各种类型邮件)、task(基础定时任务)、quartz(动态管理定时任务)、xxl-job(分布式...
Enjoy(模板引擎),JdbcTemplate(通用JDBC操作数据库),JPA(强大的ORM框架),强大的ORM框架(强大的ORM框架),通用Mapper(快速操作Mybatis Mybatis),PageHelper(通用的Mybatis分页插件),mybatis-plus...
spring 实践学习案例,与其它组件结合如 mybatis、jpa、dubbo、redis、mongodb、memcached、kafka、rabbitmq、activemq、shiro等 #### 软件架构 基于Java 1.8 #### Spring 版本 - 5.0.8.RELEASE #### 模块说明 - ...
plus(快速操作Mybatis)、BeetlSQL(强大的ORM框架)、upload(本地文件上传和七牛云文件上传)、redis(缓存)、ehcache(缓存)、email(发送各种类型邮件)、task(基础定时任务)、quartz(动态管理定时任务)、xxl-job(分布式...
plus(`快速操作Mybatis`)、BeetlSQL(`强大的ORM框架`)、upload(`本地文件上传和七牛云文件上传`)、redis(`缓存`)、ehcache(`缓存`)、email(`发送各种类型邮件`)、task(`基础定时任务`)、quartz(`动态管理定时任务`)...
spring boot 实践学习案例,与其它组件结合如 mybatis、jpa、dubbo、redis、mongodb、memcached、kafka、rabbitmq、activemq、elasticsearch、security、shiro等 #### Spring Boot 版本 - 2.0.3.RELEASE #### 模块...
该博客系统综合运用了作者发表的《 Spring Boot入门》系列的文章引用的知识内容,初学者可以阅读文章以及结合该项目学习。 与作者一样,使用hexo但苦于没有后台管理工具(界面)管理文章的写作者。该博客系统模仿...
Boot知识点整理、工程实践,并结合工作案例进行深入 使用travis-ci持续集成 使用codecov进行代码覆盖率检查 学习案例以模块方式划分,每隔模块都是独立可执行项目,直接运行Application即可 分享平台 博客: Github...
JessMA Java MVC & REST应用开发框架(简称 JessMA)是一套功能完备的高性能Full-Stack Web应用开发框架,内置稳定高效的MVC基础架构和DAO框架(已内置Hibernate、MyBatis和JDBC支持),集成 Action拦截、Form Bean ...
│ 第24节:加入ehcache,把工程加入到Git.avi │ 第25节:实现前端的业务登录等功能.avi │ 第26节:测试并调整登录的业务功能.avi │ 第27节:实现index功能的开发.avi │ 第28节:Index、商品详细页和购物车.avi ...