Sharding-Jdbc源码学习(二):jdbcTemplate与shardingDataSource关联关系

上文配置了8个库,那么执行一条SQL时拆分-JDBC是如何取与对应的Darasource建立连接并执行对应的SQL的??

一,配置shardingDataSource

  spring.xml配置的JdbcTemplate类的类的,并且将shardingDataSource设置到的JdbcTemplate的类的类的数据源属性中。


    

二,JdbcTemplate的的的的的执行SQL的步骤解析

mybatis执行对应的sql时调用jdbcTemplate中的execute(final String sql)抛出DataAccessException方法,jdbcTemplate中源码如下:

@Override
public void execute(final String sql) throws DataAccessException {
   if (logger.isDebugEnabled()) {
      logger.debug("Executing SQL statement [" + sql + "]");
   }

   class ExecuteStatementCallback implements StatementCallback, SqlProvider {
      @Override
      @Nullable
      public Object doInStatement(Statement stmt) throws SQLException {
         stmt.execute(sql);  statement执行对应的execute方法
         return null;
      }
      @Override
      public String getSql() {
         return sql;
      }
   }

   execute(new ExecuteStatementCallback());   执行对应sql时实际上jdbcTemplate调用的是下边excute方法

} 
  
@Override
@Nullable
public  T execute(StatementCallback action) throws DataAccessException {
   Assert.notNull(action, "Callback object must not be null");

   Connection con = DataSourceUtils.getConnection(obtainDataSource());   先创建对应的Connection
   Statement stmt = null;
   try {
      stmt = con.createStatement(); 由 connection创建对应的statement   
      applyStatementSettings(stmt);
      T result = action.doInStatement(stmt);  --statement执行对应的execute方法
      handleWarnings(stmt);
      return result;  返回结果集
   }
   catch (SQLException ex) {
      // Release Connection early, to avoid potential connection pool deadlock
      // in the case when the exception translator hasn't been initialized yet.
      String sql = getSql(action);
      JdbcUtils.closeStatement(stmt);
      stmt = null;
      DataSourceUtils.releaseConnection(con, getDataSource());
      con = null;
      throw translateException("StatementCallback", sql, ex);
   }
   finally {
      JdbcUtils.closeStatement(stmt);
      DataSourceUtils.releaseConnection(con, getDataSource());
   }
}

总结:由此可见JDBC执行SQL的步骤如下

1,DataSourceUtils.getConnection(obtainDataSource()); 创建连接,该处只是获得连接对象并未真正建立连接

2,由 “Connection” 创建对应的 “Statement

3, “Statement” 执行对应的“execute(sql)”方法

4,返回结果集

三,JdbcTemplate的的的的的的执行SQL如何与分片-JDBC关联

如图1所示,首先我们来看看DataSourceUtils.getConnection(obtainDataSource())是如何创建的连接对象的。

public static Connection getConnection(DataSource dataSource) throws CannotGetJdbcConnectionException {
   try {
      return doGetConnection(dataSource);  
   }
   catch (SQLException ex) {
      throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection", ex);
   }
   catch (IllegalStateException ex) {
      throw new CannotGetJdbcConnectionException("Failed to obtain JDBC Connection: " + ex.getMessage());
   }
}

 该代码是jdbcTemplate中创建Connection的源码,具体由doGetConnection再调用fetchConnection(DataSource dataSource)。

private static Connection fetchConnection(DataSource dataSource) throws SQLException {
   Connection con = dataSource.getConnection();
   if (con == null) {
      throw new IllegalStateException("DataSource returned null from getConnection(): " + dataSource);
   }
   return con;
}

 由代码可以看出最终是从的的的的的的DataSource.getConnection()中获取的连接对象,那么的的的的的的JdbcTemplate中配置的数据源是shardingDataSource,所以具体看看shardingDataSouce是如何的的的的的getConnection();

 

 

 

 

你可能感兴趣的:(Sharding-Jdbc源码学习(二):jdbcTemplate与shardingDataSource关联关系)