You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2019/10/16 15:32:38 UTC

[GitHub] [incubator-shardingsphere] qixiaobo commented on issue #1496: Read write splitter for `Select ... for update` should route to master data node

qixiaobo commented on issue #1496: Read write splitter for `Select ... for update` should route to master data node
URL: https://github.com/apache/incubator-shardingsphere/issues/1496#issuecomment-542760093
 
 
   ```java
   public final class MasterSlaveRouter {
       
       private final MasterSlaveRule masterSlaveRule;
       
       private final SQLParseEngine parseEngine;
       
       private final boolean showSQL;
       
       /**
        * Route Master slave.
        *
        * @param sql SQL
        * @param useCache use cache or not
        * @return data source names
        */
       // TODO for multiple masters may return more than one data source
       public Collection<String> route(final String sql, final boolean useCache) {
           Collection<String> result = route(parseEngine.parse(sql, useCache));
           if (showSQL) {
               SQLLogger.logSQL(sql, result);
           }
           return result;
       }
       
       private Collection<String> route(final SQLStatement sqlStatement) {
           if (isMasterRoute(sqlStatement)) {
               MasterVisitedManager.setMasterVisited();
               return Collections.singletonList(masterSlaveRule.getMasterDataSourceName());
           }
           return Collections.singletonList(masterSlaveRule.getLoadBalanceAlgorithm().getDataSource(
                   masterSlaveRule.getName(), masterSlaveRule.getMasterDataSourceName(), new ArrayList<>(masterSlaveRule.getSlaveDataSourceNames())));
       }
       
       private boolean isMasterRoute(final SQLStatement sqlStatement) {
           return !(sqlStatement instanceof SelectStatement) || MasterVisitedManager.isMasterVisited() || HintManager.isMasterRouteOnly();
       }
   ```
   You should use hintManager

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services