MyBatis數(shù)據(jù)源DataSource分類
MyBatis把數(shù)據(jù)源DataSource分為三種:
- UNPOOLED 不使用連接池的數(shù)據(jù)源
- POOLED 使用連接池的數(shù)據(jù)源
- JNDI 使用JNDI實現(xiàn)的數(shù)據(jù)源
相應(yīng)地,MyBatis內(nèi)部分別定義了實現(xiàn)了java.sql.DataSource接口的UnpooledDataSource,PooledDataSource類來表示UNPOOLED、POOLED類型的數(shù)據(jù)源。 如下圖所示:

數(shù)據(jù)源DataSource的創(chuàng)建過程
MyBatis數(shù)據(jù)源DataSource對象的創(chuàng)建發(fā)生在MyBatis初始化的過程中。下面讓我們一步步地了解MyBatis是如何創(chuàng)建數(shù)據(jù)源DataSource的。
在mybatis的XML配置文件中,使用<dataSource>元素來配置數(shù)據(jù)源:

- MyBatis在初始化時,解析此文件,根據(jù)<dataSource>的type屬性來創(chuàng)建相應(yīng)類型的的數(shù)據(jù)源DataSource,即:
type=”POOLED” :MyBatis會創(chuàng)建PooledDataSource實例
type=”UNPOOLED” :MyBatis會創(chuàng)建UnpooledDataSource實例
type=”JNDI” :MyBatis會從JNDI服務(wù)上查找DataSource實例,然后返回使用
順便說一下,MyBatis是通過工廠模式來創(chuàng)建數(shù)據(jù)源DataSource對象的,MyBatis定義了抽象的工廠接口:org.apache.ibatis.datasource.DataSourceFactory,通過其getDataSource()方法返回數(shù)據(jù)源DataSource:
public interface DataSourceFactory {
void setProperties(Properties props);
//生產(chǎn)DataSource
DataSource getDataSource();
}
上述三種不同類型的type,則有對應(yīng)的以下dataSource工廠:
POOLED PooledDataSourceFactory
UNPOOLED UnpooledDataSourceFactory
JNDI JndiDataSourceFactory
MyBatis創(chuàng)建了DataSource實例后,會將其放到Configuration對象內(nèi)的Environment對象中, 供以后使用。
DataSource什么時候創(chuàng)建Connection對象
當(dāng)我們需要創(chuàng)建SqlSession對象并需要執(zhí)行SQL語句時,這時候MyBatis才會去調(diào)用dataSource對象來創(chuàng)建java.sql.Connection對象。也就是說,java.sql.Connection對象的創(chuàng)建一直延遲到執(zhí)行SQL語句的時候。
比如,我們有如下方法執(zhí)行一個簡單的SQL語句:
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.selectList("SELECT * FROM STUDENTS");
前4句都不會導(dǎo)致java.sql.Connection對象的創(chuàng)建,只有當(dāng)?shù)?句sqlSession.selectList("SELECT * FROM STUDENTS"),才會觸發(fā)MyBatis在底層執(zhí)行下面這個方法來創(chuàng)建java.sql.Connection對象:
protected void openConnection() throws SQLException {
if (log.isDebugEnabled()) {
log.debug("Opening JDBC Connection");
}
connection = dataSource.getConnection();
if (level != null) {
connection.setTransactionIsolation(level.getLevel());
}
setDesiredAutoCommit(autoCommmit);
}
不使用連接池的UnpooledDataSource
當(dāng) <dataSource>的type屬性被配置成了”UNPOOLED”,MyBatis首先會實例化一個UnpooledDataSourceFactory工廠實例,然后通過.getDataSource()方法返回一個UnpooledDataSource實例對象引用,我們假定為dataSource。
使用UnpooledDataSource的getConnection(),每調(diào)用一次就會產(chǎn)生一個新的Connection實例對象。
UnPooledDataSource的getConnection()方法實現(xiàn)如下:
/*
UnpooledDataSource的getConnection()實現(xiàn)
*/
public Connection getConnection() throws SQLException
{
return doGetConnection(username, password);
}
private Connection doGetConnection(String username, String password) throws SQLException
{
//封裝username和password成properties
Properties props = new Properties();
if (driverProperties != null)
{
props.putAll(driverProperties);
}
if (username != null)
{
props.setProperty("user", username);
}
if (password != null)
{
props.setProperty("password", password);
}
return doGetConnection(props);
}
/*
* 獲取數(shù)據(jù)連接
*/
private Connection doGetConnection(Properties properties) throws SQLException
{
//1.初始化驅(qū)動
initializeDriver();
//2.從DriverManager中獲取連接,獲取新的Connection對象
Connection connection = DriverManager.getConnection(url, properties);
//3.配置connection屬性
configureConnection(connection);
return connection;
}
如上代碼所示,UnpooledDataSource會做以下事情:
初始化驅(qū)動: 判斷driver驅(qū)動是否已經(jīng)加載到內(nèi)存中,如果還沒有加載,則會動態(tài)地加載driver類,并實例化一個Driver對象,使用DriverManager.registerDriver()方法將其注冊到內(nèi)存中,以供后續(xù)使用。
創(chuàng)建Connection對象: 使用DriverManager.getConnection()方法創(chuàng)建連接。
配置Connection對象: 設(shè)置是否自動提交autoCommit和隔離級別isolationLevel。
返回Connection對象
上述的序列圖如下所示:

總結(jié):從上述的代碼中可以看到,我們每調(diào)用一次getConnection()方法,都會通過DriverManager.getConnection()返回新的java.sql.Connection實例。
為什么要使用連接池?
- 創(chuàng)建一個java.sql.Connection實例對象的代價
首先讓我們來看一下創(chuàng)建一個java.sql.Connection對象的資源消耗。我們通過連接Oracle數(shù)據(jù)庫,創(chuàng)建創(chuàng)建Connection對象,來看創(chuàng)建一個Connection對象、執(zhí)行SQL語句各消耗多長時間。代碼如下:
public static void main(String[] args) throws Exception
{
String sql = "select * from hr.employees where employee_id < ? and employee_id >= ?";
PreparedStatement st = null;
ResultSet rs = null;
long beforeTimeOffset = -1L; //創(chuàng)建Connection對象前時間
long afterTimeOffset = -1L; //創(chuàng)建Connection對象后時間
long executeTimeOffset = -1L; //創(chuàng)建Connection對象后時間
Connection con = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
beforeTimeOffset = new Date().getTime();
System.out.println("before:\t" + beforeTimeOffset);
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "louluan", "123456");
afterTimeOffset = new Date().getTime();
System.out.println("after:\t\t" + afterTimeOffset);
System.out.println("Create Costs:\t\t" + (afterTimeOffset - beforeTimeOffset) + " ms");
st = con.prepareStatement(sql);
//設(shè)置參數(shù)
st.setInt(1, 101);
st.setInt(2, 0);
//查詢,得出結(jié)果集
rs = st.executeQuery();
executeTimeOffset = new Date().getTime();
System.out.println("Exec Costs:\t\t" + (executeTimeOffset - afterTimeOffset) + " ms");
}

從此結(jié)果可以清楚地看出,創(chuàng)建一個Connection對象,用了250 毫秒;而執(zhí)行SQL的時間用了170毫秒。
創(chuàng)建一個Connection對象用了250毫秒!這個時間對計算機來說可以說是一個非常奢侈的!
創(chuàng)建一個java.sql.Connection對象的代價是如此巨大,是因為創(chuàng)建一個Connection對象的過程,在底層就相當(dāng)于和數(shù)據(jù)庫建立的通信連接,在建立通信連接的過程,消耗了這么多的時間,而往往我們建立連接后(即創(chuàng)建Connection對象后),就執(zhí)行一個簡單的SQL語句,然后就要拋棄掉,這是一個非常大的資源浪費!
對于需要頻繁地跟數(shù)據(jù)庫交互的應(yīng)用程序,可以在創(chuàng)建了Connection對象,并操作完數(shù)據(jù)庫后,可以不釋放掉資源,而是將它放到內(nèi)存中,當(dāng)下次需要操作數(shù)據(jù)庫時,可以直接從內(nèi)存中取出Connection對象,不需要再創(chuàng)建了,這樣就極大地節(jié)省了創(chuàng)建Connection對象的資源消耗。由于內(nèi)存也是有限和寶貴的,這又對我們對內(nèi)存中的Connection對象怎么有效地維護提出了很高的要求。我們將在內(nèi)存中存放Connection對象的容器稱之為 連接池(Connection Pool)。下面讓我們來看一下MyBatis的線程池是怎樣實現(xiàn)的。
使用了連接池的PooledDataSource
同樣地,我們也是使用PooledDataSource的getConnection()方法來返回Connection對象。現(xiàn)在讓我們看一下它的基本原理:
PooledDataSource將java.sql.Connection對象包裹成PooledConnection對象放到了PoolState類型的容器中維護。 MyBatis將連接池中的PooledConnection分為兩種狀態(tài): 空閑狀態(tài)(idle)和活動狀態(tài)(active),這兩種狀態(tài)的PooledConnection對象分別被存儲到PoolState容器內(nèi)的idleConnections和activeConnections兩個List集合中:
idleConnections:空閑(idle)狀態(tài)PooledConnection對象被放置到此集合中,表示當(dāng)前閑置的沒有被使用的PooledConnection集合,調(diào)用PooledDataSource的getConnection()方法時,會優(yōu)先從此集合中取PooledConnection對象。當(dāng)用完一個java.sql.Connection對象時,MyBatis會將其包裹成PooledConnection對象放到此集合中。
activeConnections:活動(active)狀態(tài)的PooledConnection對象被放置到名為activeConnections的ArrayList中,表示當(dāng)前正在被使用的PooledConnection集合,調(diào)用PooledDataSource的getConnection()方法時,會優(yōu)先從idleConnections集合中取PooledConnection對象,如果沒有,則看此集合是否已滿,如果未滿,PooledDataSource會創(chuàng)建出一個PooledConnection,添加到此集合中,并返回

獲取java.sql.Connection對象的過程
下面讓我們看一下PooledDataSource 的getConnection()方法獲取Connection對象的實現(xiàn):
public Connection getConnection() throws SQLException {
return popConnection(dataSource.getUsername(), dataSource.getPassword()).getProxyConnection();
}
public Connection getConnection(String username, String password) throws SQLException {
return popConnection(username, password).getProxyConnection();
}
上述的popConnection()方法,會從連接池中返回一個可用的PooledConnection對象,然后再調(diào)用getProxyConnection()方法最終返回Conection對象.
現(xiàn)在讓我們看一下popConnection()方法到底做了什么:
先看是否有空閑(idle)狀態(tài)下的PooledConnection對象,如果有,就直接返回一個可用的PooledConnection對象;否則進行第2步。
查看活動狀態(tài)的PooledConnection池activeConnections是否已滿;如果沒有滿,則創(chuàng)建一個新的PooledConnection對象,然后放到activeConnections池中,然后返回此PooledConnection對象;否則進行第三步;
看最先進入activeConnections池中的PooledConnection對象是否已經(jīng)過期:如果已經(jīng)過期,從activeConnections池中移除此對象,然后創(chuàng)建一個新的PooledConnection對象,添加到activeConnections中,然后將此對象返回;否則進行第4步。
線程等待,循環(huán)2步
protected int poolMaximumActiveConnections = 10;//最大活躍連接
protected int poolMaximumIdleConnections = 5;//最大空閑連接
protected int poolMaximumCheckoutTime = 20000;//最大checkout時長
protected int poolTimeToWait = 20000;//無法獲取鏈接時,線程要等待的時間
protected int poolMaximumLocalBadConnectionTolerance = 3;
/*
* 傳遞一個用戶名和密碼,從連接池中返回可用的PooledConnection
*/
private PooledConnection popConnection(String username, String password) throws SQLException
{
boolean countedWait = false;
PooledConnection conn = null;
long t = System.currentTimeMillis();
int localBadConnectionCount = 0;
while (conn == null)
{
synchronized (state)
{
if (state.idleConnections.size() > 0)
{
// 連接池中有空閑連接,取出第一個
conn = state.idleConnections.remove(0);
if (log.isDebugEnabled())
{
log.debug("Checked out connection " + conn.getRealHashCode() + " from pool.");
}
}
else
{
// 連接池中沒有空閑連接,則取當(dāng)前正在使用的連接數(shù)小于最大限定值,
if (state.activeConnections.size() < poolMaximumActiveConnections)
{
// 創(chuàng)建一個新的connection對象
conn = new PooledConnection(dataSource.getConnection(), this);
@SuppressWarnings("unused")
//used in logging, if enabled
Connection realConn = conn.getRealConnection();
if (log.isDebugEnabled())
{
log.debug("Created connection " + conn.getRealHashCode() + ".");
}
}
else
{
// Cannot create new connection 當(dāng)活動連接池已滿,不能創(chuàng)建時,取出活動連接池的第一個,即最先進入連接池的PooledConnection對象
// 計算它的校驗時間,如果校驗時間大于連接池規(guī)定的最大校驗時間,則認為它已經(jīng)過期了,利用這個PoolConnection內(nèi)部的realConnection重新生成一個PooledConnection
//
PooledConnection oldestActiveConnection = state.activeConnections.get(0);
long longestCheckoutTime = oldestActiveConnection.getCheckoutTime();
if (longestCheckoutTime > poolMaximumCheckoutTime)
{
// Can claim overdue connection
state.claimedOverdueConnectionCount++;
state.accumulatedCheckoutTimeOfOverdueConnections += longestCheckoutTime;
state.accumulatedCheckoutTime += longestCheckoutTime;
state.activeConnections.remove(oldestActiveConnection);
if (!oldestActiveConnection.getRealConnection().getAutoCommit())
{
oldestActiveConnection.getRealConnection().rollback();
}
conn = new PooledConnection(oldestActiveConnection.getRealConnection(), this);
oldestActiveConnection.invalidate();
if (log.isDebugEnabled())
{
log.debug("Claimed overdue connection " + conn.getRealHashCode() + ".");
}
}
else
{
//如果不能釋放,則必須等待有
// Must wait
try
{
if (!countedWait)
{
state.hadToWaitCount++;
countedWait = true;
}
if (log.isDebugEnabled())
{
log.debug("Waiting as long as " + poolTimeToWait + " milliseconds for connection.");
}
long wt = System.currentTimeMillis();
state.wait(poolTimeToWait);
state.accumulatedWaitTime += System.currentTimeMillis() - wt;
}
catch (InterruptedException e)
{
break;
}
}
}
}
//如果獲取PooledConnection成功,則更新其信息
if (conn != null)
{
if (conn.isValid())
{
if (!conn.getRealConnection().getAutoCommit())
{
conn.getRealConnection().rollback();
}
conn.setConnectionTypeCode(assembleConnectionTypeCode(dataSource.getUrl(), username, password));
conn.setCheckoutTimestamp(System.currentTimeMillis());
conn.setLastUsedTimestamp(System.currentTimeMillis());
state.activeConnections.add(conn);
state.requestCount++;
state.accumulatedRequestTime += System.currentTimeMillis() - t;
}
else
{
if (log.isDebugEnabled())
{
log.debug("A bad connection (" + conn.getRealHashCode() + ") was returned from the pool, getting another connection.");
}
state.badConnectionCount++;
localBadConnectionCount++;
conn = null;
if (localBadConnectionCount > (poolMaximumIdleConnections + 3))
{
if (log.isDebugEnabled())
{
log.debug("PooledDataSource: Could not get a good connection to the database.");
}
throw new SQLException("PooledDataSource: Could not get a good connection to the database.");
}
}
}
}
}
if (conn == null)
{
if (log.isDebugEnabled())
{
log.debug("PooledDataSource: Unknown severe error condition. The connection pool returned a null connection.");
}
throw new SQLException("PooledDataSource: Unknown severe error condition. The connection pool returned a null connection.");
}
return conn;
}

如上所示,對于PooledDataSource的getConnection()方法內(nèi),先是調(diào)用類PooledDataSource的popConnection()方法返回了一個PooledConnection對象,然后調(diào)用了PooledConnection的getProxyConnection()來返回Connection對象。
java.sql.Connection對象的回收
當(dāng)我們的程序中使用完Connection對象時,如果不使用數(shù)據(jù)庫連接池,我們一般會調(diào)用 connection.close()方法,關(guān)閉connection連接,釋放資源。如下所示:
private void test() throws ClassNotFoundException, SQLException
{
String sql = "select * from hr.employees where employee_id < ? and employee_id >= ?";
PreparedStatement st = null;
ResultSet rs = null;
Connection con = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
try
{
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "louluan", "123456");
st = con.prepareStatement(sql);
//設(shè)置參數(shù)
st.setInt(1, 101);
st.setInt(2, 0);
//查詢,得出結(jié)果集
rs = st.executeQuery();
//取數(shù)據(jù),省略
//關(guān)閉,釋放資源
con.close();
}
catch (SQLException e)
{
con.close();
e.printStackTrace();
}
}
調(diào)用過close()方法的Connection對象所持有的資源會被全部釋放掉,Connection對象也就不能再使用。
那么,如果我們使用了連接池,我們在用完了Connection對象時,需要將它放在連接池中,該怎樣做呢?
可能大家第一個在腦海里閃現(xiàn)出來的想法就是:我在應(yīng)該調(diào)用con.close()方法的時候,不調(diào)用close()f方法,將其換成將Connection對象放到連接池容器中的代碼!
好,我們將上述的想法實現(xiàn),首先定義一個簡易連接池Pool,然后將上面的代碼改寫:
package com.foo.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;
/**
*
* 一個線程安全的簡易連接池實現(xiàn),此連接池是單例的
* putConnection()將Connection添加到連接池中
* getConnection()返回一個Connection對象
*/
public class Pool {
private static Vector<Connection> pool = new Vector<Connection>();
private static int MAX_CONNECTION =100;
private static String DRIVER="oracle.jdbc.driver.OracleDriver";
private static String URL = "jdbc:oracle:thin:@127.0.0.1:1521:xe";
private static String USERNAME = "louluan";
private static String PASSWROD = "123456";
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* 將一個Connection對象放置到連接池中
*/
public static void putConnection(Connection connection){
synchronized(pool)
{
if(pool.size()<MAX_CONNECTION)
{
pool.add(connection);
}
}
}
/**
* 返回一個Connection對象,如果連接池內(nèi)有元素,則pop出第一個元素;
* 如果連接池Pool中沒有元素,則創(chuàng)建一個connection對象,然后添加到pool中
* @return Connection
*/
public static Connection getConnection(){
Connection connection = null;
synchronized(pool)
{
if(pool.size()>0)
{
connection = pool.get(0);
pool.remove(0);
}
else
{
connection = createConnection();
pool.add(connection);
}
}
return connection;
}
/**
* 創(chuàng)建一個新的Connection對象
*/
private static Connection createConnection()
{
Connection connection = null;
try {
connection = DriverManager.getConnection(URL, USERNAME,PASSWROD);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
}
package com.foo.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
public class PoolTest
{
private void test() throws ClassNotFoundException, SQLException
{
String sql = "select * from hr.employees where employee_id < ? and employee_id >= ?";
PreparedStatement st = null;
ResultSet rs = null;
Connection con = null;
Class.forName("oracle.jdbc.driver.OracleDriver");
try
{
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "louluan", "123456");
st = con.prepareStatement(sql);
//設(shè)置參數(shù)
st.setInt(1, 101);
st.setInt(2, 0);
//查詢,得出結(jié)果集
rs = st.executeQuery();
//取數(shù)據(jù),省略
//將不再使用的Connection對象放到連接池中,供以后使用
Pool.putConnection(con);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
上述的代碼就是將我們使用過的Connection對象放到Pool連接池中,我們需要Connection對象的話,只需要使用Pool.getConnection()方法從里面取即可。
是的,上述的代碼完全可以實現(xiàn)此能力,不過有一個很不優(yōu)雅的實現(xiàn):就是我們需要手動地將Connection對象放到Pool連接池中,這是一個很傻的實現(xiàn)方式。這也和一般使用Connection對象的方式不一樣:一般使用Connection的方式是使用完后,然后調(diào)用.close()方法釋放資源。
為了和一般的使用Conneciton對象的方式保持一致,我們希望當(dāng)Connection使用完后,調(diào)用.close()方法,而實際上Connection資源并沒有被釋放,而實際上被添加到了連接池中。這樣可以做到嗎?答案是可以。上述的要求從另外一個角度來描述就是:能否提供一種機制,讓我們知道Connection對象調(diào)用了什么方法,從而根據(jù)不同的方法自定義相應(yīng)的處理機制。恰好代理機制就可以完成上述要求.
怎樣實現(xiàn)Connection對象調(diào)用了close()方法,而實際是將其添加到連接池中
這是要使用代理模式,為真正的Connection對象創(chuàng)建一個代理對象,代理對象所有的方法都是調(diào)用相應(yīng)的真正Connection對象的方法實現(xiàn)。當(dāng)代理對象執(zhí)行close()方法時,要特殊處理,不調(diào)用真正Connection對象的close()方法,而是將Connection對象添加到連接池中。
MyBatis的PooledDataSource的PoolState內(nèi)部維護的對象是PooledConnection類型的對象,而PooledConnection則是對真正的數(shù)據(jù)庫連接java.sql.Connection實例對象的包裹器。
PooledConnection對象內(nèi)持有一個真正的數(shù)據(jù)庫連接java.sql.Connection實例對象和一個java.sql.Connection的代理:
class PooledConnection implements InvocationHandler {
//......
//所創(chuàng)建它的datasource引用
private PooledDataSource dataSource;
//真正的Connection對象
private Connection realConnection;
//代理自己的代理Connection
private Connection proxyConnection;
//......
}
PooledConenction實現(xiàn)了InvocationHandler接口,并且,proxyConnection對象也是根據(jù)這個它來生成的代理對象:
public PooledConnection(Connection connection, PooledDataSource dataSource) {
this.hashCode = connection.hashCode();
this.realConnection = connection;
this.dataSource = dataSource;
this.createdTimestamp = System.currentTimeMillis();
this.lastUsedTimestamp = System.currentTimeMillis();
this.valid = true;
this.proxyConnection = (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(), IFACES, this);
}
實際上,我們調(diào)用PooledDataSource的getConnection()方法返回的就是這個proxyConnection對象。
當(dāng)我們調(diào)用此proxyConnection對象上的任何方法時,都會調(diào)用PooledConnection對象內(nèi)invoke()方法。
讓我們看一下PooledConnection類中的invoke()方法定義:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName = method.getName();
//當(dāng)調(diào)用關(guān)閉的時候,回收此Connection到PooledDataSource中
if (CLOSE.hashCode() == methodName.hashCode() && CLOSE.equals(methodName)) {
dataSource.pushConnection(this);
return null;
} else {
try {
if (!Object.class.equals(method.getDeclaringClass())) {
checkConnection();
}
return method.invoke(realConnection, args);
} catch (Throwable t) {
throw ExceptionUtil.unwrapThrowable(t);
}
}
}
從上述代碼可以看到,當(dāng)我們使用了pooledDataSource.getConnection()返回的Connection對象的close()方法時,不會調(diào)用真正Connection的close()方法,而是將此Connection對象放到連接池中。