關於緩存:
緩存是介於物理數據源與應用程序之間,是對數據庫中的數據復制一份臨時放在內存中的容器,
其作用是為了減少應用程序對物理數據源訪問的次數,從而提高了應用程序的運行性能。
Hibernate在進行讀取數據的時候,根據緩存機制在相應的緩存中查詢,如果在緩存中找到了需要的數據(我們把這稱做“緩存命 中"),
則就直接把命中的數據作為結果加以利用,避免了大量發送SQL語句到數據庫查詢的性能損耗。
一般我們在項目整合中 存在數據訪問的性能問題,研究了下緩存在各個階段的應用,以在5個方面進行緩存的設計:
1.最底層可以配置的是MySQL自帶的query cache,
2.mybatis的一級緩存,默認情況下都處於開啟狀態,只能使用自帶的PerpetualCache,無法配置第三方緩存
3.mybatis的二級緩存,可以配置開關狀態,默認使用自帶的PerpetualCache,但功能比較弱,能夠配置第三方緩存,
4.service層的緩存配置,結合spring,可以靈活進行選擇
5.hibernate的一級緩存,默認也是開啟狀態,session級別的,可以自由使用。但這里注意下hibernate獲取session的 方式。
6.hibernate的二級緩存需要引用 第三方的 插件,並在hibernate配置文件中進行相應的配置。
7.針對實際業務情況,直接緩存部分html頁面,直接返回給客戶端。
發現在整合的項目測試中被spring整合的mybatis和hibernate的一級緩存都失效了。
首先1.mybatis和hibernate的一級緩存生效的范圍是sqlsession,是為了在sqlsession沒有關閉時,業務需要重復查詢相同數據使用的。一旦sqlsession關閉,則由這個sqlsession緩存的數據將會被清空。
spring管理的mybatis
@Test public void TestBi(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BillService service=(BillService)context.getBean("billServiceImpl"); List<Bill> alls = service.findAlls(); System.out.println("-----------------------------------------------"); List<Bill> a = service.findAlls(); System.out.println(alls.size()); }
applicationContext.xml是我spring的配置文件,管理session
測試結果:
ogging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter. Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@144e54da] was not registered for synchronization because synchronization is not active JDBC Connection [285401532, URL=jdbc:mysql:///supermarket_ssh, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring ==> Preparing: select * from bill ==> Parameters: <== Columns: bid, bmoney, bname, createtime, paystate, supId, hscode, scount, sunit <== Row: 24, 150262, 獼猴桃山西, 2017-09-06, 1, 3, ssh-code, 1000, bdss <== Row: 26, 1500, 蘋果梨山西, 2017-08-25, 0, 1, ssh-code, 50, bdss <== Row: 27, 150262, 蘋果梨山西, 2017-08-25, 0, 1, ssh-code, 1000, bdss <== Row: 29, 2561, xx, 2017-08-25, 0, 1, ssh-code, 0, bdss <== Row: 30, 3642, aa, 2017-08-25, 0, 1, ssh-code, 1000, 56ds3fs <== Row: 31, 0, bb, 2017-08-25, 0, 3, ssh-code, 52, bdss <== Row: 33, 3335, aa, 2017-08-25, 0, 3, ssh-code, 1000, 56ds3fs <== Row: 41, 2000, 小炒肉, 2017-09-03, 1, 3, ssh-code, 100, bdqn <== Row: 42, 0, zahngyu, 2017-09-03, 1, 12, ssh-code, 100, bdqn <== Row: 44, 200, 共百搭, 2017-09-06, 1, 3, dsfas, 15, dfa <== Row: 45, 5000, 張雨張雨, 2017-09-06, 0, 14, ssh-supmarket, 50, bdqn <== Row: 49, 0, dfaf, 2017-09-09, 0, 1, s-code, 3, dfa <== Row: 50, 0, 宮保雞丁, 2017-09-10, 0, 1, gbjd-code, 2, bdqn <== Row: 52, 0, f'da, 2017-09-10, 1, 1, f'da, 5, f'da <== Row: 55, 0, hznagu, 2017-09-10, 1, 1, ssh-code, 25, bdqn <== Row: 56, 0, 宮保雞丁, 2017-09-10, 0, 1, ssj-code, 2, bdqn <== Row: 57, 0, dfa, 2017-09-10, 1, 1, dfa, 2, fda <== Row: 58, 0, fda, 2017-09-10, 0, 1, dfa, 25, dfa <== Row: 59, 0, dfa, 2017-09-10, 0, 1, fda, 2, dfa <== Total: 19 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@144e54da] 19 ----------------------------------------------- Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d68b796] was not registered for synchronization because synchronization is not active JDBC Connection [1441587130, URL=jdbc:mysql:///supermarket_ssh, UserName=root@localhost, MySQL Connector Java] will not be managed by Spring ==> Preparing: select * from bill ==> Parameters: <== Columns: bid, bmoney, bname, createtime, paystate, supId, hscode, scount, sunit <== Row: 24, 150262, 獼猴桃山西, 2017-09-06, 1, 3, ssh-code, 1000, bdss <== Row: 26, 1500, 蘋果梨山西, 2017-08-25, 0, 1, ssh-code, 50, bdss <== Row: 27, 150262, 蘋果梨山西, 2017-08-25, 0, 1, ssh-code, 1000, bdss <== Row: 29, 2561, xx, 2017-08-25, 0, 1, ssh-code, 0, bdss <== Row: 30, 3642, aa, 2017-08-25, 0, 1, ssh-code, 1000, 56ds3fs <== Row: 31, 0, bb, 2017-08-25, 0, 3, ssh-code, 52, bdss <== Row: 33, 3335, aa, 2017-08-25, 0, 3, ssh-code, 1000, 56ds3fs <== Row: 41, 2000, 小炒肉, 2017-09-03, 1, 3, ssh-code, 100, bdqn <== Row: 42, 0, zahngyu, 2017-09-03, 1, 12, ssh-code, 100, bdqn <== Row: 44, 200, 共百搭, 2017-09-06, 1, 3, dsfas, 15, dfa <== Row: 45, 5000, 張雨張雨, 2017-09-06, 0, 14, ssh-supmarket, 50, bdqn <== Row: 49, 0, dfaf, 2017-09-09, 0, 1, s-code, 3, dfa <== Row: 50, 0, 宮保雞丁, 2017-09-10, 0, 1, gbjd-code, 2, bdqn <== Row: 52, 0, f'da, 2017-09-10, 1, 1, f'da, 5, f'da <== Row: 55, 0, hznagu, 2017-09-10, 1, 1, ssh-code, 25, bdqn <== Row: 56, 0, 宮保雞丁, 2017-09-10, 0, 1, ssj-code, 2, bdqn <== Row: 57, 0, dfa, 2017-09-10, 1, 1, dfa, 2, fda <== Row: 58, 0, fda, 2017-09-10, 0, 1, dfa, 25, dfa <== Row: 59, 0, dfa, 2017-09-10, 0, 1, fda, 2, dfa <== Total: 19 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d68b796] [entity.Bill@6afd9125, entity.Bill@2d444b03, entity.Bill@5e98c92c, entity.Bill@bf9839, entity.Bill@22a2c691, entity.Bill@6c9a6b5a, entity.Bill@dfe196a, entity.Bill@25368553, entity.Bill@1e6b1f3b, entity.Bill@acf6a17, entity.Bill@373e118c, entity.Bill@4bf21aa9, entity.Bill@3b47433, entity.Bill@3c38c22b, entity.Bill@3253b3f0, entity.Bill@154a6b60, entity.Bill@48cf768b, entity.Bill@2c6fc505, entity.Bill@5defe20d]
測試結果有點多啊但明顯是沒有使用到mybatis的一級緩存。
如果我們使用mybatis自己管理的session來看下測試結果
@Test
public void TestBi(){
SqlSession session = MyBatisUtil.getSessionTwo();
BillDao mapper = session.getMapper(BillDao.class);
List<Bill> b1 = mapper.findAlls();
System.out.println(b1.size());
List<Bill> b2 = mapper.findAlls();
System.out.println(b2.size());
Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6d20ec1b] ==> Preparing: select * from bill ==> Parameters: <== Columns: bid, bmoney, bname, createtime, paystate, supId, hscode, scount, sunit <== Row: 24, 150262, 獼猴桃山西, 2017-09-06, 1, 3, ssh-code, 1000, bdss <== Row: 26, 1500, 蘋果梨山西, 2017-08-25, 0, 1, ssh-code, 50, bdss <== Row: 27, 150262, 蘋果梨山西, 2017-08-25, 0, 1, ssh-code, 1000, bdss <== Row: 29, 2561, xx, 2017-08-25, 0, 1, ssh-code, 0, bdss <== Row: 30, 3642, aa, 2017-08-25, 0, 1, ssh-code, 1000, 56ds3fs <== Row: 31, 0, bb, 2017-08-25, 0, 3, ssh-code, 52, bdss <== Row: 33, 3335, aa, 2017-08-25, 0, 3, ssh-code, 1000, 56ds3fs <== Row: 41, 2000, 小炒肉, 2017-09-03, 1, 3, ssh-code, 100, bdqn <== Row: 42, 0, zahngyu, 2017-09-03, 1, 12, ssh-code, 100, bdqn <== Row: 44, 200, 共百搭, 2017-09-06, 1, 3, dsfas, 15, dfa <== Row: 45, 5000, 張雨張雨, 2017-09-06, 0, 14, ssh-supmarket, 50, bdqn <== Row: 49, 0, dfaf, 2017-09-09, 0, 1, s-code, 3, dfa <== Row: 50, 0, 宮保雞丁, 2017-09-10, 0, 1, gbjd-code, 2, bdqn <== Row: 52, 0, f'da, 2017-09-10, 1, 1, f'da, 5, f'da <== Row: 55, 0, hznagu, 2017-09-10, 1, 1, ssh-code, 25, bdqn <== Row: 56, 0, 宮保雞丁, 2017-09-10, 0, 1, ssj-code, 2, bdqn <== Row: 57, 0, dfa, 2017-09-10, 1, 1, dfa, 2, fda <== Row: 58, 0, fda, 2017-09-10, 0, 1, dfa, 25, dfa <== Row: 59, 0, dfa, 2017-09-10, 0, 1, fda, 2, dfa <== Total: 19 19 19
很明顯,只訪問了一次數據庫,說明單獨使用mybatis的 一級緩存是 有效的。
再看看hibernate和 spring整和的情況下,測試hibernate的一級緩存:
@org.junit.Test public void TestOneCache(){ //被spring管理的hibernate 一級緩存失效 ApplicationContext context = new ClassPathXmlApplicationContext("spring-hiberanate.xml"); BillService service= (BillService)context.getBean("billServiceImpl"); List<Bill> allBills = service.findAllBills(); System.out.println(allBills.size()); System.out.println("---------------------------------"); List<Bill> allBillss = service.findAllBills(); System.out.println(allBillss.size()); }
測試結果:
Hibernate: select bill0_.bid as bid1_1_, bill0_.bmoney as bmoney2_1_, bill0_.bname as bname3_1_, bill0_.createtime as createti4_1_, bill0_.hscode as hscode5_1_, bill0_.paystate as paystate6_1_, bill0_.scount as scount7_1_, bill0_.sunit as sunit8_1_, bill0_.supId as supId9_1_ from bill bill0_ Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? 19 --------------------------------- Hibernate: select bill0_.bid as bid1_1_, bill0_.bmoney as bmoney2_1_, bill0_.bname as bname3_1_, bill0_.createtime as createti4_1_, bill0_.hscode as hscode5_1_, bill0_.paystate as paystate6_1_, bill0_.scount as scount7_1_, bill0_.sunit as sunit8_1_, bill0_.supId as supId9_1_ from bill bill0_ Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? 19
和mybatis一樣,被spring管理的hibernate 的一級緩存無效。說明問題 出在spring身上。
先看下spring整合mybatis,sessionFactory是由mybatis提供的,進入sqlSessionFactory類內部。
/** * Copyright 2009-2015 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.ibatis.session; import java.sql.Connection; /** * Creates an {@link SqlSesion} out of a connection or a DataSource * * @author Clinton Begin */ public interface SqlSessionFactory { SqlSession openSession(); SqlSession openSession(boolean autoCommit); SqlSession openSession(Connection connection); SqlSession openSession(TransactionIsolationLevel level); SqlSession openSession(ExecutorType execType); SqlSession openSession(ExecutorType execType, boolean autoCommit); SqlSession openSession(ExecutorType execType, TransactionIsolationLevel level); SqlSession openSession(ExecutorType execType, Connection connection); Configuration getConfiguration(); }
然后找到它的一個實現類,sqlsessionManager,它同時還實現了SqlSession
** * @author Larry Meadors */ public class SqlSessionManager implements SqlSessionFactory, SqlSession { private final SqlSessionFactory sqlSessionFactory; private final SqlSession sqlSessionProxy; private ThreadLocal<SqlSession> localSqlSession = new ThreadLocal<SqlSession>(); private SqlSessionManager(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; this.sqlSessionProxy = (SqlSession) Proxy.newProxyInstance( SqlSessionFactory.class.getClassLoader(), new Class[]{SqlSession.class}, new SqlSessionInterceptor()); }
private ThreadLocal<SqlSession> localSqlSession = new ThreadLocal<SqlSession>();這個是讓session和當前的線程綁定
關於一級緩存失效的原因還要看他的另一個方法,實現自SqlSession
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { final SqlSession sqlSession = SqlSessionManager.this.localSqlSession.get(); if (sqlSession != null) { try { return method.invoke(sqlSession, args); } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } } else { final SqlSession autoSqlSession = openSession(); try { final Object result = method.invoke(autoSqlSession, args); autoSqlSession.commit(); return result; } catch (Throwable t) { autoSqlSession.rollback(); throw ExceptionUtil.unwrapThrowable(t); } finally { autoSqlSession.close(); } } }
//這是close方法 @Override public void close() { final SqlSession sqlSession = localSqlSession.get(); if (sqlSession == null) { throw new SqlSessionException("Error: Cannot close. No managed session is started."); } try { sqlSession.close(); } finally { localSqlSession.set(null); //釋放線程 } }
看下紅色部分的代碼,當一個非空的session傳過來,重新 opensession() 在代碼執行到最后執行autoSqlSession.close()方法,關閉session,釋放線程,跳出遞歸;
大致總結一下:
session和當前線程綁定的過程以及mybatis失效的原因
1,獲得請求
2,spring檢查到了這種需求,於是去申請一個mybatis的sqlsession(資源池),並將申請到的sqlsession與當前線程綁定,放入threadlocal里面
3,template從threadlocal獲取到sqlsession,去執行查詢
4,查詢結束,清空threadlocal中與當前線程綁定的sqlsession,釋放資源
5,我們又需要訪問數據,拿到新的session
6,返回到步驟2
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@org.junit.Test public void TestOneCache(){ //沒有被spring管理使用一級緩存 Session session = HibernateUtil.currentSession(); System.out.println(session); List<Bill> list=session.createQuery( "from Bill ").list(); System.out.println(list.size()); List<Bill> lists = session.createQuery( "from Bill ").list(); System.out.println(lists.size()); //被spring管理的hibernate 一級緩存失效 /*ApplicationContext context = new ClassPathXmlApplicationContext("spring-hiberanate.xml"); BillService service= (BillService)context.getBean("billServiceImpl"); List<Bill> allBills = service.findAllBills(); System.out.println(allBills.size()); System.out.println("---------------------------------"); List<Bill> allBillss = service.findAllBills(); System.out.println(allBillss.size());*/ }
很明顯走的緩存
Hibernate: select bill0_.bid as bid1_1_, bill0_.bmoney as bmoney2_1_, bill0_.bname as bname3_1_, bill0_.createtime as createti4_1_, bill0_.hscode as hscode5_1_, bill0_.paystate as paystate6_1_, bill0_.scount as scount7_1_, bill0_.sunit as sunit8_1_, bill0_.supId as supId9_1_ from bill bill0_ Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? 19 Hibernate: select bill0_.bid as bid1_1_, bill0_.bmoney as bmoney2_1_, bill0_.bname as bname3_1_, bill0_.createtime as createti4_1_, bill0_.hscode as hscode5_1_, bill0_.paystate as paystate6_1_, bill0_.scount as scount7_1_, bill0_.sunit as sunit8_1_, bill0_.supId as supId9_1_ from bill bill0_ 19
--被spring管理的hibernate緩存
@org.junit.Test public void TestOneCache(){ //被spring管理的hibernate 一級緩存失效 ApplicationContext context = new ClassPathXmlApplicationContext("spring-hiberanate.xml"); BillService service= (BillService)context.getBean("billServiceImpl"); List<Bill> allBills = service.findAllBills(); System.out.println(allBills.size()); System.out.println("---------------------------------"); List<Bill> allBillss = service.findAllBills(); System.out.println(allBillss.size()); }
Hibernate: select bill0_.bid as bid1_1_, bill0_.bmoney as bmoney2_1_, bill0_.bname as bname3_1_, bill0_.createtime as createti4_1_, bill0_.hscode as hscode5_1_, bill0_.paystate as paystate6_1_, bill0_.scount as scount7_1_, bill0_.sunit as sunit8_1_, bill0_.supId as supId9_1_ from bill bill0_ Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? 19 --------------------------------- Hibernate: select bill0_.bid as bid1_1_, bill0_.bmoney as bmoney2_1_, bill0_.bname as bname3_1_, bill0_.createtime as createti4_1_, bill0_.hscode as hscode5_1_, bill0_.paystate as paystate6_1_, bill0_.scount as scount7_1_, bill0_.sunit as sunit8_1_, bill0_.supId as supId9_1_ from bill bill0_ Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? Hibernate: select supplier0_.supId as supId1_2_0_, supplier0_.createtime as createti2_2_0_, supplier0_.supName as supName3_2_0_, supplier0_.supUserName as supUserN4_2_0_, supplier0_.supUserTel as supUserT5_2_0_, supplier0_.supcode as supcode6_2_0_, supplier0_.supfax as supfax7_2_0_ from supplier supplier0_ where supplier0_.supId=? 19
一級緩存失效
然后看下spring集成的hibernate配置文件,spring+hibernate的sessionFactory是交給spring來管理的,進入配置文件 找到LocalSessionFactory
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>zy.entity</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
private static final ThreadLocal<Session> thread=new ThreadLocal<Session>();
hibernate有兩個方法獲取session,一個是openSession(),一個是getCurrentSession()
a:getCurrentSession創建的session會綁定到當前線程,而openSession不會。
b:getCurrentSession創建的線程會在事務回滾或事務提交后自動關閉,而openSession必須手動關閉
public Session openSession() throws HibernateException { return this.withOptions().openSession(); } public Session openTemporarySession() throws HibernateException { return this.withOptions().autoClose(false).flushBeforeCompletion(false).connectionReleaseMode(ConnectionReleaseMode.AFTER_STATEMENT).openSession(); }
public SessionBuilder autoClose(boolean autoClose) { this.autoClose = autoClose; return this; }
public SessionBuilder flushBeforeCompletion(boolean flushBeforeCompletion) { this.flushBeforeCompletion = flushBeforeCompletion; return this; }
--翻了下他的源碼,感覺獲得session的過程像個迷宮似的,翻着翻着回來了,貌似回調,不懂,不過我覺得,他是根據請求每次獲得session時都“沖洗了”一下,但是我用工具類得到的session也是這樣啊
不知道spring到底對hibernate干了什么,讓一級緩存失效。你tm到底干了什么??????/
----希望知道的分享一下 ,非常感謝。