sharding-jdbc中SQLExecutionHook的用法
本篇内容主要讲解“sharding-jdbc中SQLExecutionHook的用法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“sharding-jdbc中SQLExecutionHook的用法”吧!
创新互联主要为客户提供服务项目涵盖了网页视觉设计、VI标志设计、营销推广、网站程序开发、HTML5响应式重庆网站建设公司、手机网站开发、微商城、网站托管及网站维护、WEB系统开发、域名注册、国内外服务器租用、视频、平面设计、SEO优化排名。设计、前端、后端三个建站步骤的完善服务体系。一人跟踪测试的建站服务标准。已经为石凉亭行业客户提供了网站推广服务。
序
本文主要研究一下sharding-jdbc的SQLExecutionHook
SQLExecutionHook
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-execute/src/main/java/org/apache/shardingsphere/core/execute/hook/SQLExecutionHook.java
public interface SQLExecutionHook { /** * Handle when SQL execution started. * * @param routeUnit route unit to be executed * @param dataSourceMetaData data source meta data * @param isTrunkThread is execution in trunk thread * @param shardingExecuteDataMap sharding execute data map */ void start(RouteUnit routeUnit, DataSourceMetaData dataSourceMetaData, boolean isTrunkThread, MapshardingExecuteDataMap); /** * Handle when SQL execution finished success. */ void finishSuccess(); /** * Handle when SQL execution finished failure. * * @param cause failure cause */ void finishFailure(Exception cause); }
SQLExecutionHook接口定义了start、finishSuccess、finishFailure方法
SPISQLExecutionHook
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-execute/src/main/java/org/apache/shardingsphere/core/execute/hook/SPISQLExecutionHook.java
public final class SPISQLExecutionHook implements SQLExecutionHook { private final CollectionsqlExecutionHooks = NewInstanceServiceLoader.newServiceInstances(SQLExecutionHook.class); static { NewInstanceServiceLoader.register(SQLExecutionHook.class); } @Override public void start(final RouteUnit routeUnit, final DataSourceMetaData dataSourceMetaData, final boolean isTrunkThread, final Map shardingExecuteDataMap) { for (SQLExecutionHook each : sqlExecutionHooks) { each.start(routeUnit, dataSourceMetaData, isTrunkThread, shardingExecuteDataMap); } } @Override public void finishSuccess() { for (SQLExecutionHook each : sqlExecutionHooks) { each.finishSuccess(); } } @Override public void finishFailure(final Exception cause) { for (SQLExecutionHook each : sqlExecutionHooks) { each.finishFailure(cause); } } }
SPISQLExecutionHook实现了SQLExecutionHook接口;它使用NewInstanceServiceLoader注册了SQLExecutionHook;sqlExecutionHooks集合由NewInstanceServiceLoader.newServiceInstances创建;start方法遍历sqlExecutionHooks,执行其start方法;finishSuccess方法则遍历sqlExecutionHooks,执行其finishSuccess方法;finishFailure方法则遍历sqlExecutionHooks,执行其finishFailure方法
OpenTracingSQLExecutionHook
incubator-shardingsphere-4.0.0-RC1/sharding-opentracing/src/main/java/org/apache/shardingsphere/opentracing/hook/OpenTracingSQLExecutionHook.java
public final class OpenTracingSQLExecutionHook implements SQLExecutionHook { private static final String OPERATION_NAME = "/" + ShardingTags.COMPONENT_NAME + "/executeSQL/"; private ActiveSpan activeSpan; private Span span; @Override public void start(final RouteUnit routeUnit, final DataSourceMetaData dataSourceMetaData, final boolean isTrunkThread, final MapshardingExecuteDataMap) { if (!isTrunkThread) { activeSpan = ((ActiveSpan.Continuation) shardingExecuteDataMap.get(OpenTracingRootInvokeHook.ACTIVE_SPAN_CONTINUATION)).activate(); } span = ShardingTracer.get().buildSpan(OPERATION_NAME) .withTag(Tags.COMPONENT.getKey(), ShardingTags.COMPONENT_NAME) .withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT) .withTag(Tags.PEER_HOSTNAME.getKey(), dataSourceMetaData.getHostName()) .withTag(Tags.PEER_PORT.getKey(), dataSourceMetaData.getPort()) .withTag(Tags.DB_TYPE.getKey(), "sql") .withTag(Tags.DB_INSTANCE.getKey(), routeUnit.getDataSourceName()) .withTag(Tags.DB_STATEMENT.getKey(), routeUnit.getSqlUnit().getSql()) .withTag(ShardingTags.DB_BIND_VARIABLES.getKey(), toString(routeUnit.getSqlUnit().getParameters())).startManual(); } private String toString(final List
OpenTracingSQLExecutionHook实现了SQLExecutionHook接口,其start方法创建并启动span、activeSpan;finishSuccess及finishFailure方法都会执行span.finish()及activeSpan.deactivate(),只是finishFailure则会标记span的exception信息
SQLExecuteCallback
incubator-shardingsphere-4.0.0-RC1/sharding-core/sharding-core-execute/src/main/java/org/apache/shardingsphere/core/execute/sql/execute/SQLExecuteCallback.java
@RequiredArgsConstructor public abstract class SQLExecuteCallbackimplements ShardingGroupExecuteCallback { private final DatabaseType databaseType; private final boolean isExceptionThrown; @Override public final Collection execute(final Collection statementExecuteUnits, final boolean isTrunkThread, final Map shardingExecuteDataMap) throws SQLException { Collection result = new LinkedList<>(); for (StatementExecuteUnit each : statementExecuteUnits) { result.add(execute0(each, isTrunkThread, shardingExecuteDataMap)); } return result; } private T execute0(final StatementExecuteUnit statementExecuteUnit, final boolean isTrunkThread, final Map shardingExecuteDataMap) throws SQLException { ExecutorExceptionHandler.setExceptionThrown(isExceptionThrown); DataSourceMetaData dataSourceMetaData = DataSourceMetaDataFactory.newInstance(databaseType, statementExecuteUnit.getStatement().getConnection().getMetaData().getURL()); SQLExecutionHook sqlExecutionHook = new SPISQLExecutionHook(); try { sqlExecutionHook.start(statementExecuteUnit.getRouteUnit(), dataSourceMetaData, isTrunkThread, shardingExecuteDataMap); T result = executeSQL(statementExecuteUnit.getRouteUnit(), statementExecuteUnit.getStatement(), statementExecuteUnit.getConnectionMode()); sqlExecutionHook.finishSuccess(); return result; } catch (final SQLException ex) { sqlExecutionHook.finishFailure(ex); ExecutorExceptionHandler.handleException(ex); return null; } } protected abstract T executeSQL(RouteUnit routeUnit, Statement statement, ConnectionMode connectionMode) throws SQLException; }
SQLExecuteCallback的execute0方法在执行前创建SPISQLExecutionHook,然后调用sqlExecutionHook.start方法,执行成功之后执行sqlExecutionHook.finishSuccess方法,捕获到SQLException则执行sqlExecutionHook.finishFailure方法
小结
SQLExecutionHook接口定义了start、finishSuccess、finishFailure方法;SPISQLExecutionHook实现了SQLExecutionHook接口;它使用NewInstanceServiceLoader注册了SQLExecutionHook;sqlExecutionHooks集合由NewInstanceServiceLoader.newServiceInstances创建;start方法遍历sqlExecutionHooks,执行其start方法;finishSuccess方法则遍历sqlExecutionHooks,执行其finishSuccess方法;finishFailure方法则遍历sqlExecutionHooks,执行其finishFailure方法
到此,相信大家对“sharding-jdbc中SQLExecutionHook的用法”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
标题名称:sharding-jdbc中SQLExecutionHook的用法
路径分享:http://scyanting.com/article/jjodis.html