spring 整合mybatis后用不上session緩存的原因分析
因?yàn)橐恢庇胹pring整合了mybatis,所以很少用到mybatis的session緩存。 習(xí)慣是本地緩存自己用map寫或者引入第三方的本地緩存框架ehcache,Guava
所以提出來糾結(jié)下
實(shí)驗(yàn)下(spring整合mybatis略,網(wǎng)上一堆),先看看mybatis級別的session的緩存
放出打印sql語句
configuration.xml 加入
<settings>
<!-- 打印查詢語句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
測試源代碼如下:
dao類
/**
* 測試spring里的mybatis為啥用不上緩存
*
* @author 何錦彬 2017.02.15
*/
@Component
public class TestDao {
private Logger logger = Logger.getLogger(TestDao.class.getName());
@Autowired
private SqlSessionTemplate sqlSessionTemplate;
@Autowired
private SqlSessionFactory sqlSessionFactory;
/**
* 兩次SQL
*
* @param id
* @return
*/
public TestDto selectBySpring(String id) {
TestDto testDto = (TestDto) sqlSessionTemplate.selectOne("com.hejb.TestDto.selectByPrimaryKey", id);
testDto = (TestDto) sqlSessionTemplate.selectOne("com.hejb.TestDto.selectByPrimaryKey", id);
return testDto;
}
/**
* 一次SQL
*
* @param id
* @return
*/
public TestDto selectByMybatis(String id) {
SqlSession session = sqlSessionFactory.openSession();
TestDto testDto = session.selectOne("com.hejb.TestDto.selectByPrimaryKey", id);
testDto = session.selectOne("com.hejb.TestDto.selectByPrimaryKey", id);
return testDto;
}
}
測試service類
@Component
public class TestService {
@Autowired
private TestDao testDao;
/**
* 未開啟事務(wù)的spring Mybatis查詢
*/
public void testSpringCashe() {
//查詢了兩次SQL
testDao.selectBySpring("1");
}
/**
* 開啟事務(wù)的spring Mybatis查詢
*/
@Transactional
public void testSpringCasheWithTran() {
//spring開啟事務(wù)后,查詢1次SQL
testDao.selectBySpring("1");
}
/**
* mybatis查詢
*/
public void testCash4Mybatise() {
//原生態(tài)mybatis,查詢了1次SQL
testDao.selectByMybatis("1");
}
}
輸出結(jié)果:
testSpringCashe()方法執(zhí)行了兩次SQL, 其它都是一次
源碼追蹤:
先看mybatis里的sqlSession
跟蹤到最后 調(diào)用到 org.apache.ibatis.executor.BaseExecutor的query方法
try {
queryStack++;
list = resultHandler == null ? (List<E>) localCache.getObject(key) : null; //先從緩存中取
if (list != null) {
handleLocallyCachedOutputParameters(ms, key, parameter, boundSql); //注意里面的key是CacheKey
} else {
list = queryFromDatabase(ms, parameter, rowBounds, resultHandler, key, boundSql);
}
貼下是怎么取出緩存數(shù)據(jù)的代碼
private void handleLocallyCachedOutputParameters(MappedStatement ms, CacheKey key, Object parameter, BoundSql boundSql) {
if (ms.getStatementType() == StatementType.CALLABLE) {
final Object cachedParameter = localOutputParameterCache.getObject(key);//從localOutputParameterCache取出緩存對象
if (cachedParameter != null && parameter != null) {
final MetaObject metaCachedParameter = configuration.newMetaObject(cachedParameter);
final MetaObject metaParameter = configuration.newMetaObject(parameter);
for (ParameterMapping parameterMapping : boundSql.getParameterMappings()) {
if (parameterMapping.getMode() != ParameterMode.IN) {
final String parameterName = parameterMapping.getProperty();
final Object cachedValue = metaCachedParameter.getValue(parameterName);
metaParameter.setValue(parameterName, cachedValue);
}
}
}
}
}
發(fā)現(xiàn)就是從localOutputParameterCache就是一個(gè)PerpetualCache, PerpetualCache維護(hù)了個(gè)map,就是session的緩存本質(zhì)了。
重點(diǎn)可以關(guān)注下面兩個(gè)累的邏輯
PerpetualCache , 兩個(gè)參數(shù), id和map
CacheKey,map中存的key,它有覆蓋equas方法,當(dāng)獲取緩存時(shí)調(diào)用.
這種本地map緩存獲取對象的缺點(diǎn),就我踩坑經(jīng)驗(yàn)(以前我也用map去實(shí)現(xiàn)的本地緩存),就是獲取的對象非clone的,返回的兩個(gè)對象都是一個(gè)地址
而在spring中一般都是用sqlSessionTemplate,如下
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:configuration.xml" />
<property name="mapperLocations">
<list>
<value>classpath*:com/hejb/sqlmap/*.xml</value>
</list>
</property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="sqlSessionFactory" />
</bean>
在SqlSessionTemplate中執(zhí)行SQL的session都是通過sqlSessionProxy來,sqlSessionProxy的生成在構(gòu)造函數(shù)中賦值,如下:
this.sqlSessionProxy = (SqlSession) newProxyInstance(
SqlSessionFactory.class.getClassLoader(),
new Class[] { SqlSession.class },
new SqlSessionInterceptor());
sqlSessionProxy通過JDK的動(dòng)態(tài)代理方法生成的一個(gè)代理類,主要邏輯在InvocationHandler對執(zhí)行的方法進(jìn)行了前后攔截,主要邏輯在invoke中,包好了每次執(zhí)行對sqlsesstion的創(chuàng)建,common,關(guān)閉
代碼如下:
private class SqlSessionInterceptor implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 每次執(zhí)行前都創(chuàng)建一個(gè)新的sqlSession
SqlSession sqlSession = getSqlSession(
SqlSessionTemplate.this.sqlSessionFactory,
SqlSessionTemplate.this.executorType,
SqlSessionTemplate.this.exceptionTranslator);
try {
// 執(zhí)行方法
Object result = method.invoke(sqlSession, args);
if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {
// force commit even on non-dirty sessions because some databases require
// a commit/rollback before calling close()
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 {
if (sqlSession != null) {
closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);
}
}
}
}
因?yàn)槊看味歼M(jìn)行創(chuàng)建,所以就用不上sqlSession的緩存了.
對于開啟了事務(wù)為什么可以用上呢, 跟入getSqlSession方法
如下:
public static SqlSession getSqlSession(SqlSessionFactory sessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) {
notNull(sessionFactory, NO_SQL_SESSION_FACTORY_SPECIFIED);
notNull(executorType, NO_EXECUTOR_TYPE_SPECIFIED);
SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory);
// 首先從SqlSessionHolder里取出session
SqlSession session = sessionHolder(executorType, holder);
if (session != null) {
return session;
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Creating a new SqlSession");
}
session = sessionFactory.openSession(executorType);
registerSessionHolder(sessionFactory, executorType, exceptionTranslator, session);
return session;
}
在里面維護(hù)了個(gè)SqlSessionHolder,關(guān)聯(lián)了事務(wù)與session,如果存在則直接取出,否則則新建個(gè)session,所以在有事務(wù)的里,每個(gè)session都是同一個(gè),故能用上緩存了
以上所述是小編給大家介紹的spring 整合mybatis后用不上session緩存的原因分析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
springboot集成普羅米修斯(Prometheus)的方法
這篇文章主要介紹了springboot集成普羅米修斯(Prometheus)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Java使用Jasypt進(jìn)行加密和解密的技術(shù)指南
Jasypt (Java Simplified Encryption) 是一個(gè)簡化 Java 應(yīng)用中加密工作的庫,它支持加密和解密操作,易于與 Spring Boot 集成,通過 Jasypt,可以安全地管理敏感信息,比如數(shù)據(jù)庫密碼、API 密鑰等,本文介紹了Java使用Jasypt進(jìn)行加密和解密的技術(shù)指南,需要的朋友可以參考下2025-03-03
Struts1和struts2的區(qū)別_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了Struts1和struts2的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
如何將JSP/Servlet項(xiàng)目轉(zhuǎn)換為Spring Boot項(xiàng)目
這篇文章主要介紹了如何將JSP/Servlet項(xiàng)目轉(zhuǎn)換為Spring Boot項(xiàng)目,幫助大家更好的利用springboot進(jìn)行網(wǎng)絡(luò)編程,感興趣的朋友可以了解下2020-10-10
Nacos服務(wù)實(shí)例的權(quán)重設(shè)置方式(以及設(shè)置為0時(shí)的作用與場景)
這篇文章主要介紹了Nacos服務(wù)實(shí)例的權(quán)重設(shè)置方式(以及設(shè)置為0時(shí)的作用與場景),具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
java中javamail收發(fā)郵件實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了java中javamail收發(fā)郵件實(shí)現(xiàn)方法,實(shí)例分析了javamail的使用方法與相關(guān)注意事項(xiàng),非常具有實(shí)用價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02

