在数据持久层中,数据源是一个非常重要的组件,期性能直接关系到整个数据持久层的性能。在实践中比较常见的第三方数据源组件有DBCP、C3P0、阿里druid和Springboot2推荐的HikariCP等,Mybatis不仅可以集成第三方数据源组件,还提供了自己的数据源实现。
Mybatis数据源模块包结构如下:
常见的数据源组件都实现了 javax.sql.DataSource
接口,Mybatis自身实现的数据源实现也不例外。Mybatis提供了两个 javax.sql.DataSource
接口实现,分别是PooledDataSource和UnpooledDataSource。
org.apache.ibatis.datasource.unpooled.UnpooledDataSource
实现DataSource接口,非池化的DataSource对象。代码如下:
/** * 非池化DataSource * @author Clinton Begin * @author Eduardo Macarron */ public class UnpooledDataSource implements DataSource { // Driver 类加载器 private ClassLoader driverClassLoader; // Driver 属性 private Properties driverProperties; // 已注册的 Driver 映射 private static Map<String, Driver> registeredDrivers = new ConcurrentHashMap<>(); // Driver 类名 private String driver; // 数据库 URL private String url; // 数据库用户名 private String username; // 数据库密码 private String password; // 是否自动提交事务 private Boolean autoCommit; // 默认事务隔离级别 private Integer defaultTransactionIsolationLevel; static { // 初始化 registeredDrivers 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 { // 判断 registeredDrivers是否已经存在该 driver,若不存在,进行初始化 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); } } 复制代码
org.apache.ibatis.datasource.pooled.PooledDataSource
实现DataSource接口,池化的DataSource实现类。代码如下:
复制代码
本章内容会比较多,这周会陆续把它更完。防止手贱Ctrl+z把草稿回滚没了。先发布一版(喜欢的朋友可以关注一下)