mybatis一級緩存的問題
spring整合mybatis后,sqlSeesion被sprinhIoc容器管理起來。假如第一次查詢數據庫用到sqlSeesion1,第二次我還是進行相同的查詢操作,Ioc容器會使用上次給我的sqlSession1(也就是使用緩存),還是會重新創建一個新的sqlSession。
spring整合mybatis后,sqlSeesion被sprinhIoc容器管理起來。假如第一次查詢數據庫用到sqlSeesion1,第二次我還是進行相同的查詢操作,Ioc容器會使用上次給我的sqlSession1(也就是使用緩存),還是會重新創建一個新的sqlSession。
2017-05-04
舉報
2017-05-08
關于緩存問題:
http://www.cnblogs.com/zemliu/archive/2013/08/05/3239014.html
執行了2次sql查詢,看似我們使用了同一個sqlSession,但是實際上因為我們的dao繼承了SqlSessionDaoSupport,而SqlSessionDaoSupport內部sqlSession的實現是使用用動態代理實現的,這個動態代理sqlSessionProxy使用一個模板方法封裝了select()等操作,每一次select()查詢都會自動先執行openSession(),執行完close()以后調用close()方法,相當于生成了一個新的session實例,所以我們無需手動的去關閉這個session()(關于這一點見下面mybatis的官方文檔),當然也無法使用mybatis的一級緩存,也就是說mybatis的一級緩存在spring中是沒有作用的.
官方文檔摘要
MyBatis?SqlSession?provides you with specific methods to handle transactions programmatically. But when using MyBatis-Spring your beans will be injected with a Spring managed?SqlSession?or a Spring managed mapper. That means that Spring will?always?handle your transactions.
You cannot call?SqlSession.commit(),?SqlSession.rollback()?or?SqlSession.close()?over a Spring managed?SqlSession. If you try to do so, a?UnsupportedOperationException?exception will be thrown. Note these methods are not exposed in injected mapper classes.
2017-05-08
會重新創建一個新的sqlSession,因為每次的數據庫操作,一般都是以下四步:
1,獲取SqlSession對象
2,調用數據庫操作的方法
3,提交事務
4,關閉SqlSession