示例#1
0
 /**
  * 优先使用本地配置
  *
  * @param config
  */
 private void overByLocal(DataSourceConfig config) {
   if (null == config || null == localConfig) {
     return;
   }
   if (StringUtil.isNotBlank(localConfig.getDriverClassName())) {
     config.setDriverClassName(localConfig.getDriverClassName());
   }
   if (StringUtil.isNotBlank(localConfig.getSorterClassName())) {
     config.setSorterClassName(localConfig.getSorterClassName());
   }
   if (StringUtil.isNotBlank(localConfig.getPassword())) {
     config.setPassword(localConfig.getPassword());
   }
   if (null != localConfig.getConnectionProperties()
       && !localConfig.getConnectionProperties().isEmpty()) {
     config.setConnectionProperties(localConfig.getConnectionProperties());
   }
 }
示例#2
0
  /**
   * @param dsConfig
   * @return
   */
  private LocalTxDataSourceConfig dataSourceConfig2LocalTxDataSourceConfig(
      DataSourceConfig dsConfig) {
    LocalTxDataSourceConfig config = new LocalTxDataSourceConfig();
    config.setJndiName(dsConfig.getDbName());
    config.setUserName(dsConfig.getUserName());
    config.setPassword(dsConfig.getPassword());
    config.setDriverClassName(dsConfig.getDriverClassName());
    config.setExceptionSorterClassName(dsConfig.getSorterClassName());
    if (dsConfig.getDbType() == DBType.MYSQL) {
      String connectionURL =
          DBCononectionURLTool.getMySqlConnectionURL(
              dsConfig.getIp(),
              dsConfig.getPort(),
              dsConfig.getDbName(),
              dsConfig.getConnectionProperties());
      config.setConnectionURL(connectionURL);
      // 如果可以找到mysql driver中的Valid就使用,否则不设置valid
      try {
        Class validClass = Class.forName(DBConstants.DEFAULT_MYSQL_VALID_CONNECTION_CHECKERCLASS);
        if (null != validClass) {
          config.setValidConnectionCheckerClassName(
              DBConstants.DEFAULT_MYSQL_VALID_CONNECTION_CHECKERCLASS);
        } else {
          logger.warn(
              "MYSQL Driver is Not Suport "
                  + DBConstants.DEFAULT_MYSQL_VALID_CONNECTION_CHECKERCLASS);
        }
      } catch (ClassNotFoundException e) {
        logger.warn(
            "MYSQL Driver is Not Suport "
                + DBConstants.DEFAULT_MYSQL_VALID_CONNECTION_CHECKERCLASS);
      } catch (NoClassDefFoundError e) {
        logger.warn(
            "MYSQL Driver is Not Suport "
                + DBConstants.DEFAULT_MYSQL_VALID_CONNECTION_CHECKERCLASS);
      }

      // 如果可以找到mysqlDriver中的integrationSorter就使用否则使用默认的
      try {
        Class integrationSorterCalss = Class.forName(DBConstants.MYSQL_INTEGRATION_SORTER_CLASS);
        if (null != integrationSorterCalss) {
          config.setExceptionSorterClassName(DBConstants.MYSQL_INTEGRATION_SORTER_CLASS);
        } else {
          config.setExceptionSorterClassName(DBConstants.DEFAULT_MYSQL_SORTER_CLASS);
          logger.warn(
              "MYSQL Driver is Not Suport "
                  + DBConstants.MYSQL_INTEGRATION_SORTER_CLASS
                  + " use default sorter "
                  + DBConstants.DEFAULT_MYSQL_SORTER_CLASS);
        }
      } catch (ClassNotFoundException e) {
        logger.warn(
            "MYSQL Driver is Not Suport "
                + DBConstants.MYSQL_INTEGRATION_SORTER_CLASS
                + " use default sorter "
                + DBConstants.DEFAULT_MYSQL_SORTER_CLASS);
      } catch (NoClassDefFoundError e) {
        logger.warn(
            "MYSQL Driver is Not Suport "
                + DBConstants.MYSQL_INTEGRATION_SORTER_CLASS
                + " use default sorter "
                + DBConstants.DEFAULT_MYSQL_SORTER_CLASS);
      }
    } else if (dsConfig.getDbType() == DBType.ORACLE) {
      String connectionURL =
          DBCononectionURLTool.getOracleConnectionURL(
              dsConfig.getIp(),
              dsConfig.getPort(),
              dsConfig.getDbName(),
              dsConfig.getOracleConnectionType());
      config.setConnectionURL(connectionURL);
      // 如果是oracle没有设置ConnectionProperties则给以个默认的
      if (!dsConfig.getConnectionProperties().isEmpty()) {
        config.setConnectionProperties(dsConfig.getConnectionProperties());
      } else {
        config.setConnectionProperties(DBConstants.DEFAULT_ORACLE_CONNECTION_PROPERTIES);
      }
    }

    config.setMinPoolSize(dsConfig.getMinPoolSize());
    config.setMaxPoolSize(dsConfig.getMaxPoolSize());
    config.setPreparedStatementCacheSize(dsConfig.getPreparedStatementCacheSize());
    if (dsConfig.getIdleTimeout() > 0) {
      config.setIdleTimeoutMinutes(dsConfig.getIdleTimeout());
    }
    if (dsConfig.getBlockingTimeout() > 0) {
      config.setBlockingTimeoutMillis(dsConfig.getBlockingTimeout());
    }
    return config;
  }