SpringJDBC源码初探-DataSource类

一、DataSource接口核心作用

DataSource是JDBC规范的核心接口,位于javax.sql包中,用于替代传统的DriverManager获取数据库连接。Spring框架通过org.springframework.jdbc.datasource包对该接口进行了增强,提供连接池管理、事务绑定等高级特性。

二、DataSource源码分析

核心接口javax.sql.DataSource

public interface DataSource  extends CommonDataSource, Wrapper {

  // 获取数据库连接
  Connection getConnection() throws SQLException;
  // 使用凭证获取连接
  Connection getConnection(String username, String password)
    throws SQLException;
}

可以看到,DataSource接口提供了获取连接的的方法,并且DataSource继承了两个父接口CommonDataSource和Wrapper,CommonDataSource定义如下:

public interface CommonDataSource {
    // 获取日志记录器
    PrintWriter getLogWriter() throws SQLException;
    
    // 设置日志记录器
    void setLogWriter(PrintWriter out) throws SQLException;
    
    // 设置登录超时时间(秒)
    void setLoginTimeout(int seconds) throws SQLException;
    
    // 获取登录超时时间
    int getLoginTimeout() throws SQLException;
    
    // 获取父Logger
    default Logger getParentLogger() throws SQLFeatureNotSupportedException {
        throw new SQLFeatureNotSupportedException();
    }
}

这里CommonDataSource 提供了获取和设置日志的方法,连接超时管理以及获取父Logger的方法。

public interface Wrapper {
    // 检查是否实现指定接口
    boolean isWrapperFor(Class<?> iface) throws SQLException;
    
    // 获取接口实现
    <T> T unwrap(Class<T> iface) throws SQLException;
}

Wrapper主要用于获取特定扩展功能

AbstractDataSource抽象类,主要提供DataSource接口中的某些方法(如getLoginTimeout()、setLoginTimeout(int)等)的默认实现
主要的继承关系如下

AbstractDataSource
├── AbstractDriverBasedDataSource
│   ├── DriverManagerDataSource
│   └── SimpleDriverDataSource
├── AbstractRoutingDataSource
    └──IsolationLevelDataSourceRouter

  1. DriverManagerDataSource核心方法
public class DriverManagerDataSource extends AbstractDriverBasedDataSource {
    @Override
    protected Connection getConnectionFromDriver(String username, String password) throws SQLException {
        Properties mergedProps = new Properties();
        // 合并连接属性
        Properties connProps = getConnectionProperties();
        if (connProps != null) {
            mergedProps.putAll(connProps);
        }
        if (username != null) {
            mergedProps.setProperty("user", username);
        }
        if (password != null) {
            mergedProps.setProperty("password", password);
        }
        // 关键点:每次通过DriverManager新建连接
        return DriverManager.getConnection(getUrl(), mergedProps);
    }
}

说明:通过用户名密码从驱动获取连接,每次调用 getConnection() 都创建一条新连接,无连接池功能,适合测试环境。

2. SingleConnectionDataSource方法

public class SingleConnectionDataSource extends AbstractDriverBasedDataSource {
    private volatile Connection connection;
    
    @Override
    protected Connection getConnectionFromDriver(String username, String password) throws SQLException {
        synchronized (this) {
            if (this.connection == null) {
                // 初始化唯一连接
                this.connection = doGetConnection(username, password);
            }
            return this.connection;
        }
    }
    
    protected Connection doGetConnection(String username, String password) throws SQLException {
        // 实际创建连接逻辑
        Properties mergedProps = new Properties();
        // ...属性合并逻辑与DriverManagerDataSource类似
        return DriverManager.getConnection(getUrl(), mergedProps);
    }
}

说明:单例模式来维护唯一连接,直接使用JDBC Driver实例,线程安全通过synchronized和volatile保证。

3.AbstractRoutingDataSource
AbstractRoutingDataSource实现动态数据源路由抽象类,主要属性如下

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {
    // 目标数据源映射表
    private Map<Object, Object> targetDataSources;
    // 默认数据源
    private Object defaultTargetDataSource;
    // 解析后的数据源映射表
    private Map<Object, DataSource> resolvedDataSources;
    // 解析后的默认数据源
    private DataSource resolvedDefaultDataSource;
    // 数据源查找接口
    private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
    // 是否宽松回退到默认数据源
    private boolean lenientFallback = true;
}

初始化方法(afterPropertiesSet)

@Override
	public void afterPropertiesSet() {
		if (this.targetDataSources == null) {
			throw new IllegalArgumentException("Property 'targetDataSources' is required");
		}
		this.resolvedDataSources = CollectionUtils.newHashMap(this.targetDataSources.size());
		this.targetDataSources.forEach((key, value) -> {
			Object lookupKey = resolveSpecifiedLookupKey(key);
			DataSource dataSource = resolveSpecifiedDataSource(value);
			this.resolvedDataSources.put(lookupKey, dataSource);
		});
		if (this.defaultTargetDataSource != null) {
			this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource);
		}
	}

说明:将配置的targetDataSources转换为可用的resolvedDataSources
获取连接的逻辑:

@Override
public Connection getConnection() throws SQLException {
	return determineTargetDataSource().getConnection();
}
protected DataSource determineTargetDataSource() {
    Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
    // 获取当前查找键
    Object lookupKey = determineCurrentLookupKey();
    // 根据键查找数据源
    DataSource dataSource = this.resolvedDataSources.get(lookupKey);
    // 回退到默认数据源
    if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
        dataSource = this.resolvedDefaultDataSource;
    }
    if (dataSource == null) {
        throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
    }
    return dataSource;
}

AbstractRoutingDataSource定义了determineCurrentLookupKey()抽象方法,子类仅需实现此方法提供键值获取逻辑。

核心逻辑:

  1. 初始化阶段:
  • . 实现InitializingBean接口,在afterPropertiesSet()中解析targetDataSources,生成resolvedDataSources
  • defaultTargetDataSource解析为resolvedDefaultDataSource
  1. 运行时路由:
  • 通过determineCurrentLookupKey()抽象方法获取当前数据源标识
  • 根据标识从resolvedDataSources中查找对应的数据源
  • 未找到时根据lenientFallback决定是否使用默认数据源

4.IsolationLevelDataSourceRouter(基于事务隔离级别的路由)

public class IsolationLevelDataSourceRouter extends AbstractRoutingDataSource {
    private static final Constants constants = new Constants(TransactionDefinition.class);
    
    @Override
    protected Object resolveSpecifiedLookupKey(Object lookupKey) {
        // 解析隔离级别配置
        if (lookupKey instanceof Integer) return lookupKey;
        if (lookupKey instanceof String) {
            String constantName = (String) lookupKey;
            if (!constantName.startsWith(DefaultTransactionDefinition.PREFIX_ISOLATION)) {
                throw new IllegalArgumentException("Only isolation constants allowed");
            }
            return constants.asNumber(constantName);
        }
        throw new IllegalArgumentException("Invalid lookup key");
    }
    
    @Override
    protected Object determineCurrentLookupKey() {
        // 从当前事务同步管理器中获取隔离级别
        return TransactionSynchronizationManager.getCurrentTransactionIsolationLevel();
    }
}

特点:

  • 根据事务隔离级别选择数据源
  • 支持通过整数或字符串常量配置隔离级别
内容概要:本文详细解析了2014年全国大学生电子设计竞赛C题——智能小车设计的全过程。文章首先介绍了该竞赛的背景及其重要意义,指出其不仅是对学生电子设计能力的考验,还对学生的学术成长和职业发展有深远影响。随后,文章深入剖析了C题的具体要求,包括小车的起跑、行驶、超车等复杂动作,强调了硬件(如控制模块、电源模块、车体、电机模块)和软件(如信号检测与控制、两车通信、节能技术、程序设计)方面的关键技术和实现方法。最后,文章分享了测试与优化的经验,并总结了团队合作、知识储备和实践能力的重要性,展望了电子设计领域的发展趋势。 适合人群:电子信息专业学生、电子设计爱好者及希望深入了解智能小车设计的技术人员。 使用场景及目标:①了解全国大学生电子设计竞赛的背景和重要性;②掌握智能小车设计的硬件选型和软件编程技巧;③学习信号检测与控制、两车通信、节能技术等关键技术;④借鉴测试与优化的经验,提升实际动手能力和解决问题的能力。 阅读建议:本文内容详实,涵盖了从理论到实践的各个方面。建议读者在阅读过程中结合实际操作,逐步理解和掌握智能小车设计的各项技术和原理,特别是对硬件电路设计和软件编程部分,可以通过搭建实验平台进行实践,加深理解。同时,关注文中提到的测试与优化策略,有助于提高实际项目的成功率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值