Mybatis技術內幕(2.4):數據源模塊

基於Mybatis-3.5.0版本html

1.0 數據源模塊

  在數據持久層中,數據源是一個很是重要的組件,期性能直接關係到整個數據持久層的性能。在實踐中比較常見的第三方數據源組件有DBCP、C3P0、阿里druid和Springboot2推薦的HikariCP等,Mybatis不只能夠集成第三方數據源組件,還提供了本身的數據源實現。java

Mybatis數據源模塊包結構以下: sql

2.0 DataSource 數據源接口

  常見的數據源組件都實現了javax.sql.DataSource接口,Mybatis自身實現的數據源實現也不例外。Mybatis提供了兩個javax.sql.DataSource接口實現,分別是PooledDataSource和UnpooledDataSource。數據庫

2.1 UnpooledDataSource 非池化DataSource

org.apache.ibatis.datasource.unpooled.UnpooledDataSource實現DataSource接口,非池化的DataSource對象。代碼以下:apache

/** * 非池化DataSource * @author Clinton Begin * @author Eduardo Macarron */
public class UnpooledDataSource implements DataSource {
	// Driver 類加載器
	private ClassLoader driverClassLoader;
	// 數據庫驅動的相關配置
	private Properties driverProperties;
	// 緩存全部已註冊的數據庫鏈接驅動
	private static Map<String, Driver> registeredDrivers = new ConcurrentHashMap<>();

	// 數據庫鏈接的驅動名稱
	private String driver;
	// 數據庫 URL
	private String url;
	// 用戶名
	private String username;
	// 密碼
	private String password;

	// 是否自動提交事務
	private Boolean autoCommit;
	// 默認事務隔離級別
	private Integer defaultTransactionIsolationLevel;

	/** * 初始化 registeredDrivers * 將已在DriverManager中註冊的JDBC Driver複製一份 */
	static {
		Enumeration<Driver> drivers = DriverManager.getDrivers();
		while (drivers.hasMoreElements()) {
			Driver driver = drivers.nextElement();
			registeredDrivers.put(driver.getClass().getName(), driver);
		}
	}

	public UnpooledDataSource() {
	}

	public UnpooledDataSource(String driver, String url, String username, String password) {
		this.driver = driver;
		this.url = url;
		this.username = username;
		this.password = password;
	}

	public UnpooledDataSource(String driver, String url, Properties driverProperties) {
		this.driver = driver;
		this.url = url;
		this.driverProperties = driverProperties;
	}

	public UnpooledDataSource(ClassLoader driverClassLoader, String driver, String url, String username, String password) {
		this.driverClassLoader = driverClassLoader;
		this.driver = driver;
		this.url = url;
		this.username = username;
		this.password = password;
	}

	public UnpooledDataSource(ClassLoader driverClassLoader, String driver, String url, Properties driverProperties) {
		this.driverClassLoader = driverClassLoader;
		this.driver = driver;
		this.url = url;
		this.driverProperties = driverProperties;
	}

	@Override
	public Connection getConnection() throws SQLException {
		return doGetConnection(username, password);
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		return doGetConnection(username, password);
	}

	@Override
	public void setLoginTimeout(int loginTimeout) {
		DriverManager.setLoginTimeout(loginTimeout);
	}

	@Override
	public int getLoginTimeout() {
		return DriverManager.getLoginTimeout();
	}

	@Override
	public void setLogWriter(PrintWriter logWriter) {
		DriverManager.setLogWriter(logWriter);
	}

	@Override
	public PrintWriter getLogWriter() {
		return DriverManager.getLogWriter();
	}

	public ClassLoader getDriverClassLoader() {
		return driverClassLoader;
	}

	public void setDriverClassLoader(ClassLoader driverClassLoader) {
		this.driverClassLoader = driverClassLoader;
	}

	public Properties getDriverProperties() {
		return driverProperties;
	}

	public void setDriverProperties(Properties driverProperties) {
		this.driverProperties = driverProperties;
	}

	public String getDriver() {
		return driver;
	}

	public synchronized void setDriver(String driver) {
		this.driver = driver;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Boolean isAutoCommit() {
		return autoCommit;
	}

	public void setAutoCommit(Boolean autoCommit) {
		this.autoCommit = autoCommit;
	}

	public Integer getDefaultTransactionIsolationLevel() {
		return defaultTransactionIsolationLevel;
	}

	public void setDefaultTransactionIsolationLevel(Integer defaultTransactionIsolationLevel) {
		this.defaultTransactionIsolationLevel = defaultTransactionIsolationLevel;
	}

	private Connection doGetConnection(String username, String password) throws SQLException {
		// 建立 Properties 對象
		Properties props = new Properties();
		if (driverProperties != null) {
			// 設置 driverProperties 到 props 中
			props.putAll(driverProperties);
		}
		// 設置 user和password
		if (username != null) {
			props.setProperty("user", username);
		}
		if (password != null) {
			props.setProperty("password", password);
		}
		return doGetConnection(props);
	}

	private Connection doGetConnection(Properties properties) throws SQLException {
		// 初始化 Driver
		initializeDriver();
		// 得到 Connection 對象
		Connection connection = DriverManager.getConnection(url, properties);
		// 配置 Connection 對象
		configureConnection(connection);
		return connection;
	}

	/** * 初始化 Driver * * @throws SQLException */
	private synchronized void initializeDriver() throws SQLException {
		// 檢查驅動是否已註冊
		if (!registeredDrivers.containsKey(driver)) {
			Class<?> driverType;
			try {
				// 得到 driver 類
				if (driverClassLoader != null) {
					driverType = Class.forName(driver, true, driverClassLoader);
				} else {
					driverType = Resources.classForName(driver);
				}
				// DriverManager requires the driver to be loaded via the system ClassLoader.
				// http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
				// 建立 Driver 對象
				Driver driverInstance = (Driver) driverType.newInstance();
				// 建立 DriverProxy對象,並註冊到 DriverManager中
				DriverManager.registerDriver(new DriverProxy(driverInstance));
				// 添加到registeredDrivers中
				registeredDrivers.put(driver, driverInstance);
			} catch (Exception e) {
				throw new SQLException("Error setting driver on UnpooledDataSource. Cause: " + e);
			}
		}
	}

	private void configureConnection(Connection conn) throws SQLException {
		// 設置自動提交
		if (autoCommit != null && autoCommit != conn.getAutoCommit()) {
			conn.setAutoCommit(autoCommit);
		}
		// 設置事務隔離級別
		if (defaultTransactionIsolationLevel != null) {
			conn.setTransactionIsolation(defaultTransactionIsolationLevel);
		}
	}

	/** * 靜態代理 * @ClassName: DriverProxy * @Description: 主要是針對getParentLogger方法,使用 MyBatis 自定義的 Logger 對象 * @date 2019年4月3日 * */
	private static class DriverProxy implements Driver {
		private Driver driver;

		DriverProxy(Driver d) {
			this.driver = d;
		}

		@Override
		public boolean acceptsURL(String u) throws SQLException {
			return this.driver.acceptsURL(u);
		}

		@Override
		public Connection connect(String u, Properties p) throws SQLException {
			return this.driver.connect(u, p);
		}

		@Override
		public int getMajorVersion() {
			return this.driver.getMajorVersion();
		}

		@Override
		public int getMinorVersion() {
			return this.driver.getMinorVersion();
		}

		@Override
		public DriverPropertyInfo[] getPropertyInfo(String u, Properties p) throws SQLException {
			return this.driver.getPropertyInfo(u, p);
		}

		@Override
		public boolean jdbcCompliant() {
			return this.driver.jdbcCompliant();
		}

		// @Override only valid jdk7+
		public Logger getParentLogger() {
			return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
		}
	}

	@Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		throw new SQLException(getClass().getName() + " is not a wrapper.");
	}

	@Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return false;
	}

	// @Override only valid jdk7+
	public Logger getParentLogger() {
		// requires JDK version 1.6
		return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
	}
}
複製代碼

2.2 PooledDataSource 池化的DataSource

  數據庫鏈接的建立過程是很是耗時的,數據庫可以創建的鏈接也很是有限,因此在絕大多數系統中,數據庫鏈接都是很是珍貴的資源,使用數據庫鏈接池就顯得尤爲必要。
  org.apache.ibatis.datasource.pooled.PooledDataSource實現DataSource接口,池化的DataSource實現類。代碼以下:緩存

public class PooledDataSource implements DataSource {

	private static final Log log = LogFactory.getLog(PooledDataSource.class);

	private final PoolState state = new PoolState(this);
	// UnpooledDataSource對象,用於獲取數據庫鏈接
	private final UnpooledDataSource dataSource;

	// OPTIONAL CONFIGURATION FIELDS
	// 最大活動鏈接數(默認爲10)
	protected int poolMaximumActiveConnections = 10;
	// 最大空閒鏈接數(默認爲5)
	protected int poolMaximumIdleConnections = 5;
	/** * 最大可回收時間。 * 即當達到最大活動連接數時,此時若是有程序獲取鏈接,則檢查最早使用的鏈接,看其是否超出了該時間,若是超出了該時間,則能夠回收該鏈接。(默認20s) */
	protected int poolMaximumCheckoutTime = 20000;
	// 沒有鏈接時,重嘗試獲取鏈接以及打印日誌的時間間隔(默認20s)
	protected int poolTimeToWait = 20000;
	/** * 這是一個關於壞鏈接容忍度的底層設置, 做用於每個嘗試從緩存池獲取鏈接的線程. * 若是這個線程獲取到的是一個壞的鏈接,那麼這個數據源容許這個線程嘗試從新獲取一個新的鏈接,可是這個從新嘗試的次數不該該超過poolMaximumIdleConnections與poolMaximumLocalBadConnectionTolerance之和 */
	protected int poolMaximumLocalBadConnectionTolerance = 3;
	// 檢查鏈接正確的語句,默認爲"NO PING QUERY SET",即沒有,使用會致使拋異常
	protected String poolPingQuery = "NO PING QUERY SET";
	// 是否開啓ping檢測,(默認:false)
	protected boolean poolPingEnabled;
	// 設置ping檢測時間間隔,一般用於檢測超時鏈接(默認爲0,即當開啓檢測後每次從鏈接詞中獲取鏈接以及放回鏈接池都須要檢測)
	protected int poolPingConnectionsNotUsedFor;
	
	// 指望 Connection 的類型編碼
	private int expectedConnectionTypeCode;

	public PooledDataSource() {
		dataSource = new UnpooledDataSource();
	}

	public PooledDataSource(UnpooledDataSource dataSource) {
		this.dataSource = dataSource;
	}

	public PooledDataSource(String driver, String url, String username, String password) {
		// 建立 UnpooledDataSource 對象
		dataSource = new UnpooledDataSource(driver, url, username, password);
		// 計算 expectedConnectionTypeCode 的值
		expectedConnectionTypeCode = assembleConnectionTypeCode(dataSource.getUrl(), dataSource.getUsername(),
				dataSource.getPassword());
	}

	public PooledDataSource(String driver, String url, Properties driverProperties) {
		dataSource = new UnpooledDataSource(driver, url, driverProperties);
		expectedConnectionTypeCode = assembleConnectionTypeCode(dataSource.getUrl(), dataSource.getUsername(),
				dataSource.getPassword());
	}

	public PooledDataSource(ClassLoader driverClassLoader, String driver, String url, String username, String password) {
		dataSource = new UnpooledDataSource(driverClassLoader, driver, url, username, password);
		expectedConnectionTypeCode = assembleConnectionTypeCode(dataSource.getUrl(), dataSource.getUsername(),
				dataSource.getPassword());
	}

	public PooledDataSource(ClassLoader driverClassLoader, String driver, String url, Properties driverProperties) {
		dataSource = new UnpooledDataSource(driverClassLoader, driver, url, driverProperties);
		expectedConnectionTypeCode = assembleConnectionTypeCode(dataSource.getUrl(), dataSource.getUsername(),
				dataSource.getPassword());
	}

	@Override
	public Connection getConnection() throws SQLException {
		return popConnection(dataSource.getUsername(), dataSource.getPassword()).getProxyConnection();
	}

	@Override
	public Connection getConnection(String username, String password) throws SQLException {
		return popConnection(username, password).getProxyConnection();
	}

	@Override
	public void setLoginTimeout(int loginTimeout) {
		DriverManager.setLoginTimeout(loginTimeout);
	}

	@Override
	public int getLoginTimeout() {
		return DriverManager.getLoginTimeout();
	}

	@Override
	public void setLogWriter(PrintWriter logWriter) {
		DriverManager.setLogWriter(logWriter);
	}

	@Override
	public PrintWriter getLogWriter() {
		return DriverManager.getLogWriter();
	}

	public void setDriver(String driver) {
		dataSource.setDriver(driver);
		forceCloseAll();
	}

	public void setUrl(String url) {
		dataSource.setUrl(url);
		forceCloseAll();
	}

	public void setUsername(String username) {
		dataSource.setUsername(username);
		forceCloseAll();
	}

	public void setPassword(String password) {
		dataSource.setPassword(password);
		forceCloseAll();
	}

	public void setDefaultAutoCommit(boolean defaultAutoCommit) {
		dataSource.setAutoCommit(defaultAutoCommit);
		forceCloseAll();
	}

	public void setDefaultTransactionIsolationLevel(Integer defaultTransactionIsolationLevel) {
		dataSource.setDefaultTransactionIsolationLevel(defaultTransactionIsolationLevel);
		forceCloseAll();
	}

	public void setDriverProperties(Properties driverProps) {
		dataSource.setDriverProperties(driverProps);
		forceCloseAll();
	}

	/** * The maximum number of active connections * * @param poolMaximumActiveConnections The maximum number of active connections */
	public void setPoolMaximumActiveConnections(int poolMaximumActiveConnections) {
		this.poolMaximumActiveConnections = poolMaximumActiveConnections;
		forceCloseAll();
	}

	/** * The maximum number of idle connections * * @param poolMaximumIdleConnections The maximum number of idle connections */
	public void setPoolMaximumIdleConnections(int poolMaximumIdleConnections) {
		this.poolMaximumIdleConnections = poolMaximumIdleConnections;
		forceCloseAll();
	}

	/** * The maximum number of tolerance for bad connection happens in one thread * which are applying for new {@link PooledConnection} * * @param poolMaximumLocalBadConnectionTolerance max tolerance for bad * connection happens in one * thread * * @since 3.4.5 */
	public void setPoolMaximumLocalBadConnectionTolerance(int poolMaximumLocalBadConnectionTolerance) {
		this.poolMaximumLocalBadConnectionTolerance = poolMaximumLocalBadConnectionTolerance;
	}

	/** * The maximum time a connection can be used before it *may* be given away * again. * * @param poolMaximumCheckoutTime The maximum time */
	public void setPoolMaximumCheckoutTime(int poolMaximumCheckoutTime) {
		this.poolMaximumCheckoutTime = poolMaximumCheckoutTime;
		forceCloseAll();
	}

	/** * The time to wait before retrying to get a connection * * @param poolTimeToWait The time to wait */
	public void setPoolTimeToWait(int poolTimeToWait) {
		this.poolTimeToWait = poolTimeToWait;
		forceCloseAll();
	}

	/** * The query to be used to check a connection * * @param poolPingQuery The query */
	public void setPoolPingQuery(String poolPingQuery) {
		this.poolPingQuery = poolPingQuery;
		forceCloseAll();
	}

	/** * Determines if the ping query should be used. * * @param poolPingEnabled True if we need to check a connection before using it */
	public void setPoolPingEnabled(boolean poolPingEnabled) {
		this.poolPingEnabled = poolPingEnabled;
		forceCloseAll();
	}

	/** * If a connection has not been used in this many milliseconds, ping the * database to make sure the connection is still good. * * @param milliseconds the number of milliseconds of inactivity that will * trigger a ping */
	public void setPoolPingConnectionsNotUsedFor(int milliseconds) {
		this.poolPingConnectionsNotUsedFor = milliseconds;
		forceCloseAll();
	}

	public String getDriver() {
		return dataSource.getDriver();
	}

	public String getUrl() {
		return dataSource.getUrl();
	}

	public String getUsername() {
		return dataSource.getUsername();
	}

	public String getPassword() {
		return dataSource.getPassword();
	}

	public boolean isAutoCommit() {
		return dataSource.isAutoCommit();
	}

	public Integer getDefaultTransactionIsolationLevel() {
		return dataSource.getDefaultTransactionIsolationLevel();
	}

	public Properties getDriverProperties() {
		return dataSource.getDriverProperties();
	}

	public int getPoolMaximumActiveConnections() {
		return poolMaximumActiveConnections;
	}

	public int getPoolMaximumIdleConnections() {
		return poolMaximumIdleConnections;
	}

	public int getPoolMaximumLocalBadConnectionTolerance() {
		return poolMaximumLocalBadConnectionTolerance;
	}

	public int getPoolMaximumCheckoutTime() {
		return poolMaximumCheckoutTime;
	}

	public int getPoolTimeToWait() {
		return poolTimeToWait;
	}

	public String getPoolPingQuery() {
		return poolPingQuery;
	}

	public boolean isPoolPingEnabled() {
		return poolPingEnabled;
	}

	public int getPoolPingConnectionsNotUsedFor() {
		return poolPingConnectionsNotUsedFor;
	}

	/* * Closes all active and idle connections in the pool */
	public void forceCloseAll() {
		synchronized (state) {
			expectedConnectionTypeCode = assembleConnectionTypeCode(dataSource.getUrl(), dataSource.getUsername(),
					dataSource.getPassword());
			for (int i = state.activeConnections.size(); i > 0; i--) {
				try {
					PooledConnection conn = state.activeConnections.remove(i - 1);
					conn.invalidate();

					Connection realConn = conn.getRealConnection();
					if (!realConn.getAutoCommit()) {
						realConn.rollback();
					}
					realConn.close();
				} catch (Exception e) {
					// ignore
				}
			}
			for (int i = state.idleConnections.size(); i > 0; i--) {
				try {
					PooledConnection conn = state.idleConnections.remove(i - 1);
					conn.invalidate();

					Connection realConn = conn.getRealConnection();
					if (!realConn.getAutoCommit()) {
						realConn.rollback();
					}
					realConn.close();
				} catch (Exception e) {
					// ignore
				}
			}
		}
		if (log.isDebugEnabled()) {
			log.debug("PooledDataSource forcefully closed/removed all connections.");
		}
	}

	public PoolState getPoolState() {
		return state;
	}

	private int assembleConnectionTypeCode(String url, String username, String password) {
		return ("" + url + username + password).hashCode();
	}

	protected void pushConnection(PooledConnection conn) throws SQLException {
		synchronized (state) {
			// 從activeConnections集合中移除該PooledConnection對象
			state.activeConnections.remove(conn);
			if (conn.isValid()) {// 檢測PooledConnection對象是否有效
				// 檢測空閒鏈接數是否已達到上限,以及PooledConnection是否爲該鏈接池的鏈接
				if (state.idleConnections.size() < poolMaximumIdleConnections
						&& conn.getConnectionTypeCode() == expectedConnectionTypeCode) {
					// 累積checkout時長
					state.accumulatedCheckoutTime += conn.getCheckoutTime();
					if (!conn.getRealConnection().getAutoCommit()) {// 回滾未提交的事務
						conn.getRealConnection().rollback();
					}
					// 爲返回的鏈接建立新的PooledConnection對象
					PooledConnection newConn = new PooledConnection(conn.getRealConnection(), this);
					state.idleConnections.add(newConn);// 添加到idleConnections集合
					newConn.setCreatedTimestamp(conn.getCreatedTimestamp());
					newConn.setLastUsedTimestamp(conn.getLastUsedTimestamp());
					conn.invalidate();// 將原PooledConnection對象設置爲無效
					if (log.isDebugEnabled()) {
						log.debug("Returned connection " + newConn.getRealHashCode() + " to pool.");
					}
					state.notifyAll();// 喚醒阻塞等待的線程
				} else {
					// 累積checkout時長
					state.accumulatedCheckoutTime += conn.getCheckoutTime();
					if (!conn.getRealConnection().getAutoCommit()) {
						conn.getRealConnection().rollback();
					}
					// 關閉真正的數據庫鏈接
					conn.getRealConnection().close();
					if (log.isDebugEnabled()) {
						log.debug("Closed connection " + conn.getRealHashCode() + ".");
					}
					conn.invalidate();// 將原PooledConnection對象設置爲無效
				}
			} else {
				if (log.isDebugEnabled()) {
					log.debug("A bad connection (" + conn.getRealHashCode()
							+ ") attempted to return to the pool, discarding connection.");
				}
				state.badConnectionCount++;
			}
		}
	}

	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.isEmpty()) {// 檢測空閒鏈接
					// Pool has available connection
					conn = state.idleConnections.remove(0);// 獲取鏈接
					if (log.isDebugEnabled()) {
						log.debug("Checked out connection " + conn.getRealHashCode() + " from pool.");
					}
				} else {// 當前鏈接池沒有空閒
					// Pool does not have available connection
					// 活躍的鏈接數沒有到最大值,則能夠建立新鏈接
					if (state.activeConnections.size() < poolMaximumActiveConnections) {
						// Can create new connection
						// 建立新數據庫鏈接,並封裝成PooledConnection對象
						conn = new PooledConnection(dataSource.getConnection(), this);
						if (log.isDebugEnabled()) {
							log.debug("Created connection " + conn.getRealHashCode() + ".");
						}
					} else {// 活躍鏈接數已到最大值,則不能穿件新的鏈接
						// Cannot create new connection
						// 獲取最早建立的活躍鏈接
						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;
							// 將超時列檢移出activeConnections集合
							state.activeConnections.remove(oldestActiveConnection);
							// 若是超時鏈接未提交,則自動回滾
							if (!oldestActiveConnection.getRealConnection().getAutoCommit()) {
								try {
									oldestActiveConnection.getRealConnection().rollback();
								} catch (SQLException e) {
									/* * Just log a message for debug and continue to execute the following statement * like nothing happened. Wrap the bad connection with a new PooledConnection, * this will help to not interrupt current executing thread and give current * thread a chance to join the next competition for another valid/good database * connection. At the end of this loop, bad {@link @conn} will be set as null. */
									log.debug("Bad connection. Could not roll back");
								}
							}
							// 建立新PooledConnection對象,可是真正的數據庫鏈接並未建立新的
							conn = new PooledConnection(oldestActiveConnection.getRealConnection(), this);
							conn.setCreatedTimestamp(oldestActiveConnection.getCreatedTimestamp());
							conn.setLastUsedTimestamp(oldestActiveConnection.getLastUsedTimestamp());
							// 將超時PooledConnection設置爲無效
							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;
							}
						}
					}
				}
				if (conn != null) {
					// ping to server and check the connection is valid or not
					if (conn.isValid()) {// 檢測PooledConnection是否有效
						// 若是非自動提交的,須要進行回滾。即將原有執行中的事務,所有回滾。
						if (!conn.getRealConnection().getAutoCommit()) {
							conn.getRealConnection().rollback();
						}
						// 配置PooledConnection的相關屬性
						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置空,繼續獲取
						conn = null;
						// 若是超過最大次數,拋出 SQLException 異常
						if (localBadConnectionCount > (poolMaximumIdleConnections
								+ poolMaximumLocalBadConnectionTolerance)) {
							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;
	}

	/** * Method to check to see if a connection is still usable * * @param conn - the connection to check * @return True if the connection is still usable */
	protected boolean pingConnection(PooledConnection conn) {
		boolean result = true;// 記錄ping操做是否成功

		try {
			//檢測真正的數據庫鏈接是否已關閉
			result = !conn.getRealConnection().isClosed();
		} catch (SQLException e) {
			if (log.isDebugEnabled()) {
				log.debug("Connection " + conn.getRealHashCode() + " is BAD: " + e.getMessage());
			}
			result = false;
		}

		if (result) {
			if (poolPingEnabled) {// 檢測poolPingEnabled設置,是否運行執行測試SQL語句
				// 長時間未使用的鏈接,才須要ping操做來檢測數據庫鏈接是否正常
				if (poolPingConnectionsNotUsedFor >= 0
						&& conn.getTimeElapsedSinceLastUse() > poolPingConnectionsNotUsedFor) {
					try {
						if (log.isDebugEnabled()) {
							log.debug("Testing connection " + conn.getRealHashCode() + " ...");
						}
						// 執行測試SQL語句
						Connection realConn = conn.getRealConnection();
						try (Statement statement = realConn.createStatement()) {
							statement.executeQuery(poolPingQuery).close();
						}
						if (!realConn.getAutoCommit()) {
							realConn.rollback();
						}
						result = true;
						if (log.isDebugEnabled()) {
							log.debug("Connection " + conn.getRealHashCode() + " is GOOD!");
						}
					} catch (Exception e) {
						log.warn("Execution of ping query '" + poolPingQuery + "' failed: " + e.getMessage());
						try {
							conn.getRealConnection().close();
						} catch (Exception e2) {
							// ignore
						}
						result = false;
						if (log.isDebugEnabled()) {
							log.debug("Connection " + conn.getRealHashCode() + " is BAD: " + e.getMessage());
						}
					}
				}
			}
		}
		return result;
	}

	/** * Unwraps a pooled connection to get to the 'real' connection * * @param conn - the pooled connection to unwrap * @return The 'real' connection */
	public static Connection unwrapConnection(Connection conn) {
		if (Proxy.isProxyClass(conn.getClass())) {
			InvocationHandler handler = Proxy.getInvocationHandler(conn);
			if (handler instanceof PooledConnection) {
				return ((PooledConnection) handler).getRealConnection();
			}
		}
		return conn;
	}

	protected void finalize() throws Throwable {
		forceCloseAll();
		super.finalize();
	}

	public <T> T unwrap(Class<T> iface) throws SQLException {
		throw new SQLException(getClass().getName() + " is not a wrapper.");
	}

	public boolean isWrapperFor(Class<?> iface) {
		return false;
	}

	public Logger getParentLogger() {
		return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); // requires JDK version 1.6
	}
}
複製代碼

popConnection方法獲取鏈接,流程以下:app

pushConnection方法關閉鏈接,流程以下:ide

2.2.1 PooledConnection

  從PooledDataSource並不會直接管理java.sql.Connection對象,而是管理PooledConnection對象。在PooledConnection中封裝了真正的數據庫鏈接對象以及其代理對象,這裏的代理對象是經過JDK動態代理產生的。 org.apache.ibatis.datasource.pooled.PooledConnection實現 InvocationHandler 接口,池化的Connection對象,代碼以下:oop

/** * @author Clinton Begin */
class PooledConnection implements InvocationHandler {

	// 關閉 Connection 方法名
	private static final String CLOSE = "close";
	// JDK Proxy 的接口
	private static final Class<?>[] IFACES = new Class<?>[] { Connection.class };

	private final int hashCode;
	/** * 所屬的 PooledDataSource對象 * 該PooledConnection是從該PooledDataSource中獲取的; * 當調用close()方法時會將該PooledConnection放回該PooledDataSource中 */
	private final PooledDataSource dataSource;
	// 真正的數據庫鏈接
	private final Connection realConnection;
	// 數據庫鏈接的代理對象
	private final Connection proxyConnection;
	// 從鏈接池中取出該鏈接的時間戳
	private long checkoutTimestamp;
	// 該鏈接建立的時間戳
	private long createdTimestamp;
	// 最後一次使用的時間戳
	private long lastUsedTimestamp;
	// 由數據庫URL、用戶名和密碼計算出來的hash值,用於標識該鏈接所在的鏈接池
	private int connectionTypeCode;
	/** * 檢測當前PooledConnection是否有效 * 防止程序經過close()方法將鏈接歸還給鏈接池以後,依然經過該鏈接操做數據庫 */
	private boolean valid;

	/** * Constructor for SimplePooledConnection that uses the Connection and * PooledDataSource passed in * * @param connection - the connection that is to be presented as a pooled * connection * @param dataSource - the dataSource that the connection is from */
	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;
		// 建立代理的 Connection 對象
		this.proxyConnection = (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(), IFACES, this);
	}

	/** * Invalidates the connection */
	public void invalidate() {
		valid = false;
	}

	/** * Method to see if the connection is usable * * @return True if the connection is usable */
	public boolean isValid() {
		return valid && realConnection != null && dataSource.pingConnection(this);
	}

	/** * Getter for the *real* connection that this wraps * * @return The connection */
	public Connection getRealConnection() {
		return realConnection;
	}

	/** * Getter for the proxy for the connection * * @return The proxy */
	public Connection getProxyConnection() {
		return proxyConnection;
	}

	/** * Gets the hashcode of the real connection (or 0 if it is null) * * @return The hashcode of the real connection (or 0 if it is null) */
	public int getRealHashCode() {
		return realConnection == null ? 0 : realConnection.hashCode();
	}

	/** * Getter for the connection type (based on url + user + password) * * @return The connection type */
	public int getConnectionTypeCode() {
		return connectionTypeCode;
	}

	/** * Setter for the connection type * * @param connectionTypeCode - the connection type */
	public void setConnectionTypeCode(int connectionTypeCode) {
		this.connectionTypeCode = connectionTypeCode;
	}

	/** * Getter for the time that the connection was created * * @return The creation timestamp */
	public long getCreatedTimestamp() {
		return createdTimestamp;
	}

	/** * Setter for the time that the connection was created * * @param createdTimestamp - the timestamp */
	public void setCreatedTimestamp(long createdTimestamp) {
		this.createdTimestamp = createdTimestamp;
	}

	/** * Getter for the time that the connection was last used * * @return - the timestamp */
	public long getLastUsedTimestamp() {
		return lastUsedTimestamp;
	}

	/** * Setter for the time that the connection was last used * * @param lastUsedTimestamp - the timestamp */
	public void setLastUsedTimestamp(long lastUsedTimestamp) {
		this.lastUsedTimestamp = lastUsedTimestamp;
	}

	/** * Getter for the time since this connection was last used * * @return - the time since the last use */
	public long getTimeElapsedSinceLastUse() {
		return System.currentTimeMillis() - lastUsedTimestamp;
	}

	/** * Getter for the age of the connection * * @return the age */
	public long getAge() {
		return System.currentTimeMillis() - createdTimestamp;
	}

	/** * Getter for the timestamp that this connection was checked out * * @return the timestamp */
	public long getCheckoutTimestamp() {
		return checkoutTimestamp;
	}

	/** * Setter for the timestamp that this connection was checked out * * @param timestamp the timestamp */
	public void setCheckoutTimestamp(long timestamp) {
		this.checkoutTimestamp = timestamp;
	}

	/** * Getter for the time that this connection has been checked out * * @return the time */
	public long getCheckoutTime() {
		return System.currentTimeMillis() - checkoutTimestamp;
	}

	@Override
	public int hashCode() {
		return hashCode;
	}

	/** * Allows comparing this connection to another * * @param obj - the other connection to test for equality * @see Object#equals(Object) */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof PooledConnection) {
			return realConnection.hashCode() == ((PooledConnection) obj).realConnection.hashCode();
		} else if (obj instanceof Connection) {
			return hashCode == obj.hashCode();
		} else {
			return false;
		}
	}

	/** * Required for InvocationHandler implementation. * * @param proxy - not used * @param method - the method to be executed * @param args - the parameters to be passed to the method * @see java.lang.reflect.InvocationHandler#invoke(Object, * java.lang.reflect.Method, Object[]) */
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		String methodName = method.getName();
		// 若是調用close()方法,則將其放入鏈接池,而不是真正關閉數據庫鏈接
		if (CLOSE.hashCode() == methodName.hashCode() && CLOSE.equals(methodName)) {
			dataSource.pushConnection(this);
			return null;
		}
		try {
			// 若是不是Object類的方法
			if (!Object.class.equals(method.getDeclaringClass())) {
				// issue #579 toString() should never fail
				// throw an SQLException instead of a Runtime
				checkConnection();// 檢查當前鏈接是否有效
			}
			return method.invoke(realConnection, args);
		} catch (Throwable t) {
			throw ExceptionUtil.unwrapThrowable(t);
		}
	}

	private void checkConnection() throws SQLException {
		if (!valid) {
			throw new SQLException("Error accessing PooledConnection. Connection is invalid.");
		}
	}
}
複製代碼

失控的阿甘,樂於分享,記錄點滴性能

相關文章
相關標籤/搜索