1. 程式人生 > >mybatis資料來源(JNDI、POOLED、UNPOOLED)原始碼詳解

mybatis資料來源(JNDI、POOLED、UNPOOLED)原始碼詳解

一、概述

    

二、建立

    mybatis資料來源的建立過程稍微有些曲折。

    1. 資料來源的建立過程;

    2. mybatis支援哪些資料來源,也就是dataSource標籤的type屬性可以寫哪些合法的引數?

    弄清楚這些問題,對mybatis的整個解析流程就清楚了,同理可以應用於任何一個配置上的解析上。

    從SqlSessionFactoryBuilder開始追溯DataSource的建立。SqlSessionFactoryBuilder中9個構造方法,其中字元流4個構造方法一一對應位元組流4個構造方法,都是將mybatis-config.xml配置檔案解析成Configuration物件,最終導向build(Configuration configuration)進行SqlSessionFactory的構造。

    配置檔案的在build(InputStream, env, Properties)構造方法中進行解析,InputStream和Reader方式除了流不一樣之外均相同,本處以InputStream為例,追蹤一下原始碼。

 

[html] view plain copy

  1. public SqlSessionFactory build(InputStream inputStream, String environment, Properties properties) {  
  2. try {  
  3.   // mybatis-config.xml檔案的解析物件  
  4.   // 在XMLConfigBuilder中封裝了Configuration物件  
  5.   // 此時還未真正發生解析,但是將解析的必備條件都準備好了  
  6.   XMLConfigBuilder parser = new XMLConfigBuilder(inputStream, environment, properties);  
  7.   // parser.parse()的呼叫標誌著解析的開始  
  8.   // mybatis-config.xml中的配置將會被解析成執行時物件封裝到Configuration中  
  9.   return build(parser.parse());  
  10. } catch (Exception e) {  
  11.   throw ExceptionFactory.wrapException("Error building SqlSession.", e);  
  12. } finally {  
  13.   ErrorContext.instance().reset();  
  14.   try {  
  15.     inputStream.close();  
  16.   } catch (IOException e) {  
  17.     // Intentionally ignore. Prefer previous error.  
  18.   }  
  19. }  
  20. }  

 

    在XMLConfigBuilder進一步追蹤,疑問最終保留在其父類BaseBuilder的resolveClass方法上,該方法對資料來源工廠的位元組碼進行查詢。

 

[html] view plain copy

  1. public Configuration parse() {  
  2.     if (parsed) {  
  3.       throw new BuilderException("Each XMLConfigBuilder can only be used once.");  
  4.     }  
  5.     parsed = true;  
  6.     // mybatis-config.xml的根節點就是configuration  
  7.     // 配置檔案的解析入口  
  8.     parseConfiguration(parser.evalNode("/configuration"));  
  9.     return configuration;  
  10.   }  
  11.   
  12.   private void parseConfiguration(XNode root) {  
  13.     try {  
  14.       propertiesElement(root.evalNode("properties")); //issue #117 read properties first  
  15.       typeAliasesElement(root.evalNode("typeAliases"));  
  16.       pluginElement(root.evalNode("plugins"));  
  17.       objectFactoryElement(root.evalNode("objectFactory"));  
  18.       objectWrapperFactoryElement(root.evalNode("objectWrapperFactory"));  
  19.       settingsElement(root.evalNode("settings"));  
  20.       // environment節點包含了事務和連線池節點  
  21.       environmentsElement(root.evalNode("environments")); // read it after objectFactory and objectWrapperFactory issue #631  
  22.       databaseIdProviderElement(root.evalNode("databaseIdProvider"));  
  23.       typeHandlerElement(root.evalNode("typeHandlers"));  
  24.       mapperElement(root.evalNode("mappers"));  
  25.     } catch (Exception e) {  
  26.       throw new BuilderException("Error parsing SQL Mapper Configuration. Cause: " + e, e);  
  27.     }  
  28.   }  
  29.     
  30.   private void environmentsElement(XNode context) throws Exception {  
  31.     if (context != null) {  
  32.       if (environment == null) {  
  33.         // 如果呼叫的build沒有傳入environment的id  
  34.         // 那麼就採用預設的environment,即environments標籤配置的default="environment_id"  
  35.         environment = context.getStringAttribute("default");  
  36.       }  
  37.       for (XNode child : context.getChildren()) {  
  38.         String id = child.getStringAttribute("id");  
  39.         if (isSpecifiedEnvironment(id)) {  
  40.           TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));  
  41.           // 資料來源工廠解析  
  42.           // 這裡是重點,資料來源工廠的查詢  
  43.           DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));  
  44.           // 工廠模式,生成相應的資料來源  
  45.           DataSource dataSource = dsFactory.getDataSource();  
  46.           Environment.Builder environmentBuilder = new Environment.Builder(id)  
  47.               .transactionFactory(txFactory)  
  48.               .dataSource(dataSource);  
  49.           configuration.setEnvironment(environmentBuilder.build());  
  50.         }  
  51.       }  
  52.     }  
  53.   }  
  54.     
  55.   private DataSourceFactory dataSourceElement(XNode context) throws Exception {  
  56.     if (context != null) {  
  57.       // dataSource標籤的屬性type  
  58.       String type = context.getStringAttribute("type");  
  59.       // 解析dataSource標籤下的子標籤<property name="" value="">  
  60.       // 實際上就是資料來源的配置資訊,url、driver、username、password等  
  61.       Properties props = context.getChildrenAsProperties();  
  62.       // resolveClass:到XMLConfigBuilder的父類BaseBuilder中進行工廠Class物件的查詢  
  63.       // 這裡是重點  
  64.       DataSourceFactory factory = (DataSourceFactory) resolveClass(type).newInstance();  
  65.       factory.setProperties(props);  
  66.       return factory;  
  67.     }  
  68.     throw new BuilderException("Environment declaration requires a DataSourceFactory.");  
  69.   }  

 

    在父類中並沒有窺探到重點,轉到其例項屬性typeAliasRegistry中才真正進行查詢過程。

 

[html] view plain copy

  1. protected Class<?> resolveClass(String alias) {  
  2.     if (alias == null) return null;  
  3.     try {  
  4.       // 做了一下檢查,轉  
  5.       return resolveAlias(alias);  
  6.     } catch (Exception e) {  
  7.       throw new BuilderException("Error resolving class. Cause: " + e, e);  
  8.     }  
  9.   }  
  10.   protected Class<?> resolveAlias(String alias) {  
  11.     // BaseBuilder中的例項屬性  
  12.     // 例項屬性:protected final TypeAliasRegistry typeAliasRegistry;  
  13.     return typeAliasRegistry.resolveAlias(alias);  
  14.   }<span style="font-family: SimSun; background-color: rgb(255, 255, 255);">  </span>  

 

    typeAliasRegistry中實際上是在一個Map中進行KV的匹配。

 

[html] view plain copy

  1. public <T> Class<T> resolveAlias(String string) {  
  2.     try {  
  3.       if (string == null) return null;  
  4.       String key = string.toLowerCase(Locale.ENGLISH); // issue #748  
  5.       Class<T> value;  
  6.       if (TYPE_ALIASES.containsKey(key)) {  
  7.         // private final Map<String, Class<?>> TYPE_ALIASES = new HashMap<String, Class<?>>();  
  8.         // TYPE_ALIASES是一個例項屬性,型別是一個Map  
  9.         value = (Class<T>) TYPE_ALIASES.get(key);  
  10.       } else {  
  11.         value = (Class<T>) Resources.classForName(string);  
  12.       }  
  13.       return value;  
  14.     } catch (ClassNotFoundException e) {  
  15.       throw new TypeException("Could not resolve type alias '" + string + "'.  Cause: " + e, e);  
  16.     }  
  17.   }  

 

    那麼問題就來了,工廠類什麼時候被註冊到這個map中的?

    實際上在SqlSessionFactoryBuilder的build(InputStream, env, Propeerties)方法中呼叫parse解析配置檔案之前,我們忽略了一段重要的程式碼。

    檢視建立XMLConfigBuilder的過程,根據繼承中初始化的規則,將會在父類BaseBuilder構造方法中建立Configuration物件,而Configuration物件的構造方法中將會註冊框架中的一些重要引數。

 

[html] view plain copy

  1. public XMLConfigBuilder(InputStream inputStream, String environment, Properties props) {  
  2.     // 轉  
  3.     this(new XPathParser(inputStream, true, props, new XMLMapperEntityResolver()), environment, props);  
  4.   }  
  5.     
  6.   private XMLConfigBuilder(XPathParser parser, String environment, Properties props) {  
  7.     // 轉調父類構造方法  
  8.     // 同時最終要的是直接new Configuration()傳入父類  
  9.     // Configuration中的屬性TypeAliasRegistry將會註冊資料來源工廠  
  10.     super(new Configuration());  
  11.     ErrorContext.instance().resource("SQL Mapper Configuration");  
  12.     this.configuration.setVariables(props);  
  13.     this.parsed = false;  
  14.     this.environment = environment;  
  15.     this.parser = parser;  
  16.   }  

[html] view plain copy

  1. public abstract class BaseBuilder {  
  2.   protected final Configuration configuration;  
  3.   protected final TypeAliasRegistry typeAliasRegistry;  
  4.   protected final TypeHandlerRegistry typeHandlerRegistry;  
  5.   
  6.   public BaseBuilder(Configuration configuration) {  
  7.     this.configuration = configuration;  
  8.     // typeAliasRegistry來自於Configuration  
  9.     // 也就是合理解釋了剛才通過typeAliasRegistry來找資料來源工廠  
  10.     this.typeAliasRegistry = this.configuration.getTypeAliasRegistry();  
  11.     this.typeHandlerRegistry = this.configuration.getTypeHandlerRegistry();  
  12.   }  

 

    至此,資料來源建立結束。接下來就看看怎麼用。

 

三、詳解

1. Mybatis datasource結構

2. mybatis JNDI

    mybatis JNDI之前已經剖析過原始碼,此處不再進行剖析,原文連結:點選開啟連結

3. mybatis UNPOOLED

    mybatis UNPOOLED資料來源建立的思想,先通過預設構造方法建立資料來源工廠(此時UNPOOLED dataSource隨之建立),將mybatis-config.xml中資料來源的配置資訊通過setProperties傳給工廠,然後通過工廠getDataSource。回顧一下這一段原始碼。

    最終是利用簡單的反射通過預設無參的構造方法例項化了資料來源工廠,此時在資料來源工廠中也例項化了UNPOOLED資料來源物件。

    resolveClass(type)這句話,從configuration中拿到UNPOOLED對應的value,即org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory.class,然後通過無參構造器例項化工廠物件,在工廠的無參構造中也直接例項化了dataSource物件,即org.apache.ibatis.datasource.unpooled.UnpooledDataSource,然後呼叫setProperties方法,把配置檔案中配置的引數(SqlSessionFactoryBuilder.builder中如果傳入Properties也會被putAll,同key則覆蓋value)進行dataSource設定。

    配置資料來源,最重要的是connection的獲取和管理,通過UNPOOLED方式來配置資料來源,實際上和直接是用JDBC沒有太多區別,操作的都是原生的、沒有任何修飾的connection。

4. POOLED

    POOLED工廠直接繼承UNPOOLED工廠,只是在POOLED工廠的預設構造中例項化org.apache.ibatis.datasource.pooled.PooledDataSource覆蓋了UNPOOLED中例項化的dataSource物件。其他的一模一樣,緊接著呼叫setProperties方法等。

    直接關注連線物件,通過POOLED,因為連線池需要存放連線物件,因此連線物件的close方法需要進行改寫,連線池的狀態也需要進行管理(PoolState封裝了連線池的狀態)。

    至於獲取連線,會care PoolState中空閒連結串列中是否還有可用的connection,有則直接返回,沒有則看是否已經到達配置的最大連線數,沒有到達則new一個新的,這部分原始碼較長但是邏輯簡單,就不貼出來了。直接看connection的代理部分吧。PooledConnection直接實現了JDK動態代理中的InvocationHandler,其invoke方法中重寫了close方法,將連線物件還回池中,當非close方法的時候,會檢查一下connection的狀態是否正常,正常則直接呼叫原邏輯。

[html] view plain copy

  1. class PooledConnection implements InvocationHandler {  
  2.   public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {  
  3.     String methodName = method.getName();  
  4.     if (CLOSE.hashCode() == methodName.hashCode() && CLOSE.equals(methodName)) {  
  5.       dataSource.pushConnection(this);  
  6.       return null;  
  7.     } else {  
  8.       try {  
  9.         if (!Object.class.equals(method.getDeclaringClass())) {  
  10.           // issue #579 toString() should never fail  
  11.           // throw an SQLException instead of a Runtime  
  12.           checkConnection();  
  13.         }  
  14.         return method.invoke(realConnection, args);  
  15.       } catch (Throwable t) {  
  16.         throw ExceptionUtil.unwrapThrowable(t);  
  17.       }  
  18.     }  
  19.   }  
  20. }  

 

5. Mybatis整合其他資料來源

    參看:點選開啟連結

    其實和第一節息息相關,直接使用mybatis的時候,在配置標籤<dataSource type="POOLED">,然後再Mybatis初始化的時候從Configuration中得到的POOLED=org.apache.ibatis.datasource.pooled.PooledDataSourceFactory.class,初始化這個物件就是直接通過POOLED得到class,然後newInstance通過預設構造方法直接例項化工廠物件,然後通過例項化出來的factory.setProperties(prop)把dataSource標籤中的配置引數一一注入工廠物件。

    原始碼思想很簡單,這裡就不再貼出來了,因此要在純mybatis環境下要繼承其他資料來源,例如C3P0或DBCP,只需要將其datasource繼承org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory即可,因為setProperties方法在UnpooledDataSourceFactory中定義,並且預設POOLED也是繼承於此。

    Mybatis是否支援自定義的資料來源,關鍵在於兩點,一是該資料來源是否有預設建構函式(無參建構函式),二是可以通過get/set方式來進行資料來源配置。滿足以上兩點,就足夠了。mybatis內建的資料來源實在是弱得看不下去,所以整合其他資料來源的時候,無論是傳統的JNDI/DBCP/C3P0還是當下牛逼閃閃的HikariCP也罷,都可以整合到mybatis中使用!當然,如果你使用Spring來處理資料來源,那麼這裡就可以不用考慮了,Spring-mybatis的jar包已經幫你處理好了···

 

    其實整個邏輯還是很簡單的,原始碼揭露一切。

 

 

附註:

    本文如有錯漏,煩請不吝指正,謝謝!