Mybatis深入剖析 Executor分析

2021-08-20 13:32:50 字數 2433 閱讀 2331

executor

是跟sqlsession

繫結在一起的,每乙個

sqlsession

都擁有乙個新的

executor

物件,由

configuration

建立。**如下

public executor newexecutor(transaction transaction, executortype executortype)  else if (executortype.reuse == executortype)  else 

if (cacheenabled)

executor = (executor) interceptorchain.pluginall(executor);

return executor;

}

executor類圖如下:

executor分成兩大類:

1.baseexecutor

抽像類,在建立時會根據傳過來的executortype建立不同的類,如下:

******executor

:每執行一次update或select,就開啟乙個statement物件,用完立刻關閉statement物件。(可以是statement或preparestatement物件)

reuseexecutor

:執行update或select,以sql作為key查詢statement物件,存在就使用,不存在就建立,用完後,不關閉statement物件,而是放置於map內,供下一次使用。(可以是statement或preparestatement物件),**如下:

@override

configuration configuration = ms.getconfiguration();

statementhandler handler = configuration.newstatementhandler(this, ms, parameter, rowbounds.default, null, null);

statement stmt = preparestatement(handler, ms.getstatementlog());

return handler.update(stmt);

}private statement preparestatement(statementhandler handler, log statementlog) throws sqlexception else

handler.parameterize(stmt);

return stmt;

}private boolean hasstatementfor(string sql) catch (sqlexception e)

}private statement getstatement(string s)

private void putstatement(string sql, statement stmt)

.....

batchexecutor:執行update(沒有select,jdbc批處理不支援select),將所有sql都新增到批處理中(addbatch()),等待統一執行(executebatch()),它快取了多個statement物件,每個statement物件都是addbatch()完畢後,等待逐一執行executebatch()批處理的;batchexecutor相當於維護了多個桶,每個桶裡都裝了很多屬於自己的sql,就像蘋果藍里裝了很多蘋果,番茄藍里裝了很多番茄,最後,再統一倒進倉庫。(可以是statement或preparestatement物件)

2.cachingexecutor

先從快取中獲取查詢結果,存在就返回,不存在,再委託給executor delegate去資料庫取,

delegate可以是上面任一的******executor、reuseexecutor、batchexecutor。**如下:

public cachingexecutor(executor delegate) 

....

@override

flushcacheifrequired(ms);

return delegate.update(ms, parameterobject);

}@override

throws sqlexception

return list;}}

return delegate.query(ms, parameterobject, rowbounds, resulthandler, key, boundsql);

}

......

PGA深入剖析

pga pga系統全域性區 program global area 程序全域性區 process global area pga是乙個記憶體區域,該區域包含了一些與某個特定伺服器程序相關的資料和控制資訊,每個程序都有自己的私有pga區,所以這塊區域只能被其所屬程序進入,而不能被其他程序訪問,所以在p...

epoll LT ET 深入剖析

epoll lt et 深入剖析 epoll事件有兩種模型 level triggered lt 水平觸發 socket接收緩衝區不為空 有資料可讀 讀事件一直觸發 socket傳送緩衝區不滿 可以繼續寫入資料 寫事件一直觸發 符合思維習慣,epoll wait返回的事件就是socket的狀態 ed...

深入剖析printf

printf主要是給控制台列印字串或者數字等。1 printf的引數列表printf const char format,2 printf首先呼叫va start.然後呼叫 output l,最後呼叫 ftbuf 3 output l會解析字串並呼叫va arg,獲取省略號的值,呼叫write st...