您好,登錄后才能下訂單哦!
這篇文章主要介紹“sharding-jdbc中AbstractDataSourceAdapter的用法”,在日常操作中,相信很多人在sharding-jdbc中AbstractDataSourceAdapter的用法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”sharding-jdbc中AbstractDataSourceAdapter的用法”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
本文主要研究一下sharding-jdbc的AbstractDataSourceAdapter
incubator-shardingsphere-4.0.0-RC1/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/unsupported/AbstractUnsupportedOperationDataSource.java
public abstract class AbstractUnsupportedOperationDataSource extends WrapperAdapter implements DataSource { @Override public final int getLoginTimeout() throws SQLException { throw new SQLFeatureNotSupportedException("unsupported getLoginTimeout()"); } @Override public final void setLoginTimeout(final int seconds) throws SQLException { throw new SQLFeatureNotSupportedException("unsupported setLoginTimeout(int seconds)"); } }
AbstractUnsupportedOperationDataSource繼承了WrapperAdapter,聲明實現javax.sql.DataSource接口,其覆蓋了getLoginTimeout、setLoginTimeout方法,拋出SQLFeatureNotSupportedException異常
incubator-shardingsphere-4.0.0-RC1/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/AbstractDataSourceAdapter.java
@Getter @Setter public abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOperationDataSource implements AutoCloseable { private final DatabaseType databaseType; private final Map<String, DataSource> dataSourceMap; private ShardingTransactionManagerEngine shardingTransactionManagerEngine = new ShardingTransactionManagerEngine(); private PrintWriter logWriter = new PrintWriter(System.out); public AbstractDataSourceAdapter(final Map<String, DataSource> dataSourceMap) throws SQLException { databaseType = getDatabaseType(dataSourceMap.values()); shardingTransactionManagerEngine.init(databaseType, dataSourceMap); this.dataSourceMap = dataSourceMap; } protected final DatabaseType getDatabaseType(final Collection<DataSource> dataSources) throws SQLException { DatabaseType result = null; for (DataSource each : dataSources) { DatabaseType databaseType = getDatabaseType(each); Preconditions.checkState(null == result || result.equals(databaseType), String.format("Database type inconsistent with '%s' and '%s'", result, databaseType)); result = databaseType; } return result; } private DatabaseType getDatabaseType(final DataSource dataSource) throws SQLException { if (dataSource instanceof AbstractDataSourceAdapter) { return ((AbstractDataSourceAdapter) dataSource).databaseType; } try (Connection connection = dataSource.getConnection()) { return DatabaseType.valueFrom(connection.getMetaData().getDatabaseProductName()); } } @Override public final Logger getParentLogger() { return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); } @Override public final Connection getConnection(final String username, final String password) throws SQLException { return getConnection(); } @Override public void close() throws Exception { for (DataSource each : dataSourceMap.values()) { try { Method method = each.getClass().getDeclaredMethod("close"); method.setAccessible(true); method.invoke(each); } catch (final ReflectiveOperationException ignored) { } } shardingTransactionManagerEngine.close(); } }
AbstractDataSourceAdapter繼承了AbstractUnsupportedOperationDataSource,實現了AutoCloseable接口;它的構造器接收一個DataSource的map,并執行shardingTransactionManagerEngine.init;close方法會遍歷dataSourceMap,挨個反射調用執行close方法
incubator-shardingsphere-4.0.0-RC1/sharding-transaction/sharding-transaction-core/src/main/java/org/apache/shardingsphere/transaction/ShardingTransactionManagerEngine.java
@Slf4j public final class ShardingTransactionManagerEngine { private final Map<TransactionType, ShardingTransactionManager> transactionManagerMap = new HashMap<>(); public ShardingTransactionManagerEngine() { loadShardingTransactionManager(); } private void loadShardingTransactionManager() { for (ShardingTransactionManager each : ServiceLoader.load(ShardingTransactionManager.class)) { if (transactionManagerMap.containsKey(each.getTransactionType())) { log.warn("Find more than one {} transaction manager implementation class, use `{}` now", each.getTransactionType(), transactionManagerMap.get(each.getTransactionType()).getClass().getName()); continue; } transactionManagerMap.put(each.getTransactionType(), each); } } /** * Initialize sharding transaction managers. * * @param databaseType database type * @param dataSourceMap data source map */ public void init(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) { for (Entry<TransactionType, ShardingTransactionManager> entry : transactionManagerMap.entrySet()) { entry.getValue().init(databaseType, getResourceDataSources(dataSourceMap)); } } private Collection<ResourceDataSource> getResourceDataSources(final Map<String, DataSource> dataSourceMap) { List<ResourceDataSource> result = new LinkedList<>(); for (Map.Entry<String, DataSource> entry : dataSourceMap.entrySet()) { result.add(new ResourceDataSource(entry.getKey(), entry.getValue())); } return result; } /** * Get sharding transaction manager. * * @param transactionType transaction type * @return sharding transaction manager */ public ShardingTransactionManager getTransactionManager(final TransactionType transactionType) { ShardingTransactionManager result = transactionManagerMap.get(transactionType); if (TransactionType.LOCAL != transactionType) { Preconditions.checkNotNull(result, "Cannot find transaction manager of [%s]", transactionType); } return result; } /** * Close sharding transaction managers. * * @throws Exception exception */ public void close() throws Exception { for (Entry<TransactionType, ShardingTransactionManager> entry : transactionManagerMap.entrySet()) { entry.getValue().close(); } } }
ShardingTransactionManagerEngine維護了ShardingTransactionManager的map,其構造器執行loadShardingTransactionManager方法,它會使用ServiceLoader.load(ShardingTransactionManager.class)加載,然后放入transactionManagerMap中;init方法會遍歷transactionManagerMap,然后挨個執行init方法;close方法則遍歷transactionManagerMap,挨個執行close方法
AbstractDataSourceAdapter繼承了AbstractUnsupportedOperationDataSource,實現了AutoCloseable接口;它的構造器接收一個DataSource的map,并執行shardingTransactionManagerEngine.init;close方法會遍歷dataSourceMap,挨個反射調用執行close方法
到此,關于“sharding-jdbc中AbstractDataSourceAdapter的用法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。