[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]mybatis禁用一级缓存

上一篇:[备忘]springboot的testCase写法
下一篇:[备忘]netty里DefaultEventExecutorGroup的意思。

添加日期:2021/5/8 14:28:32 快速返回   返回列表 阅读817次
答案就是不能禁用,呵呵。
-------------------------------
一级缓存分为statement和session两种,默认是后者。
可以改成statement,这样语句执行完就会清空缓存。

(1)指定mybatis配置文件路径


<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation" value="classpath:mybatis-config.xml" />


springboot在配置文件这样写:
mybatis.config-locations=classpath:mybatis-config.xml

(2)配置文件


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration   
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="localCacheScope" value="STATEMENT" />
    </settings>

</configuration>



(3)发现可以这样:
select语句:
默认flushCache="false" useCache="true",表示缓存结果,不清空缓存

insert,update,delete:
默认flushCache="true" ,表示清空缓存

在mapper里
<select id="save" parameterType="XX" flushCache="true" useCache="false"> 
那么就不缓存这个select结果了,简单试了下,貌似有用。
 
========================================
一级缓存默认开启,session级别(需要开启事务,否则不好使),无法禁用。
<setting name="localCacheScope" value="SESSION" />
<setting name="localCacheScope" value="STATEMENT" />

二级缓存默认开启,但是mapper.xml里需要写<cache/>来激活。
<setting name="cacheEnabled" value="true"/>

配置参数:
https://mybatis.org/mybatis-3/configuration.html#settings
-------------------------------------------------
org.apache.ibatis.executor.BaseExecutor


public <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    ErrorContext.instance().resource(ms.getResource()).activity("executing a query").object(ms.getId());
    if (closed) {
      throw new ExecutorException("Executor was closed.");
    }
    if (queryStack == 0 && ms.isFlushCacheRequired()) {
      clearLocalCache();
    }
    List<E> list;
    try {
      queryStack++;
      list = resultHandler == null ? (List<E>) localCache.getObject(key) : null;
      if (list != null) {
        handleLocallyCachedOutputParameters(ms, key, parameter, boundSql);
      } else {
        list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
      }
    } finally {
      queryStack--;
    }
    if (queryStack == 0) {
      for (DeferredLoad deferredLoad : deferredLoads) {
        deferredLoad.load();
      }
      // issue #601
      deferredLoads.clear();
      if (configuration.getLocalCacheScope() == LocalCacheScope.STATEMENT) {
        // issue #482
        clearLocalCache();
      }
    }
    return list;
  }



  private <E> List<E> queryFromDatabase(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey key, BoundSql boundSql) throws SQLException {
    List<E> list;
    localCache.putObject(key, EXECUTION_PLACEHOLDER);
    try {
      list = doQuery(ms, parameter, rowBounds, resultHandler, boundSql);
    } finally {
      localCache.removeObject(key);
    }
    localCache.putObject(key, list);
    if (ms.getStatementType() == StatementType.CALLABLE) {
      localOutputParameterCache.putObject(key, parameter);
    }
    return list;
  }


开始,FlushCache为true会清缓存。
queryFromDatabase方法里面,会put进缓存。也不判断一下是否useCache,直接就放,有点彪啊。
LocalCacheScope为STATEMENT,会清缓存。
但是注意queryStack这个变量,为0时才清缓存。
并发查询,最后那个才清理缓存?中间就一直灌内存?
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved