In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the usage of AbstractDataSourceAdapter in sharding-jdbc". In daily operation, I believe many people have doubts about the usage of AbstractDataSourceAdapter in sharding-jdbc. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about the usage of AbstractDataSourceAdapter in sharding-jdbc! Next, please follow the editor to study!
Order
This paper mainly studies the AbstractDataSourceAdapter of sharding-jdbc.
AbstractUnsupportedOperationDataSource
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 inherits WrapperAdapter, declares to implement the javax.sql.DataSource interface, overrides the getLoginTimeout and setLoginTimeout methods, and throws a SQLFeatureNotSupportedException exception
AbstractDataSourceAdapter
Incubator-shardingsphere-4.0.0-RC1/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/AbstractDataSourceAdapter.java
@ Getter@Setterpublic abstract class AbstractDataSourceAdapter extends AbstractUnsupportedOperationDataSource implements AutoCloseable {private final DatabaseType databaseType; private final Map dataSourceMap; private ShardingTransactionManagerEngine shardingTransactionManagerEngine = new ShardingTransactionManagerEngine (); private PrintWriter logWriter = new PrintWriter (System.out); public AbstractDataSourceAdapter (final Map dataSourceMap) throws SQLException {databaseType = getDatabaseType (dataSourceMap.values ()); shardingTransactionManagerEngine.init (databaseType, dataSourceMap); this.dataSourceMap = dataSourceMap } protected final DatabaseType getDatabaseType (final Collection 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 inherits AbstractUnsupportedOperationDataSource and implements the AutoCloseable interface; its constructor receives a map of DataSource and executes the shardingTransactionManagerEngine.init;close method to traverse the dataSourceMap, and each reflection call executes the close method
ShardingTransactionManagerEngine
Incubator-shardingsphere-4.0.0-RC1/sharding-transaction/sharding-transaction-core/src/main/java/org/apache/shardingsphere/transaction/ShardingTransactionManagerEngine.java
@ Slf4jpublic final class ShardingTransactionManagerEngine {private final Map 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 dataSourceMap) {for (Entry entry: transactionManagerMap.entrySet ()) {entry.getValue () .init (databaseType, getResourceDataSources (dataSourceMap));} private Collection getResourceDataSources (final Map dataSourceMap) {List result = new LinkedList () For (Map.Entry 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 entry: transactionManagerMap.entrySet ()) {entry.getValue () .close ();}
ShardingTransactionManagerEngine maintains ShardingTransactionManager's map, whose constructor executes the loadShardingTransactionManager method, which loads using ServiceLoader.load (ShardingTransactionManager.class) and then puts it into transactionManagerMap; the init method traverses the transactionManagerMap and then executes the init method one by one; the close method traverses the transactionManagerMap and executes the close method one by one
Summary
AbstractDataSourceAdapter inherits AbstractUnsupportedOperationDataSource and implements the AutoCloseable interface; its constructor receives a map of DataSource and executes the shardingTransactionManagerEngine.init;close method to traverse the dataSourceMap, and each reflection call executes the close method
At this point, the study of "the use of AbstractDataSourceAdapter in sharding-jdbc" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.