ShardingSphere5.4.0 SQL执行过程

1、初始化时,数据库连接池工具类,创建连接池时,连接池由连接代理类执行SQL

com.zaxxer.hikari.pool.HikariProxyConnection#delegate

实属delegate是真实连接实体类,当真实执行sql时,再从连接池中获取连接实体类

com.zaxxer.hikari.util.DriverDataSource#getConnection

创建连接的Driver为配置的:

spring.datasource.driver-class-name=org.apache.shardingsphere.driver.ShardingSphereDriver

2、连接池创建

com.zaxxer.hikari.HikariDataSource.#getConnection()

com.zaxxer.hikari.pool.HikariPool#HikariPool

com.zaxxer.hikari.pool.PoolEntry

创建连接实体 final PoolEntry poolEntry = createPoolEntry();

return new PoolEntry(newConnection(), this, isReadOnly, isAutoCommit);

连接实体创建连接

3、数据库连接实体创建

org.apache.shardingsphere.driver.jdbc.core.driver.DriverDataSourceCache#createDataSource

从这里开始一直往下跟

org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource#getConnection

在这个类创建链接 DriverStateContext.getConnection(databaseName, contextManager, jdbcContext);

创建方式通过SPI 加载DriverState.class实现类

org.apache.shardingsphere.driver.state#DriverStateContext

4、执行sql拦截

connection.prepareStatement(sql);

connection是连接池,每个连接封装了代理类

com.zaxxer.hikari.pool.HikariProxyConnection#prepareStatement

由代理连接真实执行

com.zaxxer.hikari.pool.ProxyConnection#prepareStatement

真实连接类

org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection#prepareStatement

3、ShardingSpherePreparedStatement构造方法初始化

org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement

ShardingSpherePreparedStatement#execute