Search in sources :

Example 26 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class DalClientFactory method getClient.

public static DalClient getClient(String logicDbName) {
    if (logicDbName == null)
        throw new NullPointerException("Database Set name can not be null");
    DalClient dalClient = null;
    DalConfigure config = getDalConfigure();
    // Verify if it is existed
    config.getDatabaseSet(logicDbName);
    String className = config.getFactory().getProperty(CUSTOM_DAL_CLIENT_CLASS);
    if (StringUtils.isEmpty(className)) {
        className = DalPropertiesManager.getInstance().getDalPropertiesLocator().getCustomerClientClassName();
    }
    if (StringUtils.isEmpty(className)) {
        dalClient = new DalDirectClient(config, logicDbName);
    } else {
        try {
            dalClient = (DalClient) Class.forName(className).newInstance();
            ((DalDirectClient) dalClient).init(config, logicDbName);
        } catch (Throwable t) {
            throw new DalRuntimeException(CREATE_CUSTOMER_CLIENT_ERROR, t);
        }
    }
    return dalClient;
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalConfigure(com.ctrip.platform.dal.dao.configure.DalConfigure) DalDirectClient(com.ctrip.platform.dal.dao.client.DalDirectClient)

Example 27 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class DalClassScanner method getClasses.

@Override
public List<Class<?>> getClasses(String packageName, boolean recursive) {
    long startTime = System.currentTimeMillis();
    // fileCount.set(0);
    // jarCount.set(0);
    List<Class<?>> classList = new Vector<>();
    if (null == packageName || packageName.trim().isEmpty()) {
        throw new IllegalArgumentException("packageName should not be null or empty");
    }
    packageName = packageName.trim();
    String packagePath = packageName.replace(PACKAGE_SEPARATOR, PATH_SEPARATOR);
    try {
        Enumeration<URL> urls = classLoader.getResources(packagePath);
        List<URL> urlList = new ArrayList<>();
        while (urls.hasMoreElements()) {
            URL url = urls.nextElement();
            if (url != null) {
                urlList.add(url);
            }
        }
        findClasses(classList, packageName, urlList, recursive);
    } catch (IOException e) {
        throw new DalRuntimeException(e);
    }
    long endTime = System.currentTimeMillis();
    LOGGER.info("=========================================================================");
    LOGGER.info(String.format("Time cost: %d ms", endTime - startTime));
    LOGGER.info(String.format("%d classes found", classList.size()));
    // LOGGER.info(String.format("%d files scanned", fileCount.get()));
    // LOGGER.info(String.format("%d jars scanned", jarCount.get()));
    LOGGER.info("=========================================================================");
    return classList;
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Vector(java.util.Vector) URL(java.net.URL)

Example 28 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class AbstractCombinedInsertTask method execute.

@Override
public Integer execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
    StatementParameters parameters = new StatementParameters();
    StringBuilder values = new StringBuilder();
    Set<String> unqualifiedColumns = taskContext.getUnqualifiedColumns();
    List<String> finalInsertableColumns = buildValidColumnsForInsert(unqualifiedColumns);
    String insertColumns = combineColumns(finalInsertableColumns, COLUMN_SEPARATOR);
    List<Map<String, Object>> identityFields = new ArrayList<>();
    int startIndex = 1;
    for (Integer index : daoPojos.keySet()) {
        Map<String, ?> pojo = daoPojos.get(index);
        removeUnqualifiedColumns(pojo, unqualifiedColumns);
        Map<String, Object> identityField = getIdentityField(pojo);
        if (identityField != null) {
            identityFields.add(identityField);
        }
        int paramCount = addParameters(startIndex, parameters, pojo, finalInsertableColumns);
        startIndex += paramCount;
        values.append(String.format("(%s),", combine("?", paramCount, ",")));
    }
    // Put identityFields and pojos count into context
    if (taskContext instanceof DefaultTaskContext) {
        ((DefaultTaskContext) taskContext).setIdentityFields(identityFields);
        ((DefaultTaskContext) taskContext).setPojosCount(daoPojos.size());
    }
    String tableName = getRawTableName(hints);
    if (taskContext instanceof DalContextConfigure) {
        ((DalContextConfigure) taskContext).addTables(tableName);
        ((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
    }
    String sql = String.format(getSqlTpl(), quote(tableName), insertColumns, values.substring(0, values.length() - 2) + ")");
    if (client instanceof DalContextClient)
        return ((DalContextClient) client).update(sql, parameters, hints, taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
}
Also used : DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters) ArrayList(java.util.ArrayList) DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) Map(java.util.Map)

Example 29 with DalRuntimeException

use of com.ctrip.platform.dal.exceptions.DalRuntimeException in project dal by ctripcorp.

the class AbstractBatchInsertTask method execute.

@Override
public int[] execute(DalHints hints, Map<Integer, Map<String, ?>> daoPojos, DalBulkTaskContext<T> taskContext) throws SQLException {
    StatementParameters[] parametersList = new StatementParameters[daoPojos.size()];
    int i = 0;
    Set<String> unqualifiedColumns = taskContext.getUnqualifiedColumns();
    for (Integer index : daoPojos.keySet()) {
        Map<String, ?> pojo = daoPojos.get(index);
        removeUnqualifiedColumns(pojo, unqualifiedColumns);
        StatementParameters parameters = new StatementParameters();
        addParameters(parameters, pojo);
        parametersList[i++] = parameters;
    }
    String tableName = getRawTableName(hints);
    if (taskContext instanceof DalContextConfigure) {
        ((DalContextConfigure) taskContext).addTables(tableName);
        ((DalContextConfigure) taskContext).setShardingCategory(shardingCategory);
    }
    String batchInsertSql = buildBatchInsertSql(hints, unqualifiedColumns, tableName);
    int[] result;
    if (client instanceof DalContextClient)
        result = ((DalContextClient) client).batchUpdate(batchInsertSql, parametersList, hints, taskContext);
    else
        throw new DalRuntimeException("The client is not instance of DalClient");
    return result;
}
Also used : DalRuntimeException(com.ctrip.platform.dal.exceptions.DalRuntimeException) DalContextClient(com.ctrip.platform.dal.dao.DalContextClient) StatementParameters(com.ctrip.platform.dal.dao.StatementParameters)

Aggregations

DalRuntimeException (com.ctrip.platform.dal.exceptions.DalRuntimeException)29 DalContextClient (com.ctrip.platform.dal.dao.DalContextClient)8 StatementParameters (com.ctrip.platform.dal.dao.StatementParameters)8 ConnectionString (com.ctrip.framework.dal.cluster.client.database.ConnectionString)4 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 Callback (com.ctrip.platform.dal.dao.log.Callback)3 Map (java.util.Map)3 Cluster (com.ctrip.framework.dal.cluster.client.Cluster)2 UpdatableEntity (com.ctrip.platform.dal.dao.UpdatableEntity)2 RouteStrategy (com.ctrip.platform.dal.dao.datasource.cluster.strategy.RouteStrategy)2 Node (org.w3c.dom.Node)2 HostSpec (com.ctrip.framework.dal.cluster.client.base.HostSpec)1 Listener (com.ctrip.framework.dal.cluster.client.base.Listener)1 ClusterConfig (com.ctrip.framework.dal.cluster.client.config.ClusterConfig)1 DalConfigCustomizedOption (com.ctrip.framework.dal.cluster.client.config.DalConfigCustomizedOption)1 ClusterConfigException (com.ctrip.framework.dal.cluster.client.exception.ClusterConfigException)1 CustomDataSourceFactory (com.ctrip.framework.dal.cluster.client.extended.CustomDataSourceFactory)1 DatabaseShard (com.ctrip.framework.dal.cluster.client.shard.DatabaseShard)1 ClusterIdGeneratorConfig (com.ctrip.framework.dal.cluster.client.sharding.idgen.ClusterIdGeneratorConfig)1