《深入理解mybatis原理》 Mybatis數據源與連接池


轉自:http://blog.csdn.net/luanlouis/article/details/37671851

對於ORM框架而言,數據源的組織是一個非常重要的一部分,這直接影響到框架的性能問題。本文將通過對MyBatis框架的數據源結構進行詳盡的分析,並且深入解析MyBatis的連接池。

    本文首先會講述MyBatis的數據源的分類,然后會介紹數據源是如何加載和使用的。緊接着將分類介紹UNPOOLED、POOLED和JNDI類型的數據源組織;期間我們會重點講解POOLED類型的數據源和其實現的連接池原理。

以下是本章的組織結構:


  • 一、MyBatis數據源DataSource分類
  • 二、數據源DataSource的創建過程
  • 三、 DataSource什么時候創建Connection對象
  • 四、不使用連接池的UnpooledDataSource
  • 五、為什么要使用連接池?
  • 六、使用了連接池的PooledDataSource


一、MyBatis數據源DataSource分類

MyBatis數據源實現是在以下四個包中:

MyBatis把數據源DataSource分為三種:

        ž UNPOOLED    不使用連接池的數據源

        ž POOLED      使用連接池的數據源

        ž JNDI            使用JNDI實現的數據源

即:

相應地,MyBatis內部分別定義了實現了java.sql.DataSource接口的UnpooledDataSource,PooledDataSource類來表示UNPOOLED、POOLED類型的數據源。 如下圖所示:

對於JNDI類型的數據源DataSource,則是通過JNDI上下文中取值。


二、數據源DataSource的創建過程

 MyBatis數據源DataSource對象的創建發生在MyBatis初始化的過程中。下面讓我們一步步地了解MyBatis是如何創建數據源DataSource的。

在mybatis的XML配置文件中,使用<dataSource>元素來配置數據源:

1.  MyBatis在初始化時,解析此文件,根據<dataSource>的type屬性來創建相應類型的的數據源DataSource,即:

  • type=”POOLED”  :MyBatis會創建PooledDataSource實例
  • type=”UNPOOLED” :MyBatis會創建UnpooledDataSource實例
  • type=”JNDI”     :MyBatis會從JNDI服務上查找DataSource實例,然后返回使用

2.  順便說一下,MyBatis是通過工廠模式來創建數據源DataSource對象的,MyBatis定義了抽象的工廠接口:org.apache.ibatis.datasource.DataSourceFactory,通過其getDataSource()方法返回數據源DataSource:

定義如下:

1 public interface DataSourceFactory {
2 
3   void setProperties(Properties props);
4   //生產DataSource
5   DataSource getDataSource();
6 }
View Code

上述三種不同類型的type,則有對應的以下dataSource工廠:

  • POOLED        PooledDataSourceFactory
  • UNPOOLED     UnpooledDataSourceFactory
  • JNDI          JndiDataSourceFactory

   其類圖如下所示:


3.  MyBatis創建了DataSource實例后,會將其放到Configuration對象內的Environment對象中, 供以后使用。

三、 DataSource什么時候創建Connection對象

當我們需要創建SqlSession對象並需要執行SQL語句時,這時候MyBatis才會去調用dataSource對象來創建java.sql.Connection對象。也就是說,java.sql.Connection對象的創建一直延遲到執行SQL語句的時候。

比如,我們有如下方法執行一個簡單的SQL語句:

1 String resource = "mybatis-config.xml";
2 InputStream inputStream = Resources.getResourceAsStream(resource);
3 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
4 SqlSession sqlSession = sqlSessionFactory.openSession();
5 sqlSession.selectList("SELECT * FROM STUDENTS");
View Code
前4句都不會導致java.sql.Connection對象的創建,只有當第5句sqlSession.selectList("SELECT * FROM STUDENTS"),才會觸發MyBatis在底層執行下面這個方法來創建java.sql.Connection對象:
 1 protected void openConnection() throws SQLException {
 2     if (log.isDebugEnabled()) {
 3       log.debug("Opening JDBC Connection");
 4     }
 5     connection = dataSource.getConnection();
 6     if (level != null) {
 7       connection.setTransactionIsolation(level.getLevel());
 8     }
 9     setDesiredAutoCommit(autoCommmit);
10   }
View Code

而對於DataSource的UNPOOLED的類型的實現-UnpooledDataSource是怎樣實現getConnection()方法的呢?請看下一節。

四、不使用連接池的UnpooledDataSource

當 <dataSource>的type屬性被配置成了”UNPOOLED”,MyBatis首先會實例化一個UnpooledDataSourceFactory工廠實例,然后通過.getDataSource()方法返回一個UnpooledDataSource實例對象引用,我們假定為dataSource。

使用UnpooledDataSourcegetConnection(),每調用一次就會產生一個新的Connection實例對象。

UnPooledDataSource的getConnection()方法實現如下:

 1 /*
 2 UnpooledDataSource的getConnection()實現
 3 */
 4 public Connection getConnection() throws SQLException
 5 {
 6     return doGetConnection(username, password);
 7 }
 8 
 9 private Connection doGetConnection(String username, String password) throws SQLException
10 {
11     //封裝username和password成properties
12     Properties props = new Properties();
13     if (driverProperties != null)
14     {
15         props.putAll(driverProperties);
16     }
17     if (username != null)
18     {
19         props.setProperty("user", username);
20     }
21     if (password != null)
22     {
23         props.setProperty("password", password);
24     }
25     return doGetConnection(props);
26 }
27 
28 /*
29  *  獲取數據連接
30  */
31 private Connection doGetConnection(Properties properties) throws SQLException
32 {
33     //1.初始化驅動
34     initializeDriver();
35     //2.從DriverManager中獲取連接,獲取新的Connection對象
36     Connection connection = DriverManager.getConnection(url, properties);
37     //3.配置connection屬性
38     configureConnection(connection);
39     return connection;
40 }
View Code

如上代碼所示,UnpooledDataSource會做以下事情:

1.  初始化驅動:    判斷driver驅動是否已經加載到內存中,如果還沒有加載,則會動態地加載driver類,並實例化一個Driver對象,使用DriverManager.registerDriver()方法將其注冊到內存中,以供后續使用。

2.  創建Connection對象:    使用DriverManager.getConnection()方法創建連接。

3.  配置Connection對象:    設置是否自動提交autoCommit和隔離級別isolationLevel。

4.  返回Connection對象。

上述的序列圖如下所示:

總結:從上述的代碼中可以看到,我們每調用一次getConnection()方法,都會通過DriverManager.getConnection()返回新的java.sql.Connection實例。

五、為什么要使用連接池?

1. 創建一個java.sql.Connection實例對象的代價

首先讓我們來看一下創建一個java.sql.Connection對象的資源消耗。我們通過連接Oracle數據庫,創建創建Connection對象,來看創建一個Connection對象、執行SQL語句各消耗多長時間。代碼如下:

 1 public static void main(String[] args) throws Exception
 2 {
 3 
 4     String sql = "select * from hr.employees where employee_id < ? and employee_id >= ?";
 5     PreparedStatement st = null;
 6     ResultSet rs = null;
 7 
 8     long beforeTimeOffset = -1L; //創建Connection對象前時間
 9     long afterTimeOffset = -1L; //創建Connection對象后時間
10     long executeTimeOffset = -1L; //創建Connection對象后時間
11 
12     Connection con = null;
13     Class.forName("oracle.jdbc.driver.OracleDriver");
14 
15     beforeTimeOffset = new Date().getTime();
16     System.out.println("before:\t" + beforeTimeOffset);
17 
18     con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "louluan", "123456");
19 
20     afterTimeOffset = new Date().getTime();
21     System.out.println("after:\t\t" + afterTimeOffset);
22     System.out.println("Create Costs:\t\t" + (afterTimeOffset - beforeTimeOffset) + " ms");
23 
24     st = con.prepareStatement(sql);
25     //設置參數
26     st.setInt(1, 101);
27     st.setInt(2, 0);
28     //查詢,得出結果集
29     rs = st.executeQuery();
30     executeTimeOffset = new Date().getTime();
31     System.out.println("Exec Costs:\t\t" + (executeTimeOffset - afterTimeOffset) + " ms");
32 
33 }
View Code
上述程序在我筆記本上的執行結果為:

從此結果可以清楚地看出,創建一個Connection對象,用了250 毫秒;而執行SQL的時間用了170毫秒

創建一個Connection對象用了250毫秒!這個時間對計算機來說可以說是一個非常奢侈的!

這僅僅是一個Connection對象就有這么大的代價,設想一下另外一種情況:如果我們在Web應用程序中,為用戶的每一個請求就操作一次數據庫,當有10000個在線用戶並發操作的話,對計算機而言,僅僅創建Connection對象不包括做業務的時間就要損耗10000×250ms= 250 0000 ms = 2500 s = 41.6667 min,竟然要41分鍾!!!如果對高用戶群體使用這樣的系統,簡直就是開玩笑!

2. 問題分析:

創建一個java.sql.Connection對象的代價是如此巨大,是因為創建一個Connection對象的過程,在底層就相當於和數據庫建立的通信連接,在建立通信連接的過程,消耗了這么多的時間,而往往我們建立連接后(即創建Connection對象后),就執行一個簡單的SQL語句,然后就要拋棄掉,這是一個非常大的資源浪費!

3.解決方案:

對於需要頻繁地跟數據庫交互的應用程序,可以在創建了Connection對象,並操作完數據庫后,可以不釋放掉資源,而是將它放到內存中,當下次需要操作數據庫時,可以直接從內存中取出Connection對象,不需要再創建了,這樣就極大地節省了創建Connection對象的資源消耗。由於內存也是有限和寶貴的,這又對我們對內存中的Connection對象怎么有效地維護提出了很高的要求。我們將在內存中存放Connection對象的容器稱之為 連接池(Connection Pool)。下面讓我們來看一下MyBatis的線程池是怎樣實現的。

六、使用了連接池的PooledDataSource

同樣地,我們也是使用PooledDataSource的getConnection()方法來返回Connection對象。現在讓我們看一下它的基本原理:

 PooledDataSource將java.sql.Connection對象包裹成PooledConnection對象放到了PoolState類型的容器中維護。 MyBatis將連接池中的PooledConnection分為兩種狀態: 空閑狀態(idle)和活動狀態(active),這兩種狀態的PooledConnection對象分別被存儲到PoolState容器內的idleConnectionsactiveConnections兩個List集合中:

idleConnections:空閑(idle)狀態PooledConnection對象被放置到此集合中,表示當前閑置的沒有被使用的PooledConnection集合,調用PooledDataSource的getConnection()方法時,會優先從此集合中取PooledConnection對象。當用完一個java.sql.Connection對象時,MyBatis會將其包裹成PooledConnection對象放到此集合中。

activeConnections:活動(active)狀態的PooledConnection對象被放置到名為activeConnections的ArrayList中,表示當前正在被使用的PooledConnection集合,調用PooledDataSource的getConnection()方法時,會優先從idleConnections集合中取PooledConnection對象,如果沒有,則看此集合是否已滿,如果未滿,PooledDataSource會創建出一個PooledConnection,添加到此集合中,並返回。

 

PoolState連接池的大致結構如下所示:

6.1 獲取java.sql.Connection對象的過程

下面讓我們看一下PooledDataSource 的getConnection()方法獲取Connection對象的實現:
1  public Connection getConnection() throws SQLException {
2     return popConnection(dataSource.getUsername(), dataSource.getPassword()).getProxyConnection();
3   }
4 
5   public Connection getConnection(String username, String password) throws SQLException {
6     return popConnection(username, password).getProxyConnection();
7   }
View Code

上述的popConnection()方法,會從連接池中返回一個可用的PooledConnection對象,然后再調用getProxyConnection()方法最終返回Conection對象。(至於為什么會有getProxyConnection(),請關注下一節)

現在讓我們看一下popConnection()方法到底做了什么:

1.  先看是否有空閑(idle)狀態下的PooledConnection對象,如果有,就直接返回一個可用的PooledConnection對象;否則進行第2步。

2.  查看活動狀態的PooledConnection池activeConnections是否已滿;如果沒有滿,則創建一個新的PooledConnection對象,然后放到activeConnections池中,然后返回此PooledConnection對象;否則進行第三步;

3.  看最先進入activeConnections池中的PooledConnection對象是否已經過期:如果已經過期,從activeConnections池中移除此對象,然后創建一個新的PooledConnection對象,添加到activeConnections中,然后將此對象返回;否則進行第4步。

4.  線程等待,循環2步

  1 /*
  2  * 傳遞一個用戶名和密碼,從連接池中返回可用的PooledConnection
  3  */
  4 private PooledConnection popConnection(String username, String password) throws SQLException
  5 {
  6     boolean countedWait = false;
  7     PooledConnection conn = null;
  8     long t = System.currentTimeMillis();
  9     int localBadConnectionCount = 0;
 10 
 11     while (conn == null)
 12     {
 13         synchronized (state)
 14         {
 15             if (state.idleConnections.size() > 0)
 16             {
 17                 // 連接池中有空閑連接,取出第一個
 18                 conn = state.idleConnections.remove(0);
 19                 if (log.isDebugEnabled())
 20                 {
 21                     log.debug("Checked out connection " + conn.getRealHashCode() + " from pool.");
 22                 }
 23             }
 24             else
 25             {
 26                 // 連接池中沒有空閑連接,則取當前正在使用的連接數小於最大限定值,
 27                 if (state.activeConnections.size() < poolMaximumActiveConnections)
 28                 {
 29                     // 創建一個新的connection對象
 30                     conn = new PooledConnection(dataSource.getConnection(), this);
 31                     @SuppressWarnings("unused")
 32                     //used in logging, if enabled
 33                     Connection realConn = conn.getRealConnection();
 34                     if (log.isDebugEnabled())
 35                     {
 36                         log.debug("Created connection " + conn.getRealHashCode() + ".");
 37                     }
 38                 }
 39                 else
 40                 {
 41                     // Cannot create new connection 當活動連接池已滿,不能創建時,取出活動連接池的第一個,即最先進入連接池的PooledConnection對象
 42                     // 計算它的校驗時間,如果校驗時間大於連接池規定的最大校驗時間,則認為它已經過期了,利用這個PoolConnection內部的realConnection重新生成一個PooledConnection
 43                     //
 44                     PooledConnection oldestActiveConnection = state.activeConnections.get(0);
 45                     long longestCheckoutTime = oldestActiveConnection.getCheckoutTime();
 46                     if (longestCheckoutTime > poolMaximumCheckoutTime)
 47                     {
 48                         // Can claim overdue connection
 49                         state.claimedOverdueConnectionCount++;
 50                         state.accumulatedCheckoutTimeOfOverdueConnections += longestCheckoutTime;
 51                         state.accumulatedCheckoutTime += longestCheckoutTime;
 52                         state.activeConnections.remove(oldestActiveConnection);
 53                         if (!oldestActiveConnection.getRealConnection().getAutoCommit())
 54                         {
 55                             oldestActiveConnection.getRealConnection().rollback();
 56                         }
 57                         conn = new PooledConnection(oldestActiveConnection.getRealConnection(), this);
 58                         oldestActiveConnection.invalidate();
 59                         if (log.isDebugEnabled())
 60                         {
 61                             log.debug("Claimed overdue connection " + conn.getRealHashCode() + ".");
 62                         }
 63                     }
 64                     else
 65                     {
 66 
 67                         //如果不能釋放,則必須等待有
 68                         // Must wait
 69                         try
 70                         {
 71                             if (!countedWait)
 72                             {
 73                                 state.hadToWaitCount++;
 74                                 countedWait = true;
 75                             }
 76                             if (log.isDebugEnabled())
 77                             {
 78                                 log.debug("Waiting as long as " + poolTimeToWait + " milliseconds for connection.");
 79                             }
 80                             long wt = System.currentTimeMillis();
 81                             state.wait(poolTimeToWait);
 82                             state.accumulatedWaitTime += System.currentTimeMillis() - wt;
 83                         }
 84                         catch (InterruptedException e)
 85                         {
 86                             break;
 87                         }
 88                     }
 89                 }
 90             }
 91 
 92             //如果獲取PooledConnection成功,則更新其信息
 93 
 94             if (conn != null)
 95             {
 96                 if (conn.isValid())
 97                 {
 98                     if (!conn.getRealConnection().getAutoCommit())
 99                     {
100                         conn.getRealConnection().rollback();
101                     }
102                     conn.setConnectionTypeCode(assembleConnectionTypeCode(dataSource.getUrl(), username, password));
103                     conn.setCheckoutTimestamp(System.currentTimeMillis());
104                     conn.setLastUsedTimestamp(System.currentTimeMillis());
105                     state.activeConnections.add(conn);
106                     state.requestCount++;
107                     state.accumulatedRequestTime += System.currentTimeMillis() - t;
108                 }
109                 else
110                 {
111                     if (log.isDebugEnabled())
112                     {
113                         log.debug("A bad connection (" + conn.getRealHashCode() + ") was returned from the pool, getting another connection.");
114                     }
115                     state.badConnectionCount++;
116                     localBadConnectionCount++;
117                     conn = null;
118                     if (localBadConnectionCount > (poolMaximumIdleConnections + 3))
119                     {
120                         if (log.isDebugEnabled())
121                         {
122                             log.debug("PooledDataSource: Could not get a good connection to the database.");
123                         }
124                         throw new SQLException("PooledDataSource: Could not get a good connection to the database.");
125                     }
126                 }
127             }
128         }
129 
130     }
131 
132     if (conn == null)
133     {
134         if (log.isDebugEnabled())
135         {
136             log.debug("PooledDataSource: Unknown severe error condition.  The connection pool returned a null connection.");
137         }
138         throw new SQLException("PooledDataSource: Unknown severe error condition.  The connection pool returned a null connection.");
139     }
140 
141     return conn;
142 }
View Code

對應的處理流程圖如下所示:

如上所示,對於PooledDataSource的getConnection()方法內,先是調用類PooledDataSource的popConnection()方法返回了一個PooledConnection對象,然后調用了PooledConnection的getProxyConnection()來返回Connection對象。

6.2java.sql.Connection對象的回收

       當我們的程序中使用完Connection對象時,如果不使用數據庫連接池,我們一般會調用 connection.close()方法,關閉connection連接,釋放資源。如下所示:
 1 private void test() throws ClassNotFoundException, SQLException
 2 {
 3     String sql = "select * from hr.employees where employee_id < ? and employee_id >= ?";
 4     PreparedStatement st = null;
 5     ResultSet rs = null;
 6 
 7     Connection con = null;
 8     Class.forName("oracle.jdbc.driver.OracleDriver");
 9     try
10     {
11         con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "louluan", "123456");
12         st = con.prepareStatement(sql);
13         //設置參數
14         st.setInt(1, 101);
15         st.setInt(2, 0);
16         //查詢,得出結果集
17         rs = st.executeQuery();
18         //取數據,省略
19         //關閉,釋放資源
20         con.close();
21     }
22     catch (SQLException e)
23     {
24         con.close();
25         e.printStackTrace();
26     }
27 }
View Code

調用過close()方法的Connection對象所持有的資源會被全部釋放掉,Connection對象也就不能再使用。

那么,如果我們使用了連接池,我們在用完了Connection對象時,需要將它放在連接池中,該怎樣做呢?

可能大家第一個在腦海里閃現出來的想法就是:我在應該調用con.close()方法的時候,不調用close()f方法,將其換成將Connection對象放到連接池容器中的代碼!

好,我們將上述的想法實現,首先定義一個簡易連接池Pool,然后將上面的代碼改寫:
 1 package com.foo.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.SQLException;
 6 import java.util.Vector;
 7 
 8 /**
 9  * 
10  * 一個線程安全的簡易連接池實現,此連接池是單例的
11  *  putConnection()將Connection添加到連接池中
12  *  getConnection()返回一個Connection對象
13  */
14 public class Pool {
15 
16     private static Vector<Connection> pool = new Vector<Connection>();
17     
18     private static int MAX_CONNECTION =100;
19     
20     private static String DRIVER="oracle.jdbc.driver.OracleDriver";
21     private static String URL = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
22     private static String USERNAME = "louluan";
23     private static String PASSWROD = "123456";
24     
25     static {
26         try {
27             Class.forName(DRIVER);
28         } catch (ClassNotFoundException e) {
29             e.printStackTrace();
30         }
31     }
32     
33     /**
34      * 將一個Connection對象放置到連接池中 
35      */
36     public static  void putConnection(Connection connection){
37         
38         synchronized(pool)
39         {
40             if(pool.size()<MAX_CONNECTION)
41             {
42                 pool.add(connection);       
43             }
44         }
45     }
46     
47     
48     /**
49      * 返回一個Connection對象,如果連接池內有元素,則pop出第一個元素;
50      * 如果連接池Pool中沒有元素,則創建一個connection對象,然后添加到pool中
51      * @return Connection
52      */
53     public static Connection getConnection(){
54         Connection connection = null;
55         synchronized(pool)
56         {
57             if(pool.size()>0)
58             {
59                 connection = pool.get(0);
60                 pool.remove(0);
61             }
62             else
63             {
64                 connection = createConnection();
65                 pool.add(connection);
66             }
67         }
68         return connection;
69     }
70     
71     /**
72      * 創建一個新的Connection對象
73      */
74     private static Connection createConnection()
75     {
76         Connection connection = null;
77         try {
78             connection = DriverManager.getConnection(URL, USERNAME,PASSWROD);
79         } catch (SQLException e) {
80             e.printStackTrace();
81         }
82         return connection;
83     }
84     
85 }
View Code
 1 package com.foo.jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 import java.util.Vector;
 9 
10 public class PoolTest
11 {
12 
13     private void test() throws ClassNotFoundException, SQLException
14     {
15         String sql = "select * from hr.employees where employee_id < ? and employee_id >= ?";
16         PreparedStatement st = null;
17         ResultSet rs = null;
18 
19         Connection con = null;
20         Class.forName("oracle.jdbc.driver.OracleDriver");
21         try
22         {
23             con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "louluan", "123456");
24             st = con.prepareStatement(sql);
25             //設置參數
26             st.setInt(1, 101);
27             st.setInt(2, 0);
28             //查詢,得出結果集
29             rs = st.executeQuery();
30             //取數據,省略
31             //將不再使用的Connection對象放到連接池中,供以后使用
32             Pool.putConnection(con);
33         }
34         catch (SQLException e)
35         {
36             e.printStackTrace();
37         }
38     }
39 }
View Code

上述的代碼就是將我們使用過的Connection對象放到Pool連接池中,我們需要Connection對象的話,只需要使用Pool.getConnection()方法從里面取即可。

是的,上述的代碼完全可以實現此能力,不過有一個很不優雅的實現:就是我們需要手動地將Connection對象放到Pool連接池中,這是一個很傻的實現方式。這也和一般使用Connection對象的方式不一樣:一般使用Connection的方式是使用完后,然后調用.close()方法釋放資源。

為了和一般的使用Conneciton對象的方式保持一致,我們希望當Connection使用完后,調用.close()方法,而實際上Connection資源並沒有被釋放,而實際上被添加到了連接池中。這樣可以做到嗎?答案是可以。上述的要求從另外一個角度來描述就是:能否提供一種機制,讓我們知道Connection對象調用了什么方法,從而根據不同的方法自定義相應的處理機制。恰好代理機制就可以完成上述要求.

怎樣實現Connection對象調用了close()方法,而實際是將其添加到連接池中

這是要使用代理模式,為真正的Connection對象創建一個代理對象,代理對象所有的方法都是調用相應的真正Connection對象的方法實現。當代理對象執行close()方法時,要特殊處理,不調用真正Connection對象的close()方法,而是將Connection對象添加到連接池中。

MyBatis的PooledDataSource的PoolState內部維護的對象是PooledConnection類型的對象,而PooledConnection則是對真正的數據庫連接java.sql.Connection實例對象的包裹器。

PooledConnection對象內持有一個真正的數據庫連接java.sql.Connection實例對象和一個java.sql.Connection的代理:

其部分定義如下:

 1 class PooledConnection implements InvocationHandler {
 2   
 3   //......
 4   //所創建它的datasource引用
 5   private PooledDataSource dataSource;
 6   //真正的Connection對象
 7   private Connection realConnection;
 8   //代理自己的代理Connection
 9   private Connection proxyConnection;
10   
11   //......
12 }
View Code
PooledConenction實現了InvocationHandler接口,並且,proxyConnection對象也是根據這個它來生成的代理對象:
1 public PooledConnection(Connection connection, PooledDataSource dataSource) {
2     this.hashCode = connection.hashCode();
3     this.realConnection = connection;
4     this.dataSource = dataSource;
5     this.createdTimestamp = System.currentTimeMillis();
6     this.lastUsedTimestamp = System.currentTimeMillis();
7     this.valid = true;
8     this.proxyConnection = (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(), IFACES, this);
9   }
View Code

實際上,我們調用PooledDataSource的getConnection()方法返回的就是這個proxyConnection對象。

當我們調用此proxyConnection對象上的任何方法時,都會調用PooledConnection對象內invoke()方法。

讓我們看一下PooledConnection類中的invoke()方法定義:
 1 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 2     String methodName = method.getName();
 3     //當調用關閉的時候,回收此Connection到PooledDataSource中
 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           checkConnection();
11         }
12         return method.invoke(realConnection, args);
13       } catch (Throwable t) {
14         throw ExceptionUtil.unwrapThrowable(t);
15       }
16     }
17   }
View Code

從上述代碼可以看到,當我們使用了pooledDataSource.getConnection()返回的Connection對象的close()方法時,不會調用真正Connection的close()方法,而是將此Connection對象放到連接池中。

七、JNDI類型的數據源DataSource

對於JNDI類型的數據源DataSource的獲取就比較簡單,MyBatis定義了一個JndiDataSourceFactory工廠來創建通過JNDI形式生成的DataSource。

下面讓我們看一下JndiDataSourceFactory的關鍵代碼:
 1 if (properties.containsKey(INITIAL_CONTEXT)
 2         && properties.containsKey(DATA_SOURCE))
 3 {
 4     //從JNDI上下文中找到DataSource並返回
 5     Context ctx = (Context) initCtx.lookup(properties.getProperty(INITIAL_CONTEXT));
 6     dataSource = (DataSource) ctx.lookup(properties.getProperty(DATA_SOURCE));
 7 }
 8 else if (properties.containsKey(DATA_SOURCE))
 9 {
10     // //從JNDI上下文中找到DataSource並返回
11     dataSource = (DataSource) initCtx.lookup(properties.getProperty(DATA_SOURCE));
12 }
View Code


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM