Search in sources :

Example 1 with MetricsContext

use of com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext in project sharding-jdbc by dangdangdotcom.

the class ShardingConnection method getConnectionInternal.

private Connection getConnectionInternal(final String dataSourceName, final SQLStatementType sqlStatementType) throws SQLException {
    Optional<Connection> connectionOptional = fetchCachedConnectionBySqlStatementType(dataSourceName, sqlStatementType);
    if (connectionOptional.isPresent()) {
        return connectionOptional.get();
    }
    Context metricsContext = MetricsContext.start(Joiner.on("-").join("ShardingConnection-getConnection", dataSourceName));
    DataSource dataSource = shardingContext.getShardingRule().getDataSourceRule().getDataSource(dataSourceName);
    Preconditions.checkState(null != dataSource, "Missing the rule of %s in DataSourceRule", dataSourceName);
    String realDataSourceName = dataSourceName;
    if (dataSource instanceof MasterSlaveDataSource) {
        dataSource = ((MasterSlaveDataSource) dataSource).getDataSource(sqlStatementType);
        realDataSourceName = getRealDataSourceName(dataSourceName, sqlStatementType);
    }
    Connection result = dataSource.getConnection();
    MetricsContext.stop(metricsContext);
    connectionMap.put(realDataSourceName, result);
    return result;
}
Also used : MetricsContext(com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext) Context(com.codahale.metrics.Timer.Context) Connection(java.sql.Connection) DataSource(javax.sql.DataSource)

Aggregations

Context (com.codahale.metrics.Timer.Context)1 MetricsContext (com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext)1 Connection (java.sql.Connection)1 DataSource (javax.sql.DataSource)1