解决spring结合mybatis时一级缓存失效的问题

之前了解到mybatis的一级缓存是默认开启的,作用域是sqlSession,是基 HashMap的本地缓存。不同的SqlSession之间的缓存数据区域互不影响。

当进行select、update、delete操作后并且commit事物到数据库之后,sqlSession中的Cache自动被清空

<setting name="localCacheScope" value="SESSION"/>

结论

spring结合mybatis后,一级缓存作用:

在未开启事物的情况之下,每次查询,spring都会关闭旧的sqlSession而创建新的sqlSession,因此此时的一级缓存是没有启作用的

在开启事物的情况之下,spring使用threadLocal获取当前资源绑定同一个sqlSession,因此此时一级缓存是有效的

案例

情景一:未开启事物

@Service("countryService")
public class CountryService {

 @Autowired
 private CountryDao countryDao;

 // @Transactional 未开启事物
 public void noTranSactionMethod() throws JsonProcessingException {
  CountryDo countryDo = countryDao.getById(1L);
  CountryDo countryDo1 = countryDao.getById(1L);
  ObjectMapper objectMapper = new ObjectMapper();
  String json = objectMapper.writeValueAsString(countryDo);
  String json1 = objectMapper.writeValueAsString(countryDo1);
  System.out.println(json);
  System.out.println(json1);
 }
}

测试案例:

@Test
public void transactionTest() throws JsonProcessingException {
 countryService.noTranSactionMethod();
}

结果:

[DEBUG] SqlSessionUtils Creating a new SqlSession
[DEBUG] SpringManagedTransaction JDBC Connection [com.mysql.jdbc.JDBC4Connection@14a54ef6] will not be managed by Spring
[DEBUG] getById ==> Preparing: SELECT * FROM country WHERE country_id = ?
[DEBUG] getById ==> Parameters: 1(Long)
[DEBUG] getById <==  Total: 1
[DEBUG] SqlSessionUtils Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3359c978]
[DEBUG] SqlSessionUtils Creating a new SqlSession
[DEBUG] SqlSessionUtils SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2aa27288] was not registered for synchronization because synchronization is not active
[DEBUG] SpringManagedTransaction JDBC Connection [com.mysql.jdbc.JDBC4Connection@14a54ef6] will not be managed by Spring
[DEBUG] getById ==> Preparing: SELECT * FROM country WHERE country_id = ?
[DEBUG] getById ==> Parameters: 1(Long)
[DEBUG] getById <==  Total: 1
[DEBUG] SqlSessionUtils Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@2aa27288]
{"countryId":1,"country":"Afghanistan","lastUpdate":"2006-02-15 04:44:00.0"}
{"countryId":1,"country":"Afghanistan","lastUpdate":"2006-02-15 04:44:00.0"}

可以看到,两次查询,都创建了新的sqlSession,并向数据库查询,此时缓存并没有起效果

情景二: 开启事物

打开@Transactional注解:

@Service("countryService")
public class CountryService {

 @Autowired
 private CountryDao countryDao;

 @Transactional
 public void noTranSactionMethod() throws JsonProcessingException {
  CountryDo countryDo = countryDao.getById(1L);
  CountryDo countryDo1 = countryDao.getById(1L);
  ObjectMapper objectMapper = new ObjectMapper();
  String json = objectMapper.writeValueAsString(countryDo);
  String json1 = objectMapper.writeValueAsString(countryDo1);
  System.out.println(json);
  System.out.println(json1);
 }
}

使用原来的测试案例,输出结果:

[DEBUG] SqlSessionUtils Creating a new SqlSession
[DEBUG] SqlSessionUtils Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@109f5dd8]
[DEBUG] SpringManagedTransaction JDBC Connection [com.mysql.jdbc.JDBC4Connection@55caeb35] will be managed by Spring
[DEBUG] getById ==> Preparing: SELECT * FROM country WHERE country_id = ?
[DEBUG] getById ==> Parameters: 1(Long)
[DEBUG] getById <==  Total: 1
[DEBUG] SqlSessionUtils Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@109f5dd8]
// 从当前事物中获取sqlSession
[DEBUG] SqlSessionUtils Fetched SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@109f5dd8] from current transaction
[DEBUG] SqlSessionUtils Releasing transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@109f5dd8]
{"countryId":1,"country":"Afghanistan","lastUpdate":"2006-02-15 04:44:00.0"}
{"countryId":1,"country":"Afghanistan","lastUpdate":"2006-02-15 04:44:00.0"}

可以看到,两次查询,只创建了一次sqlSession,说明一级缓存起作用了

跟踪源码

从SqlSessionDaoSupport作为路口,这个类在mybatis-spring包下,sping为sqlSession做了代理

public abstract class SqlSessionDaoSupport extends DaoSupport {

 private SqlSession sqlSession;

 private boolean externalSqlSession;

 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
 if (!this.externalSqlSession) {
  this.sqlSession = new SqlSessionTemplate(sqlSessionFactory);
 }
 }
 //....omit
}

创建了SqlSessionTemplate后,在SqlSessionTemplate中:

public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType,
 PersistenceExceptionTranslator exceptionTranslator) {

 notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required");
 notNull(executorType, "Property 'executorType' is required");

 this.sqlSessionFactory = sqlSessionFactory;
 this.executorType = executorType;
 this.exceptionTranslator = exceptionTranslator;
 //代理了SqlSession
 this.sqlSessionProxy = (SqlSession) newProxyInstance(
  SqlSessionFactory.class.getClassLoader(),
  new Class[] { SqlSession.class },
  new SqlSessionInterceptor());
}

再看SqlSessionInterceptor,SqlSessionInterceptor是SqlSessionTemplate的内部类:

public class SqlSessionTemplate implements SqlSession, DisposableBean {
 // ...omit..
 private class SqlSessionInterceptor implements InvocationHandler {
  @Override
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  SqlSession sqlSession = getSqlSession(
   SqlSessionTemplate.this.sqlSessionFactory,
   SqlSessionTemplate.this.executorType,
   SqlSessionTemplate.this.exceptionTranslator);
  try {
   Object result = method.invoke(sqlSession, args);
   //如果尚未开启事物(事物不是由spring来管理),则sqlSession直接提交
   if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {
   // force commit even on non-dirty sessions because some databases require
   // a commit/rollback before calling close()
   // 手动commit
   sqlSession.commit(true);
   }
   return result;
  } catch (Throwable t) {
   Throwable unwrapped = unwrapThrowable(t);
   if (SqlSessionTemplate.this.exceptionTranslator != null && unwrapped instanceof PersistenceException) {
   // release the connection to avoid a deadlock if the translator is no loaded. See issue #22
   closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
   sqlSession = null;
   Throwable translated = SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException) unwrapped);
   if (translated != null) {
    unwrapped = translated;
   }
   }
   throw unwrapped;
  } finally {
   //一般情况下,默认都是关闭sqlSession
   if (sqlSession != null) {
   closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
   }
  }
  }
 }
}

再看getSqlSession方法,这个方法是在SqlSessionUtils.java中的:

public static SqlSession getSqlSession(SqlSessionFactory sessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) {

 notNull(sessionFactory, NO_SQL_SESSION_FACTORY_SPECIFIED);
 notNull(executorType, NO_EXECUTOR_TYPE_SPECIFIED);
 //获取holder
 SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
 //从sessionHolder中获取SqlSession
 SqlSession session = sessionHolder(executorType, holder);
 if (session != null) {
 return session;
 }

 if (LOGGER.isDebugEnabled()) {
 LOGGER.debug("Creating a new SqlSession");
 }

 //如果sqlSession不存在,则创建一个新的
 session = sessionFactory.openSession(executorType);
 //将sqlSession注册在sessionHolder中
 registerSessionHolder(sessionFactory, executorType, exceptionTranslator, session);

 return session;
}

private static void registerSessionHolder(SqlSessionFactory sessionFactory, ExecutorType executorType,
  PersistenceExceptionTranslator exceptionTranslator, SqlSession session) {
 SqlSessionHolder holder;
 //在开启事物的情况下
 if (TransactionSynchronizationManager.isSynchronizationActive()) {
  Environment environment = sessionFactory.getConfiguration().getEnvironment();

  //由spring来管理事物的情况下
  if (environment.getTransactionFactory() instanceof SpringManagedTransactionFactory) {
  if (LOGGER.isDebugEnabled()) {
   LOGGER.debug("Registering transaction synchronization for SqlSession [" + session + "]");
  }

  holder = new SqlSessionHolder(session, executorType, exceptionTranslator);
  //将sessionFactory绑定在sessionHolde相互绑定
  TransactionSynchronizationManager.bindResource(sessionFactory, holder);
  TransactionSynchronizationManager.registerSynchronization(new SqlSessionSynchronization(holder, sessionFactory));
  holder.setSynchronizedWithTransaction(true);
  holder.requested();
  } else {
  if (TransactionSynchronizationManager.getResource(environment.getDataSource()) == null) {
   if (LOGGER.isDebugEnabled()) {
   LOGGER.debug("SqlSession [" + session + "] was not registered for synchronization because DataSource is not transactional");
   }
  } else {
   throw new TransientDataAccessResourceException(
    "SqlSessionFactory must be using a SpringManagedTransactionFactory in order to use Spring transaction synchronization");
  }
  }
 } else {
  if (LOGGER.isDebugEnabled()) {
  LOGGER.debug("SqlSession [" + session + "] was not registered for synchronization because synchronization is not active");
  }
 }

再看TransactionSynchronizationManager.bindResource的方法:

public abstract class TransactionSynchronizationManager {

 //omit...
 private static final ThreadLocal<Map<Object, Object>> resources =
   new NamedThreadLocal<Map<Object, Object>>("Transactional resources");

  // key:sessionFactory, value:SqlSessionHolder(Connection)
  public static void bindResource(Object key, Object value) throws IllegalStateException {
  Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key);
  Assert.notNull(value, "Value must not be null");
  //从threadLocal类型的resources中获取与当前线程绑定的资源,如sessionFactory,Connection等等
  Map<Object, Object> map = resources.get();
  // set ThreadLocal Map if none found
  if (map == null) {
   map = new HashMap<Object, Object>();
   resources.set(map);
  }
  Object oldValue = map.put(actualKey, value);
  // Transparently suppress a ResourceHolder that was marked as void...
  if (oldValue instanceof ResourceHolder && ((ResourceHolder) oldValue).isVoid()) {
   oldValue = null;
  }
  if (oldValue != null) {
   throw new IllegalStateException("Already value [" + oldValue + "] for key [" +
    actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]");
  }
  if (logger.isTraceEnabled()) {
   logger.trace("Bound value [" + value + "] for key [" + actualKey + "] to thread [" +
    Thread.currentThread().getName() + "]");
  }
  }
}

这里可以看到,spring是如何做到获取到的是同一个SqlSession,前面的长篇大论,就是为使用ThreadLocal将当前线程绑定创建SqlSession相关的资源,从而获取同一个sqlSession

以上这篇解决spring结合mybatis时一级缓存失效的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • mybatis教程之查询缓存(一级缓存二级缓存和整合ehcache)

    1 缓存的意义 将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效率,解决了高并发系统的性能问题. 2 mybatis持久层缓存 mybatis提供一级缓存和二级缓存 mybatis一级缓存是一个SqlSession级别,sqlsession只能访问自己的一级缓存的数据,二级缓存是跨sqlSession,是mapper级别的缓存,对于mapper级别的缓存不同的sqlsession是可以共享的. 3 一级缓存 3.1 原

  • MyBatis一级缓存避坑完全指南

    一级缓存概念 当我们使用Mybatis进行数据库的操作时候,会创建一个SqlSession来进行一次数据库的会话,会话结束则关闭SqlSession对象.那么一个SqlSession的生命周期即对应于Mybatis的一次会话.在Mybatis的一次会话中,我们很有可能多次查询完全相同的sql语句,如果不采取措施的话,每一次查询都查询一次数据库.而一次会话时间一般都是极短的,相同Sql的查询结果极有可能完全相同.由于查询数据库代价是比较大的,这会导致系统的资源浪费. 为了解决这个问题,Mybati

  • 深入理解MyBatis中的一级缓存与二级缓存

    前言 先说缓存,合理使用缓存是优化中最常见的,将从数据库中查询出来的数据放入缓存中,下次使用时不必从数据库查询,而是直接从缓存中读取,避免频繁操作数据库,减轻数据库的压力,同时提高系统性能. 一级缓存 一级缓存是SqlSession级别的缓存.在操作数据库时需要构造sqlSession对象,在对象中有一个数据结构用于存储缓存数据.不同的sqlSession之间的缓存数据区域是互相不影响的.也就是他只能作用在同一个sqlSession中,不同的sqlSession中的缓存是互相不能读取的. 一级缓

  • MyBatis 延迟加载、一级缓存、二级缓存(详解)

    使用ORM框架我们更多的是使用其查询功能,那么查询海量数据则又离不开性能,那么这篇中我们就看下mybatis高级应用之延迟加载.一级缓存.二级缓存.使用时需要注意延迟加载必须使用resultMap,resultType不具有延迟加载功能. 一.延迟加载 延迟加载已经是老生常谈的问题,什么最大化利用数据库性能之类之类的,也懒的列举了,总是我一提到延迟加载脑子里就会想起来了Hibernate get和load的区别.OK,废话少说,直接看代码. 先来修改配置项xml. 注意,编写mybatis.xm

  • 深入理解Mybatis一级缓存

    客户端向数据库服务器发送同样的sql查询语句,如果每次都去访问数据库,会导致性能的降低. 那么怎么提高呢? mybatis为我们提供了一级缓存的策略 在一个sqlSession开启和关闭之间,sqlSession对象内部(其实是Executor)会维护一个缓存的对象,当查询数据时候,先从缓存中寻找是否存在该条数据,存在就直接取出来,不存在,向数据库发送sql查询, 然后将查询后的数据存入缓存,和返回给程序. 这样会存在一个问题: 如果在第一次和第二次查询期间,有程序更改了要查讯的数据库的数据,就

  • mybatis中的一级缓存深入剖析

    mybatis中提供有一级缓存 和 二级缓存,这里记录一下一级缓存 一级缓存(mybatis中默认开启) SqlSession级别的缓存,操作数据库时需要构造SQLSession对象, 在对象中有一个数据结构(HashMap)用于存储缓存数据,不同的SQLSession对象之间的缓存数据是不共享的,即独立的 根据第一点,简单一点讲就是一级缓存是属于对象的(个人记法) 从别处搞来一个图,便于我们理解: 下面用spring整合mybatis来测试一下mybatis的一级缓存: 1.下面是servic

  • 解决spring结合mybatis时一级缓存失效的问题

    之前了解到mybatis的一级缓存是默认开启的,作用域是sqlSession,是基 HashMap的本地缓存.不同的SqlSession之间的缓存数据区域互不影响. 当进行select.update.delete操作后并且commit事物到数据库之后,sqlSession中的Cache自动被清空 <setting name="localCacheScope" value="SESSION"/> 结论 spring结合mybatis后,一级缓存作用: 在未

  • 关于mybatis的一级缓存和二级缓存的那些事儿

    一.缓存是什么 缓存其实就是存储在内存中的临时数据,这里的数据量会比较小,一般来说,服务器的内存也是有限的,不可能将所有的数据都放到服务器的内存里面,所以, 只会把关键数据放到缓存中,缓存因为速度快,使用方便而出名! 二.为什么需要缓存 BS架构里面,用户的所有操作都是对数据库的增删改查,其中查询的操作是最多的,但如果用户想要某个数据时每次都去数据库查询,这无疑会增加数据库的压力,而且获取时间效率也会降低,所以为了解决这些问题,缓存应用而生,使用了缓存之后,服务器只需要查询一次数据库,然后将数据

  • Mybatis的一级缓存和二级缓存原理分析与使用

    目录 Mybatis的一级缓存和二级缓存 1 Mybatis如何判断两次查询是完全相同的查询 2 二级缓存 2.1 二级缓存配置 2.2 二级缓存特点 2.3 配置二级缓存 2.4 测试 Mybatis的一级缓存和二级缓存 Mybatis会将相同查询条件的SQL语句的查询结果存储在内存或者某种缓存介质中,当下次遇到相同的SQL时不执行该SQL,而是直接从缓存中获取结果,减少服务器的压力,尤其是在查询越多.缓存命中率越高的情况下,使用缓存对性能的提高更明显. Mybatis缓存分为一级缓存和二级缓

  • MyBatis关闭一级缓存的两种方式(分注解和xml两种方式)

    目录 问题:为什么有缓存 什么场景下必须需要关闭一级缓存 关闭一级缓存方法(针对使用MyBatis场景) 第一种:xml形式(关闭所有一级缓存) 第二种:注解形式(可指定仅仅某个Mapper关闭注解) 第三种:sql动态拼接传入的随机数 问题:为什么有缓存 mybatis默认开启一级缓存 什么场景下必须需要关闭一级缓存 场景:执行2次相同sql,但是第一次查询sql结果会加工处理,比如解析铭文,或者反编译加密解密用户名/密码字符串等等,如果不关闭一级缓存,等第二次再查询相同sql时不会去数据库表

  • Mybatis一级缓存和结合Spring Framework后失效的源码探究

    1.在下面的案例中,执行两次查询控制台只会输出一次 SQL 查询: mybatis-config.xml <?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"

  • Mybatis详细对比一级缓存与二级缓存

    目录 基本要点 1.缓存 2.一级缓存(默认开启,无法关闭) 3.二级缓存 4.缓存查询原理 基本要点 1.缓存 什么是缓存? 存在内存中的临时数据,我们可以把用户经常查询的数据存放到缓存中,当用户重复查询时,我们可以直接从缓存中查询,提高查询效率,可以解决高并发系统的性能问题 为什么使用缓存? 减少和数据库交互次数,减轻数据库的压力,提高系统效率 什么样的数据能使用缓存? 经常查询且不经常改变的数据 2.一级缓存(默认开启,无法关闭) 1)一级缓存的有效区间是sqlSession从创建到关闭的

  • Mybatis 一级缓存与二级缓存的实现

    mybatis缓存 mybatis作为一个流行的持久化工具,缓存必然是缺少不了的组件.通过这篇文章,就让我们来了解一下mybatis的缓存. mybatis缓存类型 说起mybatis的缓存,了解过的同学都知道,mybatis中可以有两种缓存类型: 第一种,我们通常称为以及缓存,或者sqlSession级别的缓存,这种缓存是mybatis自带的,如果mapper中的配置都是默认的话,那么一级缓存也是默认开启的. 第二种,就是非sqlSession级别的缓存了,我们通常称为二级缓存,mybatis

  • MyBatis关于二级缓存问题

    MyBatis提供一级缓存和二级缓存,其中一级缓存是sqlSession级别的缓存,不同的sqlSession之间的缓存互不影响.二级缓存是Mapper级别的缓存,多个sqlSession操作同一个Mapper,其二级缓存是可以共享的. MyBatis有多种二级缓存方案可供选择.其中对Memcached的支持较为成熟,现以Memcached为例介绍与spring项目的集成. 使用配置 配置pom.xml,添加依赖. <dependencies> ... <dependency> &

随机推荐