关于二级缓存的commit
来源:2-1 MyBatis二级缓存-1
三金好同学
2019-10-31 23:18:18
@Test public void selectById2() throws Exception { SqlSession session = null; try { session = MyBatisUtils.openSession(); Goods g = session.selectOne("selectById", 740); System.out.println(g.hashCode()); } catch (Exception e) { throw e; } finally { MyBatisUtils.closeSession(session); } try { session = MyBatisUtils.openSession(); session.commit(); System.out.println("二级缓存============"); Goods g1 = session.selectOne("selectById", 740); System.out.println(g1.hashCode()); } catch (Exception e) { throw e; } finally { MyBatisUtils.closeSession(session); } }
<cache eviction="LRU" flushInterval="600000" size="512" readOnly="true"/>
[main] 23:11:22:963 DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter. [main] 23:11:22:978 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections. [main] 23:11:22:979 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections. [main] 23:11:22:979 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections. [main] 23:11:22:979 DEBUG o.a.i.d.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections. [main] 23:11:23:096 DEBUG goods - Cache Hit Ratio [goods]: 0.0 [main] 23:11:23:103 DEBUG o.a.i.t.jdbc.JdbcTransaction - Opening JDBC Connection [main] 23:11:24:817 DEBUG o.a.i.d.pooled.PooledDataSource - Created connection 1565740893. [main] 23:11:24:817 DEBUG o.a.i.t.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5d534f5d] [main] 23:11:24:822 DEBUG goods.selectById - ==> Preparing: select * from t_goods where goods_id = ? [main] 23:11:24:859 DEBUG goods.selectById - ==> Parameters: 740(Integer) [main] 23:11:24:895 DEBUG goods.selectById - <== Total: 1 1807648168 [main] 23:11:24:899 DEBUG o.a.i.t.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5d534f5d] [main] 23:11:24:900 DEBUG o.a.i.t.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5d534f5d] [main] 23:11:24:900 DEBUG o.a.i.d.pooled.PooledDataSource - Returned connection 1565740893 to pool. 开启二级缓存============ [main] 23:11:24:900 DEBUG goods - Cache Hit Ratio [goods]: 0.5 1807648168
我设置了session.commit()方法,为什么namespace没有清除缓存,
1回答
同学你好!
SqlSession执行更新操作(update、delete、insert)后并执行SqlSession.commit()时,不仅清空其自身的一级缓存(执行更新操作的结果),也清空二级缓存
但是在查询时,commit是不会清除二级缓存的,所以同学这里没有效果
由于查询操作其实并不会改变数据,因此对数据没有影响。所以我们通常会说commit之后会将二级缓存也清空。这里老师有一些疏忽,很抱歉给同学带来疑惑,还望同学见谅。这里的问题我们会反馈给相关工作人员~
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
相似问题