Search in sources :

Example 1 with ShardingJdbcException

use of io.shardingjdbc.core.exception.ShardingJdbcException in project sharding-jdbc by shardingjdbc.

the class TableRule method generateDataNodes.

private List<DataNode> generateDataNodes(final List<String> actualDataNodes, final Collection<String> dataSourceNames) {
    List<DataNode> result = new LinkedList<>();
    for (String each : actualDataNodes) {
        DataNode dataNode = new DataNode(each);
        if (!dataSourceNames.contains(dataNode.getDataSourceName())) {
            throw new ShardingJdbcException("Cannot find data source in sharding rule, invalid actual data node is: '%s'", each);
        }
        result.add(dataNode);
    }
    return result;
}
Also used : ShardingJdbcException(io.shardingjdbc.core.exception.ShardingJdbcException) ToString(lombok.ToString) LinkedList(java.util.LinkedList)

Example 2 with ShardingJdbcException

use of io.shardingjdbc.core.exception.ShardingJdbcException in project sharding-jdbc by shardingjdbc.

the class ExecuteEventListener method handle.

private void handle(final AbstractSQLExecutionEvent event, final String operation) {
    Tracer tracer = ShardingJDBCTracer.get();
    switch(event.getEventExecutionType()) {
        case BEFORE_EXECUTE:
            if (ExecutorDataMap.getDataMap().containsKey(SNAPSHOT_DATA_KEY) && !isCurrentMainThread()) {
                trunkInBranchContainer.set(((ActiveSpan.Continuation) ExecutorDataMap.getDataMap().get(SNAPSHOT_DATA_KEY)).activate());
            }
            branchContainer.set(tracer.buildSpan("/SJDBC/BRANCH/" + operation).withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT).withTag(Tags.PEER_HOSTNAME.getKey(), event.getDataSource()).withTag(Tags.COMPONENT.getKey(), "ShardingJDBC").withTag(Tags.DB_INSTANCE.getKey(), event.getDataSource()).withTag(Tags.DB_TYPE.getKey(), "sql").withTag(Tags.DB_STATEMENT.getKey(), event.getSql()).startManual());
            break;
        case EXECUTE_FAILURE:
            Span span = branchContainer.get();
            span.setTag(Tags.ERROR.getKey(), true);
            if (event.getException().isPresent()) {
                span.log(System.currentTimeMillis(), log(event.getException().get()));
            }
            finish();
            break;
        case EXECUTE_SUCCESS:
            finish();
            break;
        default:
            throw new ShardingJdbcException("Unsupported event type");
    }
}
Also used : ActiveSpan(io.opentracing.ActiveSpan) Tracer(io.opentracing.Tracer) ShardingJdbcException(io.shardingjdbc.core.exception.ShardingJdbcException) ActiveSpan(io.opentracing.ActiveSpan) Span(io.opentracing.Span)

Example 3 with ShardingJdbcException

use of io.shardingjdbc.core.exception.ShardingJdbcException in project sharding-jdbc by shardingjdbc.

the class ExecuteEventListener method listenOverall.

/**
 * listen overall sql execution event.
 *
 * @param event Overall sql execution event.
 */
@Subscribe
@AllowConcurrentEvents
public void listenOverall(final OverallExecutionEvent event) {
    Tracer tracer = ShardingJDBCTracer.get();
    ActiveSpan activeSpan;
    switch(event.getEventExecutionType()) {
        case BEFORE_EXECUTE:
            activeSpan = tracer.buildSpan("/SJDBC/TRUNK/" + event.getSqlType().name()).withTag(Tags.COMPONENT.getKey(), "ShardingJDBC").startActive();
            trunkContainer.set(activeSpan);
            if (isParallelExecute(event)) {
                ExecutorDataMap.getDataMap().put(SNAPSHOT_DATA_KEY, activeSpan.capture());
            }
            break;
        case EXECUTE_FAILURE:
            activeSpan = trunkContainer.get();
            activeSpan.setTag(Tags.ERROR.getKey(), true);
            if (event.getException().isPresent()) {
                activeSpan.log(System.currentTimeMillis(), log(event.getException().get()));
            }
            deactivate();
            break;
        case EXECUTE_SUCCESS:
            deactivate();
            break;
        default:
            throw new ShardingJdbcException("Unsupported event type");
    }
}
Also used : ActiveSpan(io.opentracing.ActiveSpan) Tracer(io.opentracing.Tracer) ShardingJdbcException(io.shardingjdbc.core.exception.ShardingJdbcException) AllowConcurrentEvents(com.google.common.eventbus.AllowConcurrentEvents) Subscribe(com.google.common.eventbus.Subscribe)

Example 4 with ShardingJdbcException

use of io.shardingjdbc.core.exception.ShardingJdbcException in project sharding-jdbc by shardingjdbc.

the class PropertyUtil method v2.

private static Object v2(final Environment environment, final String prefix, final Class<?> targetClass) {
    try {
        Class<?> binderClass = Class.forName("org.springframework.boot.context.properties.bind.Binder");
        Method getMethod = binderClass.getDeclaredMethod("get", Environment.class);
        Method bindMethod = binderClass.getDeclaredMethod("bind", String.class, Class.class);
        Object binderObject = getMethod.invoke(null, environment);
        String prefixParam = prefix.endsWith(".") ? prefix.substring(0, prefix.length() - 1) : prefix;
        Object bindResultObject = bindMethod.invoke(binderObject, prefixParam, targetClass);
        Method resultGetMethod = bindResultObject.getClass().getDeclaredMethod("get");
        return resultGetMethod.invoke(bindResultObject);
    } catch (final ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
        throw new ShardingJdbcException(ex.getMessage(), ex);
    }
}
Also used : ShardingJdbcException(io.shardingjdbc.core.exception.ShardingJdbcException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with ShardingJdbcException

use of io.shardingjdbc.core.exception.ShardingJdbcException in project sharding-jdbc by shardingjdbc.

the class MasterSlaveLoadBalanceAlgorithmGsonTypeAdapter method read.

@Override
public MasterSlaveLoadBalanceAlgorithm read(final JsonReader in) throws IOException {
    String masterSlaveLoadBalanceAlgorithmClassName = null;
    in.beginObject();
    while (in.hasNext()) {
        Preconditions.checkArgument("masterSlaveLoadBalanceAlgorithm".equals(in.nextName()));
        masterSlaveLoadBalanceAlgorithmClassName = in.nextString();
    }
    in.endObject();
    try {
        return Strings.isNullOrEmpty(masterSlaveLoadBalanceAlgorithmClassName) ? null : (MasterSlaveLoadBalanceAlgorithm) Class.forName(masterSlaveLoadBalanceAlgorithmClassName).newInstance();
    } catch (ReflectiveOperationException ex) {
        throw new ShardingJdbcException(ex);
    }
}
Also used : ShardingJdbcException(io.shardingjdbc.core.exception.ShardingJdbcException)

Aggregations

ShardingJdbcException (io.shardingjdbc.core.exception.ShardingJdbcException)11 ActiveSpan (io.opentracing.ActiveSpan)2 Tracer (io.opentracing.Tracer)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 DataSource (javax.sql.DataSource)2 Optional (com.google.common.base.Optional)1 AllowConcurrentEvents (com.google.common.eventbus.AllowConcurrentEvents)1 Subscribe (com.google.common.eventbus.Subscribe)1 Span (io.opentracing.Span)1 NamedDataSource (io.shardingjdbc.core.jdbc.core.datasource.NamedDataSource)1 IndexPlaceholder (io.shardingjdbc.core.rewrite.placeholder.IndexPlaceholder)1 SchemaPlaceholder (io.shardingjdbc.core.rewrite.placeholder.SchemaPlaceholder)1 ShardingPlaceholder (io.shardingjdbc.core.rewrite.placeholder.ShardingPlaceholder)1 TablePlaceholder (io.shardingjdbc.core.rewrite.placeholder.TablePlaceholder)1 LinkedList (java.util.LinkedList)1 TreeMap (java.util.TreeMap)1 ToString (lombok.ToString)1 DatabaseConfig (org.dbunit.database.DatabaseConfig)1 IDatabaseConnection (org.dbunit.database.IDatabaseConnection)1