日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

解決spring結合mybatis時一級緩存失效的問題

瀏覽:52日期:2023-07-30 17:03:04

之前了解到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); }}

測試案例:

@Testpublic 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時一級緩存失效的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
日韩视频不卡| 另类综合日韩欧美亚洲| 麻豆成全视频免费观看在线看| 欧美一级网址| 国产精品a级| 久久av综合| 美女免费视频一区| 麻豆精品久久| 久久精品理论片| 国产成人精品亚洲线观看| 久久这里只有| av中文字幕在线观看第一页| 国产亚洲人成a在线v网站| 国产精品magnet| 国产成人免费视频网站视频社区| 动漫av一区| 久久免费国产| 中文亚洲免费| 日韩欧美在线精品| 久久久国产精品入口麻豆| 精品国产乱码久久久| 肉色欧美久久久久久久免费看| 久久精品卡一| 影音国产精品| 日本在线不卡视频| 久久一区国产| 99精品电影| 午夜国产一区二区| 综合激情视频| 国产精品66| 99成人在线视频| 亚洲精选成人| 精品中文字幕一区二区三区 | 免费的成人av| 国产精品久久久久久av公交车| 美女av在线免费看| 日韩天堂av| 国产欧美综合一区二区三区| 日本午夜大片a在线观看| 日韩中文字幕麻豆| 精品日韩在线| 999国产精品视频| 日韩亚洲精品在线观看| 久久精品免费看| 精品91久久久久| 欧美日韩亚洲一区三区| 欧洲av不卡| 亚洲日韩中文字幕一区| 久久精品三级| 久久国产高清| 激情久久99| 免费观看在线色综合| 国产一区精品福利| 蜜桃免费网站一区二区三区| 久久精品国产网站| 三级一区在线视频先锋| 国产欧美日韩一区二区三区在线| 久久视频一区| 国产精成人品2018| 国产一区白浆| 国产一区二区三区不卡av| 欧美专区18| 精品三级久久久| 在线综合亚洲| 国产va免费精品观看精品视频| 国产精品毛片在线| 国产成人免费| 亚洲精品三级| 久久男女视频| 国产精品日本一区二区三区在线 | 日韩中文一区二区| 日韩免费看片| 欧美影院视频| 婷婷亚洲五月| 久久精品国产在热久久| 蜜桃视频一区二区三区在线观看| 国产传媒在线观看| 欧美片网站免费| 视频一区二区中文字幕| 久久久人人人| 国产一区二区三区黄网站| 日韩av网站在线免费观看| 99久久婷婷这里只有精品| 国产精品一级| 蜜桃视频一区二区三区在线观看| 日韩精品免费一区二区在线观看 | 日本成人在线视频网站| 香蕉国产精品| 97se综合| 麻豆精品在线视频| 日韩美女精品| 视频一区视频二区在线观看| 日韩一区二区三区免费| 国产极品久久久久久久久波多结野| 首页欧美精品中文字幕| 欧美一区二区三区高清视频| 久久影院一区二区三区| 欧美日一区二区三区在线观看国产免| 国产午夜精品一区二区三区欧美| 亚洲精品**中文毛片| 久久影院资源站| 国产精品亚洲人成在99www| 色综合视频一区二区三区日韩 | 四虎在线精品| 在线日韩中文| 91亚洲人成网污www| 久久影视三级福利片| 国产精品久一| 91亚洲无吗| 视频一区日韩精品| 中文精品视频| 在线精品视频在线观看高清| 欧美69视频| 欧美香蕉视频| 日韩免费在线| 成人日韩在线| 欧美成a人国产精品高清乱码在线观看片在线观看久 | 麻豆精品蜜桃视频网站| 日本成人在线一区| 亚洲三级观看| 中文字幕一区二区三区日韩精品 | 亚洲成人免费| 亚洲精品网址| 9久re热视频在线精品| 国产精品视区| 亚洲综合精品四区| 美女国产精品| 日韩三级精品| 久久狠狠久久| 国产精品蜜月aⅴ在线| 久久不见久久见国语| 毛片不卡一区二区| 精品视频自拍| 色网在线免费观看| 视频二区不卡| 国内激情久久| 久久av一区| 综合亚洲自拍| 国产欧美日韩一区二区三区四区| 国产精品115| 国产不卡精品| 另类中文字幕国产精品| 尹人成人综合网| 在线免费观看亚洲| 视频一区中文字幕精品| 国产女人18毛片水真多18精品| 国产精品亚洲二区| 国产精品s色| 97国产成人高清在线观看| 美女网站视频一区| 国产偷自视频区视频一区二区| 亚洲精品九九| 麻豆精品新av中文字幕| 秋霞影院一区二区三区| aa国产精品| 日韩欧美中文字幕电影| 麻豆成人av在线| 成人羞羞视频播放网站| 国产精品试看| 国产欧美自拍一区| 国产不卡一区| 亚洲视频www| 国产日韩欧美三级| 久久婷婷av| 免费久久精品视频| 国产精品久久久久av蜜臀| 亚洲天堂一区二区| 亚洲图片久久| 国产一区二区三区91| 亚洲少妇在线| 久久国产三级| 欧美亚洲日本精品| 美国三级日本三级久久99| 久久精品一区二区国产| 午夜精品网站| 国产精品久久久久久妇女| 日韩精品一卡| 日本aⅴ精品一区二区三区 | 欧美特黄一区| 国产免费av一区二区三区| 久久精品青草| 午夜精品影视国产一区在线麻豆| 精品91福利视频| 国产精品毛片在线看| 国产精品xxx| 99成人在线| 精品福利久久久| 视频在线在亚洲| 丰满少妇一区| 在线观看视频免费一区二区三区| 成人国产精品一区二区免费麻豆| 亚洲尤物在线| 精品久久国产一区| 中文字幕成人| 久久久久网站| 国产日韩欧美一区二区三区在线观看| 99精品电影| 国产黄色精品| 欧美特黄一级| 欧美国产中文高清|